#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int read_file(uint8_t *program, const char *fn) {
  FILE *fp;
  int readcount;
  fp = fopen(fn, "rb");
  if (!fp) {
    fprintf(stderr, "Can't open '%s': %s\n", fn, strerror(errno));
    return -1;
  }
  readcount = fread(program, sizeof(uint8_t), (0x1000 - 0x200), fp);
  if (readcount < 1) {
    if (readcount == 0 && feof(fp))
      fprintf(stderr, "File empty.\n");
    else
      fprintf(stderr, "Failed read from '%s': %s\n", fn, strerror(errno));
    readcount = -1;
  } else if (!feof(fp)) {
    fprintf(stderr, "File too long. Maximum size is %d.\n", (0x1000 - 0x200));
    readcount = -1;
  }
  fclose(fp);
  return readcount;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d5 <read_file+0x75>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c2 <read_file+0x162>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x4,%esi
mov    $0xe00,%edx
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    1278 <read_file+0x118>
cmpl   $0x0,-0x24(%rbp)
jne    1232 <read_file+0xd2>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     1232 <read_file+0xd2>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    126c <read_file+0x10c>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    12b3 <read_file+0x153>
mov    -0x20(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
jne    12ae <read_file+0x14e>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xda5(%rip),%rsi
mov    $0xe00,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    12b3 <read_file+0x153>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe8f(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ce <read_file+0x6e>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0xe00,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1202 <read_file+0xa2>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
jne    125c <read_file+0xfc>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
mov    $0xe00,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1257 <read_file+0xf7>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ebx
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe17(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1264 <read_file+0x104>
je     1233 <read_file+0xd3>
mov    0x2dd5(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe03(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1257 <read_file+0xf7>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %rax,%rax
je     1204 <read_file+0xa4>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Can't open '%s': %s\0A\00"
@errno = global i32 0
@.str.2 = [13 x i8] c"File empty.\0A\00"
@.str.3 = [27 x i8] c"Failed read from '%s': %s\0A\00"
@.str.4 = [36 x i8] c"File too long. Maximum size is %d.\0A\00"
define i32 @read_file(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %18, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %5
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %16)
store i32 -1, i32* %3
br label %53
18: ; preds = %2
%19 = load i32*, i32** %4
%20 = load i32*, i32** %6
%21 = call i32 @fread(i32* %19, i32 4, i32 3584, i32* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %41
24: ; preds = %18
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i32*, i32** %6
%29 = call i64 @feof(i32* %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %40
34: ; preds = %27, %24
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %5
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %36, i8* %38)
br label %40
40: ; preds = %34, %31
store i32 -1, i32* %7
br label %49
41: ; preds = %18
%42 = load i32*, i32** %6
%43 = call i64 @feof(i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %48, label %45
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 3584)
store i32 -1, i32* %7
br label %48
48: ; preds = %45, %41
br label %49
49: ; preds = %48, %40
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* %7
store i32 %52, i32* %3
br label %53
53: ; preds = %49, %12
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Can't open '%s': %s\0A\00"
@errno = global i32 0
@.str.2 = [13 x i8] c"File empty.\0A\00"
@.str.3 = [27 x i8] c"Failed read from '%s': %s\0A\00"
@.str.4 = [36 x i8] c"File too long. Maximum size is %d.\0A\00"
define i32 @read_file(i32* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %8)
br label %35
10: ; preds = %2
%11 = tail call i32 @fread(i32* %0, i32 4, i32 3584, i32* %3)
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %26
13: ; preds = %10
%14 = icmp eq i32 %11, 0
br i1 %14, label %15, label %21
15: ; preds = %13
%16 = tail call i64 @feof(i32* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %32
21: ; preds = %15, %13
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %1, i8* %24)
br label %32
26: ; preds = %10
%27 = tail call i64 @feof(i32* %3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0), i32 3584)
br label %32
32: ; preds = %18, %21, %26, %29
%33 = phi i32 [ %11, %26 ], [ -1, %29 ], [ -1, %21 ], [ -1, %18 ]
%34 = tail call i32 @fclose(i32* %3)
br label %35
35: ; preds = %32, %5
%36 = phi i32 [ %33, %32 ], [ -1, %5 ]
ret i32 %36
}