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


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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),%eax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
jne    11fa <load_file+0x9a>
movl   $0x1,-0x4(%rbp)
jmp    122c <load_file+0xcc>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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,%r12
lea    0xe8a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
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    0x0(,%r13,8),%edi
call   1060 <malloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
je     11e8 <load_file+0x88>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rax,(%r14)
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11ee <load_file+0x8e>
mov    $0x1,%r15d
mov    %r15d,%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
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i64 0, i32 %13)
%15 = load i32*, i32** %8
%16 = call i32 @ftell(i32* %15)
store i32 %16, i32* %9
%17 = load i32*, i32** %8
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %17, i64 0, i32 %18)
%20 = load i32, i32* %9
%21 = zext i32 %20 to i64
%22 = mul i64 %21, 8
%23 = trunc i64 %22 to i32
%24 = call i8* @malloc(i32 %23)
%25 = load i8**, i8*** %6
store i8* %24, i8** %25
%26 = load i8**, i8*** %6
%27 = load i8*, i8** %26
%28 = icmp eq i8* null, %27
br i1 %28, label %29, label %30
29: ; preds = %3
store i32 1, i32* %4
br label %39
30: ; preds = %3
%31 = load i8**, i8*** %6
%32 = load i8*, i8** %31
%33 = load i32, i32* %9
%34 = load i32*, i32** %8
%35 = call i64 @fread(i8* %32, i32 1, i32 %33, i32* %34)
%36 = load i64*, i64** %7
store i64 %35, i64* %36
%37 = load i32*, i32** %8
%38 = call i32 @fclose(i32* %37)
store i32 0, i32* %4
br label %39
39: ; preds = %30, %29
%40 = load i32, i32* %4
ret i32 %40
}

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 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 = 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 = shl i32 %7, 3
%11 = tail call i8* @malloc(i32 %10)
store i8* %11, i8** %1
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = tail call i64 @fread(i8* %11, i32 1, i32 %7, i32* %4)
store i64 %14, i64* %2
%15 = tail call i32 @fclose(i32* %4)
br label %16
16: ; preds = %3, %13
%17 = phi i32 [ 0, %13 ], [ 1, %3 ]
ret i32 %17
}