#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*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(void *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
void *malloc(size_t);
int /*<<< orphan*/ printf(char *, char *);
void load_binary(char *path, void **data, size_t *len) {
  FILE *fp;
  printf("Loading file: %s\n", path);
  fp = fopen(path, "rb");
  if (fp == NULL) {
    printf("Error open file %s\n", path);
    exit(0);
  }
  fseek(fp, 0, SEEK_END);
  *len = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  *data = malloc(*len);
  fread(*data, 1, *len, fp);
  fclose(fp);
}


x86 assembly (clang, -O0):
<load_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe61(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe63(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11da <load_binary+0x5a>
mov    -0x8(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_binary>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe68(%rip),%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
lea    0xe6b(%rip),%rsi
mov    %r12,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     121c <load_binary+0x9c>
mov    %rax,%rbx
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %rax,(%r15)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    (%r15),%r15
mov    %r15,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
lea    0xdf2(%rip),%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Loading file: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [20 x i8] c"Error open file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define void @load_binary(i8* %0, i8** %1, i64* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i32*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i64* %2, i64** %6
%8 = load i8*, i8** %4
%9 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %3
%19 = load i32*, i32** %7
%20 = load i32, i32* @SEEK_END
%21 = call i32 @fseek(i32* %19, i32 0, i32 %20)
%22 = load i32*, i32** %7
%23 = call i64 @ftell(i32* %22)
%24 = load i64*, i64** %6
store i64 %23, i64* %24
%25 = load i32*, i32** %7
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i32 0, i32 %26)
%28 = load i64*, i64** %6
%29 = load i64, i64* %28
%30 = call i8* @malloc(i64 %29)
%31 = load i8**, i8*** %5
store i8* %30, i8** %31
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
%34 = load i64*, i64** %6
%35 = load i64, i64* %34
%36 = load i32*, i32** %7
%37 = call i32 @fread(i8* %33, i32 1, i64 %35, i32* %36)
%38 = load i32*, i32** %7
%39 = call i32 @fclose(i32* %38)
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"Loading file: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [20 x i8] c"Error open file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define void @load_binary(i8* %0, i8** nocapture writeonly %1, i64* nocapture %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %5, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %5)
store i64 %13, i64* %2
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %5, i32 0, i32 %14)
%16 = load i64, i64* %2
%17 = tail call i8* @malloc(i64 %16)
store i8* %17, i8** %1
%18 = tail call i32 @fread(i8* %17, i32 1, i64 %16, i32* %5)
%19 = tail call i32 @fclose(i32* %5)
ret void
}