#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 /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
long load_file(const char *file_path, char **buffer) {
  FILE *file;
  size_t result;
  long file_size;
  file_size = 0;
  if ((file = fopen(file_path, "rb")) != NULL) {
    fseek(file, 0, SEEK_END);
    file_size = ftell(file);
    rewind(file);
    if ((*buffer = (char *)malloc(sizeof(char) * file_size)) == NULL)
      fputs("malloc() error allocating memory", stderr);
    if ((result = fread(*buffer, sizeof(char), file_size, file)) != file_size)
      fputs("fread() error", stderr);
    fclose(file);
  }
  return file_size;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     125a <load_file+0xda>
mov    -0x18(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x28(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1215 <load_file+0x95>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,-0x20(%rbp)
cmp    -0x28(%rbp),%rax
je     1251 <load_file+0xd1>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
lea    0xe71(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1225 <load_file+0xa5>
mov    %rax,%rbx
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r14d,%edi
call   1080 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
jne    11ee <load_file+0x6e>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
call   1060 <fputs@plt>
mov    (%r15),%rax
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r14,%rax
je     121b <load_file+0x9b>
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
lea    0xe0e(%rip),%rdi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    1228 <load_file+0xa8>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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