#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ lg(char *, ...);
void load_bin(char *path, void *data, int len) {
  FILE *fp;
  lg("Loading %s\n", path);
  fp = fopen(path, "rb");
  if (!fp) {
    lg("Cant open file\n");
    exit(1);
  }
  if (len != fread(data, 1, len, fp)) {
    lg("Failed to read all data\n");
    exit(1);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<load_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1050 <lg@plt>
mov    -0x8(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <load_bin+0x5c>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1050 <lg@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
je     11ec <load_bin+0x9c>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1050 <lg@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_bin>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <lg@plt>
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a6 <load_bin+0x56>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
cmp    %ebp,%eax
jne    11af <load_bin+0x5f>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe62(%rip),%rdi
jmp    11b6 <load_bin+0x66>
lea    0xe69(%rip),%rdi
xor    %eax,%eax
call   1050 <lg@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Loading %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [16 x i8] c"Cant open file\0A\00"
@.str.3 = [25 x i8] c"Failed to read all data\0A\00"
define void @load_bin(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([12 x i8], [12 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 ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i32, i32* %6
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = load i32*, i32** %7
%22 = call i32 @fread(i8* %19, i32 1, i32 %20, i32* %21)
%23 = icmp ne i32 %18, %22
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = load i32*, i32** %7
%29 = call i32 @fclose(i32* %28)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"Loading %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [16 x i8] c"Cant open file\0A\00"
@.str.3 = [25 x i8] c"Failed to read all data\0A\00"
define void @load_bin(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([12 x i8], [12 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 (i8*, ...) @lg(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = tail call i32 @fread(i8* %1, i32 1, i32 %2, i32* %5)
%12 = icmp eq i32 %11, %2
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @lg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i32 @fclose(i32* %5)
ret void
}