#include <stdio.h>		/* fprintf(), printf(), stderr() */
#include <stdlib.h>		/* atoi(), exit() */

/* Greatest common divisor function gcd(a,b).
 * Expect nonnegative integers a,b.
 * If a!=0, recursively compute and return gcd(b%a,a)==gcd(a,b);
 * Else a==0, so return b.  This is gcd(a,b) if b>0, or 0 if b==0.
 */
int
gcd(int a, int b) {
  return a ? gcd(b%a,a) : b;
}

int
usage(const char *execname) {
  fprintf(stderr, "usage: %s a b\n where a,b are integers.\n", execname);
  exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
  int a,b;

  if(argc!=3) usage(argv[0]);	/* check for two command-line arguments */
  a = atoi(argv[1]);
  b = atoi(argv[2]);

  /* Replace a,b with equivalent nonnegative values: */
  a = a<0? -a : a;		/* a <-- |a| */
  b = b<0? -b : b;		/* b <-- |b| */
  
  /* Compute and print gcd(|a|,|b|) */
  printf("%d\n", gcd(a,b));

  return EXIT_SUCCESS;
}
  

