#include <stdio.h>

/* User to modify input file location */
//str USER_FILE = "./input_file.txt";  /* !!check why this can't be referenced */
int *USER_INT1 = 2;  /* !!check why this gives error when assigned int */
int *USER_INT2 = 3;  /* "Initialization makes pointer from integer without a cast" */

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

int main() {
  /* Read input from a file */
  FILE *fp = fopen("./input_file.txt", "r");;
  
  if (fp == NULL) {
    if (USER_INT1 != NULL || USER_INT2 != NULL) {
      multiply(USER_INT1, USER_INT2);
    } else {
      int a;
      int b;
      printf("Enter the first integer\n");
      scanf("%d", &a);
      printf("Enter the second integer\n");
      scanf("%d", &b);
      multiply(a, b);
    }
  } else {
    /* when there is a file, do something
       get fscanf() to file
       multiply(temp1, temp2)
    */
  }
  return 0;
}

/* Multiplies two integers passed as arguments */
int multiply(int a, int b) {
  /* Checks for arguments are within 32bit int range */
  if (inputOutOfRange(a)) return 0;
  if (inputOutOfRange(b)) return 0;

  /* Checks if multiplication causes overflow */
  if (overflow(a, b)) return 0;

  int 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 */
  }
}
