#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*/ exit(int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ stderr;
main() {
  FILE *fp, *fp1;
  int x[100], i, j;
  fp = fopen("test.dat", "r");
  if (fp == NULL) {
    fprintf(stderr, "file test.dat does not exist\n");
    exit(1);
  }
  fp1 = fopen("output.dat", "w");
  if (fp == NULL) {
    fprintf(stderr, "creating write file failed\n");
    exit(1);
  }
  i = 0;
  while (fscanf(fp, "%d", &x[i]) != EOF)
    i++;
  for (j = 0; j < i; j++)
    fprintf(fp1, "%d\n", x[j]);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <main+0x56>
mov    0x2e5d(%rip),%rax
movslq (%rax),%rdi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe8c(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11da <main+0x9a>
mov    0x2e19(%rip),%rax
movslq (%rax),%rdi
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0x0,-0x1b4(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x1b4(%rbp),%rax
lea    -0x1b0(%rbp),%rdx
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe4e(%rip),%rsi
call   1030 <fscanf@plt>
mov    0x2dd8(%rip),%rcx
cmp    (%rcx),%rax
je     122d <main+0xed>
mov    -0x1b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b4(%rbp)
jmp    11e4 <main+0xa4>
movl   $0x0,-0x1b8(%rbp)
mov    -0x1b8(%rbp),%eax
cmp    -0x1b4(%rbp),%eax
jge    127d <main+0x13d>
mov    -0x18(%rbp),%rdi
movslq -0x1b8(%rbp),%rax
mov    -0x1b0(%rbp,%rax,4),%edx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1b8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b8(%rbp)
jmp    1237 <main+0xf7>
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x198,%rsp
lea    0xe98(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     120d <main+0xbd>
mov    %rax,%r15
lea    0xea2(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r14
mov    $0xffffffff,%ebp
mov    %rsp,%rbx
lea    0xe91(%rip),%r12
mov    0x2e3c(%rip),%r13
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <fscanf@plt>
add    $0x1,%ebp
add    $0x4,%rbx
cmp    0x0(%r13),%rax
jne    11b0 <main+0x60>
test   %ebp,%ebp
je     11f9 <main+0xa9>
mov    %ebp,%ebp
lea    0xe61(%rip),%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%rsp,%rbx,4),%edx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11e0 <main+0x90>
xor    %eax,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc4(%rip),%rax
movslq (%rax),%rcx
lea    0xded(%rip),%rdi
mov    $0x1d,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.dat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"file test.dat does not exist\0A\00"
@.str.3 = [11 x i8] c"output.dat\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [28 x i8] c"creating write file failed\0A\00"
@.str.6 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.7 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [100 x i32]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %17, i32** %3
%18 = load i32*, i32** %2
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32*
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
store i32 0, i32* %5
br label %27
27: ; preds = %35, %26
%28 = load i32*, i32** %2
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [100 x i32], [100 x i32]* %4, i64 0, i64 %30
%32 = call i64 @fscanf(i32* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %31)
%33 = load i64, i64* @EOF
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %27
38: ; preds = %27
store i32 0, i32* %6
br label %39
39: ; preds = %50, %38
%40 = load i32, i32* %6
%41 = load i32, i32* %5
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %53
43: ; preds = %39
%44 = load i32*, i32** %3
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [100 x i32], [100 x i32]* %4, i64 0, i64 %46
%48 = load i32, i32* %47
%49 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %43
%51 = load i32, i32* %6
%52 = add nsw i32 %51, 1
store i32 %52, i32* %6
br label %39
53: ; preds = %39
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.dat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [30 x i8] c"file test.dat does not exist\0A\00"
@.str.3 = [11 x i8] c"output.dat\00"
@.str.4 = [2 x i8] c"w\00"
@.str.6 = [3 x i8] c"%d\00"
@EOF = global i64 0
@.str.7 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [100 x i32]
%2 = bitcast [100 x i32]* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %11
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = sext i32 %6 to i64
%8 = inttoptr i64 %7 to i32*
%9 = tail call i64 @fwrite(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 29, i64 1, i32* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %13
13: ; preds = %13, %11
%14 = phi i64 [ %19, %13 ], [ 0, %11 ]
%15 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 %14
%16 = call i64 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %15)
%17 = load i64, i64* @EOF
%18 = icmp eq i64 %16, %17
%19 = add nuw i64 %14, 1
br i1 %18, label %20, label %13
20: ; preds = %13
%21 = trunc i64 %14 to i32
%22 = icmp eq i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = and i64 %14, 4294967295
br label %25
25: ; preds = %23, %25
%26 = phi i64 [ 0, %23 ], [ %30, %25 ]
%27 = getelementptr inbounds [100 x i32], [100 x i32]* %1, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %28)
%30 = add nuw nsw i64 %26, 1
%31 = icmp eq i64 %30, %24
br i1 %31, label %32, label %25
32: ; preds = %25, %20
ret i32 0
}