#include <stdio.h>

/* Constants */
int INT_MIN = -2147483648;
int INT_MAX = 2147483647;

/* Multiplies two integers passed as arguments */
int main() {
  int a;
  int b;
  int c;

  printf("Enter the first integer\n");
  scanf("%d", &a);
  if (inputOutOfRange(a)) return 0;

  printf("Enter the second integer\n");
  scanf("%d", &b);
  if (inputOutOfRange(b)) return 0;

  if (overflow(a, b)) return 0;
  c = a * b;
  printf("%d * %d = %d\n", a, b, c);

  return 0;
}

/* Checks if user input is within 32bit int limits */
int inputOutOfRange(int input) {
  if ((INT_MIN < input) || (input < INT_MAX)) {
    return 0;  /* False: user input is good */
  } else {
    printf("Integer entered is outside 32bit range\n");
    return 1;  /* True: user input is outside 32bit int limits */
  }
}

/* Checks for integer overflow during multiplication */
int overflow(int a, int b) {
  if ((a == 1 || b == 1) ||
      (!(b != 0 && a > INT_MAX / b)) ||
      (!(a != 0 && b > INT_MAX / a))) {
    return 0;  /* False: proceed program to the next step */
  } else {
    printf("Multiplication caused overflow. Please enter smaller numbers\n");
    return 1;  /* True: overflow occured */
  }
}
