#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*/ error(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
unsigned char *load_file(const char *path, long *size_out) {
  size_t size;
  unsigned char *data;
  FILE *in = fopen(path, "rb");
  if (!in)
    error("Couldn't open file");
  fseek(in, 0, SEEK_END);
  size = ftell(in);
  if (size_out)
    *size_out = size;
  rewind(in);
  data = (unsigned char *)malloc(size);
  if (!data)
    error("Out of memory");
  if (fread(data, 1, size, in) < size)
    error("Couldn't read file");
  fclose(in);
  return data;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bb <load_file+0x3b>
lea    0xe4d(%rip),%rdi
call   1090 <error@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f2 <load_file+0x72>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121f <load_file+0x9f>
lea    0xdfc(%rip),%rdi
call   1090 <error@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x18(%rbp),%rax
jae    124b <load_file+0xcb>
lea    0xdde(%rip),%rdi
call   1090 <error@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe71(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a8 <load_file+0x28>
lea    0xe60(%rip),%rdi
call   1090 <error@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r15,%rdi
call   1060 <ftell@plt>
mov    %rax,%rbx
test   %r14,%r14
je     11ce <load_file+0x4e>
mov    %rbx,(%r14)
mov    %r15,%rdi
call   1050 <rewind@plt>
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11f2 <load_file+0x72>
lea    0xe29(%rip),%rdi
call   1090 <error@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1030 <fread@plt>
cmp    %rbx,%rax
jae    1216 <load_file+0x96>
lea    0xe13(%rip),%rdi
call   1090 <error@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Couldn't open file\00"
@SEEK_END = global i32 0
@.str.2 = [14 x i8] c"Out of memory\00"
@.str.3 = [19 x i8] c"Couldn't read file\00"
define i8* @load_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %3
store i64* %1, i64** %4
%8 = load i8*, i8** %3
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32*, i32** %7
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %7
%19 = call i64 @ftell(i32* %18)
store i64 %19, i64* %5
%20 = load i64*, i64** %4
%21 = icmp ne i64* %20, null
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i64, i64* %5
%24 = load i64*, i64** %4
store i64 %23, i64* %24
br label %25
25: ; preds = %22, %14
%26 = load i32*, i32** %7
%27 = call i32 @rewind(i32* %26)
%28 = load i64, i64* %5
%29 = call i64 @malloc(i64 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = icmp ne i8* %31, null
br i1 %32, label %35, label %33
33: ; preds = %25
%34 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %25
%36 = load i8*, i8** %6
%37 = load i64, i64* %5
%38 = load i32*, i32** %7
%39 = call i64 @fread(i8* %36, i32 1, i64 %37, i32* %38)
%40 = load i64, i64* %5
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %35
%43 = call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %35
%45 = load i32*, i32** %7
%46 = call i32 @fclose(i32* %45)
%47 = load i8*, i8** %6
ret i8* %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Couldn't open file\00"
@SEEK_END = global i32 0
@.str.2 = [14 x i8] c"Out of memory\00"
@.str.3 = [19 x i8] c"Couldn't read file\00"
define i8* @load_file(i8* nocapture %0, i64* writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, 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 %7
5: ; preds = %2
%6 = tail call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i32 0, i32 %8)
%10 = tail call i64 @ftell(i32* %3)
%11 = icmp eq i64* %1, null
br i1 %11, label %13, label %12
12: ; preds = %7
store i64 %10, i64* %1
br label %13
13: ; preds = %12, %7
%14 = tail call i32 @rewind(i32* %3)
%15 = tail call i64 @malloc(i64 %10)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i64 %15, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
%21 = tail call i64 @fread(i8* %16, i32 1, i64 %10, i32* %3)
%22 = icmp ult i64 %21, %10
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @error(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = tail call i32 @fclose(i32* %3)
ret i8* %16
}