#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*/ *);
scalar_t__ feof(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) {
      if (feof(fp))
        break;
      else
        perror("File read error: ");
    }
    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    0xe6a(%rip),%rax
mov    %rax,-0xf(%rbp)
mov    0xe67(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe5f(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0xf(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f0 <main+0x70>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    -0xf(%rbp),%rdx
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11f5 <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     1242 <main+0xc2>
mov    -0x18(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
je     1231 <main+0xb1>
jmp    1256 <main+0xd6>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
jmp    1242 <main+0xc2>
mov    -0x1c(%rbp),%esi
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
jmp    11f5 <main+0x75>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2d7c(%rip),%rcx
cmp    (%rcx),%eax
jne    1280 <main+0x100>
lea    0xdb9(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movabs $0x642e7972616e6962,%rax
79 2e 64
mov    %rax,0x8(%rsp)
movl   $0x746164,0xf(%rsp)
lea    0xe9c(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     124c <main+0xcc>
mov    %rax,%rbx
lea    0x4(%rsp),%r14
lea    0xe65(%rip),%r15
lea    0xe4c(%rip),%r12
jmp    11ec <main+0x6c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x4(%rsp),%esi
mov    %r15,%rdi
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11e0 <main+0x60>
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    121d <main+0x9d>
mov    %r12,%rdi
call   1090 <perror@plt>
jmp    11e0 <main+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%eax
je     123e <main+0xbe>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdef(%rip),%rdi
call   1090 <perror@plt>
jmp    1230 <main+0xb0>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <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 = [18 x i8] c"File read error: \00"
@.str.3 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
@.str.4 = [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, %30
%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 %30
22: ; preds = %17
%23 = load i32*, i32** %3
%24 = call i64 @feof(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
br label %33
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27
br label %30
30: ; preds = %29, %17
%31 = load i32, i32* %4
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %17
33: ; preds = %26
%34 = load i32*, i32** %3
%35 = call i32 @fclose(i32* %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
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 = [18 x i8] c"File read error: \00"
@.str.3 = [4 x i8] c"%d\0A\00"
@EOF = global i32 0
@.str.4 = [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 %7, label %11
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0, %19
%12 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %5)
%13 = icmp eq i32 %12, 1
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i64 @feof(i32* %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %11
%20 = load i32, i32* %2
%21 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %11
22: ; preds = %14
%23 = tail call i32 @fclose(i32* %5)
%24 = load i32, i32* @EOF
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
ret i32 0
}