#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*/ exit(int);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int main() {
  const char filename[] = "binary.dat";
  FILE *fp;
  int a, r;
  fp = fopen(filename, "r");
  if (fp == NULL) {
    fprintf(stderr, "Unable to open %s\n", filename);
    exit(1);
  }
  while (1) {
    r = fread(&a, sizeof(int), 1, fp);
    if (r != 1)
      break;
    printf("%d\n", a);
  }
  r = fclose(fp);
  if (r == EOF)
    perror("File closing error");
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe77(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe6f(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0xf(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e0 <main+0x70>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    -0xf(%rbp),%rdx
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11e5 <main+0x75>
mov    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
je     120e <main+0x9e>
jmp    1222 <main+0xb2>
mov    -0x1c(%rbp),%esi
lea    0xe06(%rip),%rdi
call   1050 <printf@plt>
jmp    11e5 <main+0x75>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%eax
jne    124c <main+0xdc>
lea    0xddb(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movabs $0x642e7972616e6962,%rax
79 2e 64
mov    %rax,0x10(%rsp)
movl   $0x746164,0x17(%rsp)
lea    0xe9c(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1233 <main+0xc3>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    1206 <main+0x96>
lea    0xe4e(%rip),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11e0 <main+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2dd3(%rip),%rcx
cmp    (%rcx),%eax
je     1225 <main+0xb5>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdf6(%rip),%rdi
call   1080 <perror@plt>
jmp    1219 <main+0xa9>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.main.filename = [11 x i8] c"binary.dat\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
@.str.3 = [19 x i8] c"File closing error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [11 x i8]
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [11 x i8]* %2 to i8*
%7 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %3
%9 = load i32*, i32** %3
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
br label %17
17: ; preds = %16, %23
%18 = load i32*, i32** %3
%19 = call i32 @fread(i32* %4, i32 4, i32 1, i32* %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %17
br label %26
23: ; preds = %17
%24 = load i32, i32* %4
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %17
26: ; preds = %22
%27 = load i32*, i32** %3
%28 = call i32 @fclose(i32* %27)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %26
ret i32 0
}

LLVM IR (-O2):
@__const.main.filename = [11 x i8] c"binary.dat\00"
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
@.str.3 = [19 x i8] c"File closing error\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %5)
%9 = icmp eq i32 %8, 1
br i1 %9, label %14, label %19
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %7, %14
%15 = load i32, i32* %2
%16 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %5)
%18 = icmp eq i32 %17, 1
br i1 %18, label %14, label %19
19: ; preds = %14, %7
%20 = tail call i32 @fclose(i32* %5)
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
ret i32 0
}