#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 *, char *);
unsigned long fread(unsigned char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
unsigned char *malloc(long);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long loadfile2mem(char *file, unsigned char **memptr) {
  long filesize;
  FILE *fd;
  *memptr = NULL;
  fd = fopen(file, "rb");
  if (fd == NULL)
    return (-1);
  fseek(fd, 0, SEEK_END);
  filesize = ftell(fd);
  if (filesize <= 0)
    return (-1);
  rewind(fd);
  *memptr = malloc(filesize + 1);
  if (*memptr == NULL)
    return (-1);
  (*memptr)[filesize] = 0;
  if (fread(*memptr, 1, filesize, fd) != (unsigned long)filesize) {
    fclose(fd);
    free(*memptr);
    *memptr = NULL;
    return (-1);
  }
  fclose(fd);
  return (filesize);
}


x86 assembly (clang, -O0):
<loadfile2mem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c7 <loadfile2mem+0x47>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ab <loadfile2mem+0x12b>
mov    -0x28(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jg     1200 <loadfile2mem+0x80>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ab <loadfile2mem+0x12b>
mov    -0x28(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    123b <loadfile2mem+0xbb>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ab <loadfile2mem+0x12b>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x20(%rbp),%rax
je     129a <loadfile2mem+0x11a>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ab <loadfile2mem+0x12b>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadfile2mem>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
movq   $0x0,(%rsi)
lea    0xe66(%rip),%rsi
call   10a0 <fopen@plt>
mov    $0xffffffffffffffff,%r15
test   %rax,%rax
je     1230 <loadfile2mem+0xb0>
mov    %rax,%rbx
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
test   %rax,%rax
jle    1230 <loadfile2mem+0xb0>
mov    %rax,%r12
mov    %rbx,%rdi
call   1060 <rewind@plt>
lea    0x1(%r12),%rdi
call   1080 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1230 <loadfile2mem+0xb0>
movb   $0x0,(%rax,%r12,1)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%r15
cmp    %r12,%r13
je     1230 <loadfile2mem+0xb0>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    $0xffffffffffffffff,%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i64 @loadfile2mem(i8* %0, i8** %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
store i8* null, i8** %8
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i64 -1, i64* %3
br label %57
14: ; preds = %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* %6
%20 = load i64, i64* %6
%21 = icmp sle i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %14
store i64 -1, i64* %3
br label %57
23: ; preds = %14
%24 = load i32*, i32** %7
%25 = call i32 @rewind(i32* %24)
%26 = load i64, i64* %6
%27 = add nsw i64 %26, 1
%28 = call i8* @malloc(i64 %27)
%29 = load i8**, i8*** %5
store i8* %28, i8** %29
%30 = load i8**, i8*** %5
%31 = load i8*, i8** %30
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %23
store i64 -1, i64* %3
br label %57
34: ; preds = %23
%35 = load i8**, i8*** %5
%36 = load i8*, i8** %35
%37 = load i64, i64* %6
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8 0, i8* %38
%39 = load i8**, i8*** %5
%40 = load i8*, i8** %39
%41 = load i64, i64* %6
%42 = load i32*, i32** %7
%43 = call i64 @fread(i8* %40, i32 1, i64 %41, i32* %42)
%44 = load i64, i64* %6
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %53
46: ; preds = %34
%47 = load i32*, i32** %7
%48 = call i32 @fclose(i32* %47)
%49 = load i8**, i8*** %5
%50 = load i8*, i8** %49
%51 = call i32 @free(i8* %50)
%52 = load i8**, i8*** %5
store i8* null, i8** %52
store i64 -1, i64* %3
br label %57
53: ; preds = %34
%54 = load i32*, i32** %7
%55 = call i32 @fclose(i32* %54)
%56 = load i64, i64* %6
store i64 %56, i64* %3
br label %57
57: ; preds = %53, %46, %33, %22, %13
%58 = load i64, i64* %3
ret i64 %58
}

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