#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 */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *);
int main() {
  FILE *fp = fopen("grades.txt", "r");
  FILE *cf = fopen("grades_copy.txt", "w");
  int first = 1, read = 0;
  if (fp == NULL || cf == NULL) {
    printf("ERROR: Could not open file.");
    return -1;
  }
  while ((fscanf(fp, "%d", &read)) != EOF) {
    if (first) {
      fprintf(cf, "%d", read);
      first = !first;
    } else
      fprintf(cf, "\n%d", read);
  }
  fclose(fp);
  fclose(cf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe90(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b1 <main+0x61>
cmpq   $0x0,-0x18(%rbp)
jne    11c9 <main+0x79>
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1256 <main+0x106>
jmp    11ce <main+0x7e>
mov    -0x10(%rbp),%rdi
lea    0xe63(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <fscanf@plt>
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
je     123d <main+0xed>
cmpl   $0x0,-0x1c(%rbp)
je     1225 <main+0xd5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe32(%rip),%rsi
call   1060 <fprintf@plt>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
jmp    1238 <main+0xe8>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe08(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11ce <main+0x7e>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rdi
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe95(%rip),%rdi
lea    0xe9e(%rip),%rsi
call   1070 <fopen@plt>
movl   $0x0,0x4(%rsp)
test   %rbx,%rbx
je     1247 <main+0xf7>
mov    %rax,%r14
test   %rax,%rax
je     1247 <main+0xf7>
lea    0xe94(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <fscanf@plt>
mov    0x2e2c(%rip),%rbp
cmp    0x0(%rbp),%rax
je     1233 <main+0xe3>
mov    0x4(%rsp),%edx
lea    0xe6f(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <fprintf@plt>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <fscanf@plt>
cmp    0x0(%rbp),%rax
je     1233 <main+0xe3>
lea    0xe46(%rip),%r15
lea    0xe40(%rip),%r12
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <fscanf@plt>
cmp    0x0(%rbp),%rax
jne    1210 <main+0xc0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1258 <main+0x108>
lea    0xdd1(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"grades.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"grades_copy.txt\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [28 x i8] c"ERROR: Could not open file.\00"
@.str.5 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.6 = [4 x i8] c"\0A%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %7, i32** %3
store i32 1, i32* %4
store i32 0, i32* %5
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %0
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10, %0
%14 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %42
15: ; preds = %10
br label %16
16: ; preds = %36, %15
%17 = load i32*, i32** %2
%18 = call i64 @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %5)
%19 = load i64, i64* @EOF
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %37
21: ; preds = %16
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i32*, i32** %3
%26 = load i32, i32* %5
%27 = call i32 @fprintf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %4
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
%31 = zext i1 %30 to i32
store i32 %31, i32* %4
br label %36
32: ; preds = %21
%33 = load i32*, i32** %3
%34 = load i32, i32* %5
%35 = call i32 @fprintf(i32* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %24
br label %16
37: ; preds = %16
%38 = load i32*, i32** %2
%39 = call i32 @fclose(i32* %38)
%40 = load i32*, i32** %3
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %1
br label %42
42: ; preds = %37, %13
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
@.str = [11 x i8] c"grades.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"grades_copy.txt\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [28 x i8] c"ERROR: Could not open file.\00"
@.str.5 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.6 = [4 x i8] c"\0A%d\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%4 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%5 = icmp eq i32* %2, null
%6 = icmp eq i32* %3, null
%7 = or i1 %5, %6
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = call i64 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%10 = load i64, i64* @EOF
%11 = icmp eq i64 %9, %10
br i1 %11, label %26, label %14
12: ; preds = %0
%13 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %29
14: ; preds = %8
%15 = load i32, i32* %1
%16 = call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %15)
%17 = call i64 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%18 = load i64, i64* @EOF
%19 = icmp eq i64 %17, %18
br i1 %19, label %26, label %20
20: ; preds = %14, %20
%21 = load i32, i32* %1
%22 = call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%23 = call i64 @fscanf(i32* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%24 = load i64, i64* @EOF
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %20
26: ; preds = %20, %14, %8
%27 = call i32 @fclose(i32* %2)
%28 = call i32 @fclose(i32* %3)
br label %29
29: ; preds = %26, %12
%30 = phi i32 [ -1, %12 ], [ 0, %26 ]
ret i32 %30
}