#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
scalar_t__ ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int load_file(const char *path, unsigned char **buf, size_t *n) {
  FILE *f;
  if ((f = fopen(path, "rb")) == NULL)
    return (1);
  fseek(f, 0, SEEK_END);
  *n = (size_t)ftell(f);
  fseek(f, 0, SEEK_SET);
  if ((*buf = (unsigned char *)malloc(*n + 1)) == NULL)
    return (1);
  if (fread(*buf, 1, *n, f) != *n) {
    fclose(f);
    free(*buf);
    return (1);
  }
  fclose(f);
  (*buf)[*n] = '\0';
  return (0);
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ae <load_file+0x3e>
movl   $0x1,-0x4(%rbp)
jmp    1282 <load_file+0x112>
mov    -0x28(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1216 <load_file+0xa6>
movl   $0x1,-0x4(%rbp)
jmp    1282 <load_file+0x112>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
je     1260 <load_file+0xf0>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    1282 <load_file+0x112>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     1224 <load_file+0xb4>
mov    %rax,%rbp
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbp,%rdi
call   1060 <ftell@plt>
mov    %rax,(%rbx)
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    (%rbx),%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1224 <load_file+0xb4>
mov    (%rbx),%rdx
mov    $0x1,%r15d
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbp,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
mov    (%rbx),%r13
mov    %rbp,%rdi
call   1050 <fclose@plt>
mov    (%r14),%rdi
cmp    %r13,%r12
jne    121f <load_file+0xaf>
mov    (%rbx),%rax
movb   $0x0,(%rdi,%rax,1)
xor    %r15d,%r15d
jmp    1224 <load_file+0xb4>
call   1030 <free@plt>
mov    %r15d,%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
@SEEK_SET = global i32 0
define i32 @load_file(i8* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 1, i32* %4
br label %55
13: ; preds = %3
%14 = load i32*, i32** %8
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i32 0, i32 %15)
%17 = load i32*, i32** %8
%18 = call i64 @ftell(i32* %17)
%19 = load i64*, i64** %7
store i64 %18, i64* %19
%20 = load i32*, i32** %8
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
%23 = load i64*, i64** %7
%24 = load i64, i64* %23
%25 = add i64 %24, 1
%26 = call i64 @malloc(i64 %25)
%27 = inttoptr i64 %26 to i8*
%28 = load i8**, i8*** %6
store i8* %27, i8** %28
%29 = icmp eq i8* %27, null
br i1 %29, label %30, label %31
30: ; preds = %13
store i32 1, i32* %4
br label %55
31: ; preds = %13
%32 = load i8**, i8*** %6
%33 = load i8*, i8** %32
%34 = load i64*, i64** %7
%35 = load i64, i64* %34
%36 = load i32*, i32** %8
%37 = call i64 @fread(i8* %33, i32 1, i64 %35, i32* %36)
%38 = load i64*, i64** %7
%39 = load i64, i64* %38
%40 = icmp ne i64 %37, %39
br i1 %40, label %41, label %47
41: ; preds = %31
%42 = load i32*, i32** %8
%43 = call i32 @fclose(i32* %42)
%44 = load i8**, i8*** %6
%45 = load i8*, i8** %44
%46 = call i32 @free(i8* %45)
store i32 1, i32* %4
br label %55
47: ; preds = %31
%48 = load i32*, i32** %8
%49 = call i32 @fclose(i32* %48)
%50 = load i8**, i8*** %6
%51 = load i8*, i8** %50
%52 = load i64*, i64** %7
%53 = load i64, i64* %52
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
store i32 0, i32* %4
br label %55
55: ; preds = %47, %41, %30, %12
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i32 @load_file(i8* nocapture %0, i8** nocapture %1, i64* nocapture %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 %29, 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 i64 @ftell(i32* %4)
store i64 %9, i64* %2
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @fseek(i32* %4, i32 0, i32 %10)
%12 = load i64, i64* %2
%13 = add i64 %12, 1
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %1
%16 = icmp eq i64 %14, 0
br i1 %16, label %29, label %17
17: ; preds = %6
%18 = load i64, i64* %2
%19 = tail call i64 @fread(i8* %15, i32 1, i64 %18, i32* %4)
%20 = load i64, i64* %2
%21 = icmp eq i64 %19, %20
%22 = tail call i32 @fclose(i32* %4)
%23 = load i8*, i8** %1
br i1 %21, label %26, label %24
24: ; preds = %17
%25 = tail call i32 @free(i8* %23)
br label %29
26: ; preds = %17
%27 = load i64, i64* %2
%28 = getelementptr inbounds i8, i8* %23, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %6, %3, %26, %24
%30 = phi i32 [ 1, %24 ], [ 0, %26 ], [ 1, %3 ], [ 1, %6 ]
ret i32 %30
}