#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getchar();
scalar_t__ malloc(int);
int /*<<< orphan*/ parseInput(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
char *removeSpaces(char *);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ *stdin;
int strlen(char *);
int main(void) {
  int ch;
  int inputSource;
  int type;
  char *inputFileString = (char *)malloc(sizeof(char) * 100);
  char *inputString = (char *)malloc(sizeof(char) * 100);
  FILE *inputFile;
  printf("Choose input source (1 = Keyboard, 2 = File): ");
  scanf("%d", &inputSource);
  if (inputSource == 1) {
    while ((ch = getchar()) != '\n' && ch != EOF)
      ;
    printf("\nEnter an expression (use single letters for variables): ");
    fgets(inputString, 100, stdin);
    inputString = removeSpaces(inputString);
    parseInput(inputString, "output.txt");
  } else if (inputSource == 2) {
    while ((ch = getchar()) != '\n' && ch != EOF)
      ;
    printf("\nEnter file path: ");
    fgets(inputFileString, 100, stdin);
    inputFileString[strlen(inputFileString) - 1] = '\0';
    inputFile = fopen(inputFileString, "r");
    if (inputFile == NULL) {
      perror("Error opening file. Check that file exists and/or path is "
             "correct.\n");
      return 2;
    } else {
      while (!feof(inputFile)) {
        if (fgets(inputString, 100, inputFile)) {
          if (inputString[strlen(inputString) - 1] == '\n')
            inputString[strlen(inputString) - 1] = '\0';
          inputString = removeSpaces(inputString);
          parseInput(inputString, inputFileString);
        }
      }
    }
  } else {
    printf("Invalid input.");
    return 1;
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x64,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x64,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    0xe1e(%rip),%rdi
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   10b0 <scanf@plt>
cmpl   $0x1,-0xc(%rbp)
jne    128d <main+0xdd>
jmp    1206 <main+0x56>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x29(%rbp)
je     1232 <main+0x82>
mov    -0x8(%rbp),%eax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1242 <main+0x92>
jmp    1247 <main+0x97>
jmp    1206 <main+0x56>
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
mov    -0x20(%rbp),%rdi
call   1030 <removeSpaces@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1060 <parseInput@plt>
jmp    1429 <main+0x279>
cmpl   $0x2,-0xc(%rbp)
jne    140c <main+0x25c>
jmp    129c <main+0xec>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x2a(%rbp)
je     12c8 <main+0x118>
mov    -0x8(%rbp),%eax
mov    0x2d20(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    12d8 <main+0x128>
jmp    12dd <main+0x12d>
jmp    129c <main+0xec>
lea    0xd93(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2cf4(%rip),%rax
mov    (%rax),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xd5d(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1359 <main+0x1a9>
lea    0xd44(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    143c <main+0x28c>
jmp    135e <main+0x1ae>
mov    -0x28(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    137c <main+0x1cc>
jmp    1402 <main+0x252>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x64,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     13fd <main+0x24d>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    13e3 <main+0x233>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1030 <removeSpaces@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <parseInput@plt>
jmp    135e <main+0x1ae>
jmp    1407 <main+0x257>
jmp    1424 <main+0x274>
lea    0xcbd(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    143c <main+0x28c>
jmp    1429 <main+0x279>
lea    0xc9e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x64,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    $0x64,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
lea    0xe17(%rip),%rdi
call   1060 <printf@plt>
lea    0xe3a(%rip),%rdi
lea    0x4(%rsp),%rsi
call   10c0 <scanf@plt>
mov    0x4(%rsp),%eax
cmp    $0x1,%eax
je     12df <main+0x11f>
cmp    $0x2,%eax
jne    1348 <main+0x188>
mov    0x2dc4(%rip),%rbx
nopl   0x0(%rax)
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    $0xa,%eax
je     1230 <main+0x70>
cmp    (%rbx),%eax
jne    1220 <main+0x60>
lea    0xe40(%rip),%rdi
call   1060 <printf@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%rdx
mov    %r14,%rdi
mov    $0x64,%esi
call   1080 <fgets@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r14,1)
lea    0xe20(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1365 <main+0x1a5>
mov    %rax,%r15
jmp    1296 <main+0xd6>
mov    %r12,%rdi
call   1040 <removeSpaces@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <parseInput@plt>
mov    %r15,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
jne    133a <main+0x17a>
mov    %r12,%rdi
mov    $0x64,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1296 <main+0xd6>
mov    %r12,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r12,1)
jne    1280 <main+0xc0>
mov    %r12,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r12,1)
jmp    1280 <main+0xc0>
mov    0x2cfa(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    $0xa,%eax
je     1300 <main+0x140>
cmp    (%rbx),%eax
jne    12f0 <main+0x130>
lea    0xd2b(%rip),%rdi
call   1060 <printf@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%rdx
mov    %r12,%rdi
mov    $0x64,%esi
call   1080 <fgets@plt>
mov    %r12,%rdi
call   1040 <removeSpaces@plt>
lea    0xd3a(%rip),%rsi
mov    %rax,%rdi
call   1070 <parseInput@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
jmp    1359 <main+0x199>
lea    0xd81(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd20(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x2,%eax
jmp    1359 <main+0x199>

LLVM IR (-O0):
@.str = [47 x i8] c"Choose input source (1 = Keyboard, 2 = File): \00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i32 0
@.str.2 = [58 x i8] c"\0AEnter an expression (use single letters for variables): \00"
@stdin = global i32* null
@.str.3 = [11 x i8] c"output.txt\00"
@.str.4 = [19 x i8] c"\0AEnter file path: \00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [68 x i8] c"Error opening file. Check that file exists and/or path is correct.\0A\00"
@.str.7 = [15 x i8] c"Invalid input.\00"
@.str.8 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
store i32 0, i32* %1
%8 = call i64 @malloc(i32 100)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %5
%10 = call i64 @malloc(i32 100)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
%12 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %36
16: ; preds = %0
br label %17
17: ; preds = %26, %16
%18 = call i32 (...) @getchar()
store i32 %18, i32* %2
%19 = icmp ne i32 %18, 10
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* %2
%22 = load i32, i32* @EOF
%23 = icmp ne i32 %21, %22
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %26, label %27
26: ; preds = %24
br label %17
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
%29 = load i8*, i8** %6
%30 = load i32*, i32** @stdin
%31 = call i64 @fgets(i8* %29, i32 100, i32* %30)
%32 = load i8*, i8** %6
%33 = call i8* @removeSpaces(i8* %32)
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = call i32 @parseInput(i8* %34, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %107
36: ; preds = %0
%37 = load i32, i32* %3
%38 = icmp eq i32 %37, 2
br i1 %38, label %39, label %104
39: ; preds = %36
br label %40
40: ; preds = %49, %39
%41 = call i32 (...) @getchar()
store i32 %41, i32* %2
%42 = icmp ne i32 %41, 10
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* %2
%45 = load i32, i32* @EOF
%46 = icmp ne i32 %44, %45
br label %47
47: ; preds = %43, %40
%48 = phi i1 [ false, %40 ], [ %46, %43 ]
br i1 %48, label %49, label %50
49: ; preds = %47
br label %40
50: ; preds = %47
%51 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%52 = load i8*, i8** %5
%53 = load i32*, i32** @stdin
%54 = call i64 @fgets(i8* %52, i32 100, i32* %53)
%55 = load i8*, i8** %5
%56 = load i8*, i8** %5
%57 = call i32 @strlen(i8* %56)
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %55, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** %5
%62 = call i32* @fopen(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %62, i32** %7
%63 = load i32*, i32** %7
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %67
65: ; preds = %50
%66 = call i32 @perror(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.6, i64 0, i64 0))
store i32 2, i32* %1
br label %109
67: ; preds = %50
br label %68
68: ; preds = %101, %67
%69 = load i32*, i32** %7
%70 = call i32 @feof(i32* %69)
%71 = icmp ne i32 %70, 0
%72 = xor i1 %71, true
br i1 %72, label %73, label %102
73: ; preds = %68
%74 = load i8*, i8** %6
%75 = load i32*, i32** %7
%76 = call i64 @fgets(i8* %74, i32 100, i32* %75)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %101
78: ; preds = %73
%79 = load i8*, i8** %6
%80 = load i8*, i8** %6
%81 = call i32 @strlen(i8* %80)
%82 = sub nsw i32 %81, 1
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %79, i64 %83
%85 = load i8, i8* %84
%86 = sext i8 %85 to i32
%87 = icmp eq i32 %86, 10
br i1 %87, label %88, label %95
88: ; preds = %78
%89 = load i8*, i8** %6
%90 = load i8*, i8** %6
%91 = call i32 @strlen(i8* %90)
%92 = sub nsw i32 %91, 1
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8, i8* %89, i64 %93
store i8 0, i8* %94
br label %95
95: ; preds = %88, %78
%96 = load i8*, i8** %6
%97 = call i8* @removeSpaces(i8* %96)
store i8* %97, i8** %6
%98 = load i8*, i8** %6
%99 = load i8*, i8** %5
%100 = call i32 @parseInput(i8* %98, i8* %99)
br label %101
101: ; preds = %95, %73
br label %68
102: ; preds = %68
br label %103
103: ; preds = %102
br label %106
104: ; preds = %36
%105 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %109
106: ; preds = %103
br label %107
107: ; preds = %106, %27
%108 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %109
109: ; preds = %107, %104, %65
%110 = load i32, i32* %1
ret i32 %110
}

LLVM IR (-O2):
@.str = [47 x i8] c"Choose input source (1 = Keyboard, 2 = File): \00"
@.str.1 = [3 x i8] c"%d\00"
@EOF = global i32 0
@.str.2 = [58 x i8] c"\0AEnter an expression (use single letters for variables): \00"
@stdin = global i32* null
@.str.3 = [11 x i8] c"output.txt\00"
@.str.4 = [19 x i8] c"\0AEnter file path: \00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [68 x i8] c"Error opening file. Check that file exists and/or path is correct.\0A\00"
@.str.7 = [15 x i8] c"Invalid input.\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 @malloc(i32 100)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @malloc(i32 100)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%9 = load i32, i32* %1
switch i32 %9, label %66 [
i32 1, label %10
i32 2, label %22
]
10: ; preds = %0, %10
%11 = call i32 (...) @getchar()
%12 = icmp ne i32 %11, 10
%13 = load i32, i32* @EOF
%14 = icmp ne i32 %11, %13
%15 = select i1 %12, i1 %14, i1 false
br i1 %15, label %10, label %16
16: ; preds = %10
%17 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32*, i32** @stdin
%19 = call i64 @fgets(i8* %6, i32 100, i32* %18)
%20 = call i8* @removeSpaces(i8* %6)
%21 = call i32 @parseInput(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %68
22: ; preds = %0, %22
%23 = call i32 (...) @getchar()
%24 = icmp ne i32 %23, 10
%25 = load i32, i32* @EOF
%26 = icmp ne i32 %23, %25
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %22, label %28
28: ; preds = %22
%29 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%30 = load i32*, i32** @stdin
%31 = call i64 @fgets(i8* %4, i32 100, i32* %30)
%32 = call i32 @strlen(i8* %4)
%33 = add nsw i32 %32, -1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %4, i64 %34
store i8 0, i8* %35
%36 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%37 = icmp eq i32* %36, null
br i1 %37, label %41, label %38
38: ; preds = %28
%39 = call i32 @feof(i32* %36)
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %68
41: ; preds = %28
%42 = call i32 @perror(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.6, i64 0, i64 0))
br label %70
43: ; preds = %38, %62
%44 = phi i8* [ %63, %62 ], [ %6, %38 ]
%45 = call i64 @fgets(i8* %44, i32 100, i32* %36)
%46 = icmp eq i64 %45, 0
br i1 %46, label %62, label %47
47: ; preds = %43
%48 = call i32 @strlen(i8* %44)
%49 = add nsw i32 %48, -1
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %44, i64 %50
%52 = load i8, i8* %51
%53 = icmp eq i8 %52, 10
br i1 %53, label %54, label %59
54: ; preds = %47
%55 = call i32 @strlen(i8* %44)
%56 = add nsw i32 %55, -1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %44, i64 %57
store i8 0, i8* %58
br label %59
59: ; preds = %54, %47
%60 = call i8* @removeSpaces(i8* %44)
%61 = call i32 @parseInput(i8* %60, i8* %4)
br label %62
62: ; preds = %59, %43
%63 = phi i8* [ %60, %59 ], [ %44, %43 ]
%64 = call i32 @feof(i32* %36)
%65 = icmp eq i32 %64, 0
br i1 %65, label %43, label %68
66: ; preds = %0
%67 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %70
68: ; preds = %62, %38, %16
%69 = call i32 @putchar(i32 10)
br label %70
70: ; preds = %68, %66, %41
%71 = phi i32 [ 0, %68 ], [ 2, %41 ], [ 1, %66 ]
ret i32 %71
}