#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(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(size_t);
int loadfile(const char *file, char **out, size_t *outsize) {
  char *data;
  size_t len;
  long ftrv;
  FILE *f;
  f = fopen(file, "rb");
  if (!f)
    return -1;
  fseek(f, 0, SEEK_END);
  ftrv = ftell(f);
  if (ftrv < 0) {
    fclose(f);
    return -7;
  }
  len = (size_t)ftrv;
  fseek(f, 0, SEEK_SET);
  data = (char *)malloc(len);
  if (fread(data, 1, len, f) != len) {
    fclose(f);
    free(data);
    return -7;
  }
  fclose(f);
  *out = data;
  *outsize = len;
  return 0;
}


x86 assembly (clang, -O0):
<loadfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11af <loadfile+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <loadfile+0x10d>
mov    -0x40(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x40(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jge    11f0 <loadfile+0x80>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xfffffff9,-0x4(%rbp)
jmp    127d <loadfile+0x10d>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x30(%rbp),%rax
je     1257 <loadfile+0xe7>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffff9,-0x4(%rbp)
jmp    127d <loadfile+0x10d>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

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

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