#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
bool LoadBinaryFile(const char *filename, int *size_bytes, void **data_ptr) {
  FILE *fd;
  int file_len;
  int num_read;
  void *file_data = NULL;
  bool had_error = false;
  fd = fopen(filename, "rb");
  if (fd == NULL) {
    had_error = true;
    goto BAIL;
  }
  fseek(fd, 0, SEEK_END);
  file_len = ftell(fd);
  if (file_len <= 0) {
    had_error = true;
    goto BAIL;
  }
  rewind(fd);
  file_data = malloc(file_len);
  if (file_data == NULL) {
    had_error = true;
    goto BAIL;
  }
  num_read = fread(file_data, 1, file_len, fd);
  if (num_read != file_len) {
    had_error = true;
    goto BAIL;
  }
BAIL:
  if (fd != NULL) {
    fclose(fd);
  }
  if (had_error) {
    if (file_data != NULL) {
      free(file_data);
      file_data = NULL;
    }
    file_len = 0;
  }
  *size_bytes = file_len;
  *data_ptr = file_data;
  return !had_error;
}


x86 assembly (clang, -O0):
<LoadBinaryFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ce <LoadBinaryFile+0x4e>
movl   $0x1,-0x34(%rbp)
jmp    1265 <LoadBinaryFile+0xe5>
mov    -0x20(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1204 <LoadBinaryFile+0x84>
movl   $0x1,-0x34(%rbp)
jmp    1265 <LoadBinaryFile+0xe5>
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x24(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1230 <LoadBinaryFile+0xb0>
movl   $0x1,-0x34(%rbp)
jmp    1265 <LoadBinaryFile+0xe5>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     1260 <LoadBinaryFile+0xe0>
movl   $0x1,-0x34(%rbp)
jmp    1265 <LoadBinaryFile+0xe5>
jmp    1265 <LoadBinaryFile+0xe5>
cmpq   $0x0,-0x20(%rbp)
je     1279 <LoadBinaryFile+0xf9>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x34(%rbp)
je     12a6 <LoadBinaryFile+0x126>
cmpq   $0x0,-0x30(%rbp)
je     129f <LoadBinaryFile+0x11f>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x0,-0x34(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LoadBinaryFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe68(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1212 <LoadBinaryFile+0x92>
mov    %rax,%rbx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
test   %eax,%eax
jle    1217 <LoadBinaryFile+0x97>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1217 <LoadBinaryFile+0x97>
mov    %rax,%r13
mov    $0x1,%r12d
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1050 <fclose@plt>
cmp    %ebp,0x4(%rsp)
je     1224 <LoadBinaryFile+0xa4>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    121f <LoadBinaryFile+0x9f>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebp,%ebp
xor    %r13d,%r13d
mov    %ebp,(%r15)
mov    %r13,(%r14)
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @LoadBinaryFile(i8* %0, i32* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8** %2, i8*** %6
store i8* null, i8** %10
store i32 0, i32* %11
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %7
%14 = load i32*, i32** %7
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 1, i32* %11
br label %44
17: ; preds = %3
%18 = load i32*, i32** %7
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %7
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 1, i32* %11
br label %44
26: ; preds = %17
%27 = load i32*, i32** %7
%28 = call i32 @rewind(i32* %27)
%29 = load i32, i32* %8
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %10
%31 = load i8*, i8** %10
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 1, i32* %11
br label %44
34: ; preds = %26
%35 = load i8*, i8** %10
%36 = load i32, i32* %8
%37 = load i32*, i32** %7
%38 = call i32 @fread(i8* %35, i32 1, i32 %36, i32* %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = load i32, i32* %8
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %34
store i32 1, i32* %11
br label %44
43: ; preds = %34
br label %44
44: ; preds = %43, %42, %33, %25, %16
%45 = load i32*, i32** %7
%46 = icmp ne i32* %45, null
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32*, i32** %7
%49 = call i32 @fclose(i32* %48)
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* %11
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %60
53: ; preds = %50
%54 = load i8*, i8** %10
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i8*, i8** %10
%58 = call i32 @free(i8* %57)
store i8* null, i8** %10
br label %59
59: ; preds = %56, %53
store i32 0, i32* %8
br label %60
60: ; preds = %59, %50
%61 = load i32, i32* %8
%62 = load i32*, i32** %5
store i32 %61, i32* %62
%63 = load i8*, i8** %10
%64 = load i8**, i8*** %6
store i8* %63, i8** %64
%65 = load i32, i32* %11
%66 = icmp ne i32 %65, 0
%67 = xor i1 %66, true
%68 = zext i1 %67 to i32
ret i32 %68
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32 @LoadBinaryFile(i8* nocapture %0, i32* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %4, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %4)
%10 = icmp slt i32 %9, 1
br i1 %10, label %15, label %11
11: ; preds = %6
%12 = tail call i32 @rewind(i32* %4)
%13 = tail call i8* @malloc(i32 %9)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %11, %6
%16 = tail call i32 @fclose(i32* %4)
br label %23
17: ; preds = %11
%18 = tail call i32 @fread(i8* %13, i32 1, i32 %9, i32* %4)
%19 = icmp eq i32 %18, %9
%20 = tail call i32 @fclose(i32* %4)
br i1 %19, label %23, label %21
21: ; preds = %17
%22 = tail call i32 @free(i8* %13)
br label %23
23: ; preds = %3, %15, %21, %17
%24 = phi i32 [ 1, %17 ], [ 0, %21 ], [ 0, %15 ], [ 0, %3 ]
%25 = phi i32 [ %9, %17 ], [ 0, %21 ], [ 0, %15 ], [ 0, %3 ]
%26 = phi i8* [ %13, %17 ], [ null, %21 ], [ null, %15 ], [ null, %3 ]
store i32 %25, i32* %1
store i8* %26, i8** %2
ret i32 %24
}