#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 *, char *);
int fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
void *malloc(unsigned int);
unsigned load_file(char *fd_stream, char **contents, size_t *contents_size) {
  FILE *stream;
  unsigned int fileSize = 0;
  void *p;
  stream = fopen(fd_stream, "rb");
  fseek(stream, 0L, SEEK_END);
  fileSize = ftell(stream);
  fseek(stream, 0L, SEEK_SET);
  p = malloc(fileSize + 1);
  if (NULL == p)
    return 0x00;
  (*contents) = (char *)p;
  *contents_size = fread((*contents), 1, fileSize, stream) + 1;
  (*contents)[*contents_size] = 0;
  fclose(stream);
  return 0x1;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fseek@plt>
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jne    11ee <load_file+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    1240 <load_file+0xe0>
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
add    $0x1,%eax
movslq %eax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xe8a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
mov    %eax,%r13d
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0x1(%r13),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f5 <load_file+0x95>
mov    %rax,(%r15)
mov    $0x1,%r12d
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cltq
lea    0x1(%rax),%rcx
mov    %rcx,(%r14)
mov    (%r15),%rcx
movb   $0x0,0x1(%rcx,%rax,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r12d,%eax
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
@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*
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
store i32 0, i32* %9
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = load i32, i32* @SEEK_END
%15 = call i32 @fseek(i32* %13, i64 0, i32 %14)
%16 = load i32*, i32** %8
%17 = call i32 @ftell(i32* %16)
store i32 %17, i32* %9
%18 = load i32*, i32** %8
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
%21 = load i32, i32* %9
%22 = add i32 %21, 1
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = icmp eq i8* null, %24
br i1 %25, label %26, label %27
26: ; preds = %3
store i32 0, i32* %4
br label %45
27: ; preds = %3
%28 = load i8*, i8** %10
%29 = load i8**, i8*** %6
store i8* %28, i8** %29
%30 = load i8**, i8*** %6
%31 = load i8*, i8** %30
%32 = load i32, i32* %9
%33 = load i32*, i32** %8
%34 = call i32 @fread(i8* %31, i32 1, i32 %32, i32* %33)
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = load i64*, i64** %7
store i64 %36, i64* %37
%38 = load i8**, i8*** %6
%39 = load i8*, i8** %38
%40 = load i64*, i64** %7
%41 = load i64, i64* %40
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 0, i8* %42
%43 = load i32*, i32** %8
%44 = call i32 @fclose(i32* %43)
store i32 1, i32* %4
br label %45
45: ; preds = %27, %26
%46 = load i32, i32* %4
ret i32 %46
}

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 writeonly %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32* %4, i64 0, i32 %5)
%7 = tail call i32 @ftell(i32* %4)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %4, i64 0, i32 %8)
%10 = add i32 %7, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %20, label %13
13: ; preds = %3
store i8* %11, i8** %1
%14 = tail call i32 @fread(i8* %11, i32 1, i32 %7, i32* %4)
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
store i64 %16, i64* %2
%17 = load i8*, i8** %1
%18 = getelementptr inbounds i8, i8* %17, i64 %16
store i8 0, i8* %18
%19 = tail call i32 @fclose(i32* %4)
br label %20
20: ; preds = %3, %13
%21 = phi i32 [ 1, %13 ], [ 0, %3 ]
ret i32 %21
}