#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long loadFile(char **buf, const char *fileName) {
  FILE *fp = NULL;
  long size = 0;
  if ((fp = fopen(fileName, "rb")) == NULL)
    return -1;
  fseek(fp, 0, SEEK_END);
  size = ftell(fp);
  rewind(fp);
  *buf = (char *)malloc(size + 1);
  (*buf)[size] = '\0';
  if (fread(*buf, size, 1, fp) < 1) {
    free(*buf);
    size = -1;
  }
  fclose(fp);
  return size;
}


x86 assembly (clang, -O0):
<loadFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11cb <loadFile+0x4b>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1262 <loadFile+0xe2>
mov    -0x20(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
jge    1251 <loadFile+0xd1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movq   $0xffffffffffffffff,-0x28(%rbp)
ff
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadFile>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
mov    %rax,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1209 <loadFile+0x89>
mov    %rax,%rbx
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <rewind@plt>
lea    0x1(%r15),%rdi
call   1080 <malloc@plt>
mov    %rax,(%r14)
movb   $0x0,(%rax,%r15,1)
mov    (%r14),%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jg     11ff <loadFile+0x7f>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%r15
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1210 <loadFile+0x90>
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i64 @loadFile(i8** nocapture %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %23, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = tail call i64 @ftell(i32* %3)
%9 = tail call i32 @rewind(i32* %3)
%10 = add nsw i64 %8, 1
%11 = tail call i64 @malloc(i64 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %0
%13 = getelementptr inbounds i8, i8* %12, i64 %8
store i8 0, i8* %13
%14 = load i8*, i8** %0
%15 = tail call i32 @fread(i8* %14, i64 %8, i32 1, i32* %3)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %5
%18 = load i8*, i8** %0
%19 = tail call i32 @free(i8* %18)
br label %20
20: ; preds = %17, %5
%21 = phi i64 [ -1, %17 ], [ %8, %5 ]
%22 = tail call i32 @fclose(i32* %3)
br label %23
23: ; preds = %2, %20
%24 = phi i64 [ %21, %20 ], [ -1, %2 ]
ret i64 %24
}