#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *);
void scanInput(int *a, int *b, int n) {
  int i;
  FILE *fileA = fopen("./examples/q1/inputA.txt", "r");
  FILE *fileB = fopen("./examples/q1/inputB.txt", "r");
  if (fileA == NULL || fileB == NULL) {
    printf("\nERROR: FILE_FAILED_TO_OPEN");
    exit(0);
  }
  for (i = 0; i < n; i++) {
    fscanf(fileA, "%d ", &a[i]);
    fscanf(fileB, "%d ", &b[i]);
  }
  fclose(fileA);
  fclose(fileB);
}


x86 assembly (clang, -O0):
<scanInput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    0xe96(%rip),%rdi
lea    0xea8(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a7 <scanInput+0x57>
cmpq   $0x0,-0x28(%rbp)
jne    11ba <scanInput+0x6a>
lea    0xe86(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1219 <scanInput+0xc9>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe69(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe4a(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c1 <scanInput+0x71>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanInput>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe96(%rip),%rdi
lea    0xea8(%rip),%r12
mov    %r12,%rsi
call   1060 <fopen@plt>
mov    %rax,%r15
lea    0xe98(%rip),%rdi
mov    %r12,%rsi
call   1060 <fopen@plt>
test   %r15,%r15
je     11f8 <scanInput+0xa8>
mov    %rax,%r12
test   %rax,%rax
je     11f8 <scanInput+0xa8>
test   %ebp,%ebp
jle    11da <scanInput+0x8a>
mov    %ebp,%ebp
lea    0xeab(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <fscanf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1050 <fscanf@plt>
add    $0x4,%r14
add    $0x4,%rbx
add    $0xffffffffffffffff,%rbp
jne    11b0 <scanInput+0x60>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"./examples/q1/inputA.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"./examples/q1/inputB.txt\00"
@.str.3 = [28 x i8] c"\0AERROR: FILE_FAILED_TO_OPEN\00"
@.str.4 = [4 x i8] c"%d \00"
define void @scanInput(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %8
%11 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32*, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14, %3
%18 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %14
store i32 0, i32* %7
br label %21
21: ; preds = %38, %20
%22 = load i32, i32* %7
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = load i32*, i32** %8
%27 = load i32*, i32** %4
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = call i32 @fscanf(i32* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %30)
%32 = load i32*, i32** %9
%33 = load i32*, i32** %5
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = call i32 @fscanf(i32* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %36)
br label %38
38: ; preds = %25
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
br label %21
41: ; preds = %21
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
%44 = load i32*, i32** %9
%45 = call i32 @fclose(i32* %44)
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"./examples/q1/inputA.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [25 x i8] c"./examples/q1/inputB.txt\00"
@.str.3 = [28 x i8] c"\0AERROR: FILE_FAILED_TO_OPEN\00"
@.str.4 = [4 x i8] c"%d \00"
define void @scanInput(i32* %0, i32* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %4, null
%7 = icmp eq i32* %5, null
%8 = or i1 %6, %7
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %24
11: ; preds = %9
%12 = zext i32 %2 to i64
br label %16
13: ; preds = %3
%14 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %11, %16
%17 = phi i64 [ 0, %11 ], [ %22, %16 ]
%18 = getelementptr inbounds i32, i32* %0, i64 %17
%19 = tail call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %18)
%20 = getelementptr inbounds i32, i32* %1, i64 %17
%21 = tail call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32* %20)
%22 = add nuw nsw i64 %17, 1
%23 = icmp eq i64 %22, %12
br i1 %23, label %24, label %16
24: ; preds = %16, %9
%25 = tail call i32 @fclose(i32* %4)
%26 = tail call i32 @fclose(i32* %5)
ret void
}