#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 */
struct stat {
  unsigned int st_size;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int stat(char const *, struct stat *);
int file_load(const char *filename, char **image, unsigned int *image_size) {
  struct stat stbuf = {0};
  int fd = -1;
  char *mem = NULL;
  if (!filename || !image || !image_size) {
    printf("file_load:: null param\n");
    return 0;
  }
  if (stat(filename, &stbuf) == -1) {
    printf("file_load:: error stat'ing file [%s]\n", filename);
    return 0;
  }
  *image_size = stbuf.st_size;
  mem = (char *)malloc(stbuf.st_size + 1);
  if (mem == NULL) {
    printf("file_load:: error allocating memory\n");
    return 0;
  }
  fd = open(filename, O_RDONLY);
  if (fd == -1) {
    printf("file_load:: error loading file\n");
    free((void *)mem);
    return 0;
  }
  if (read(fd, mem, stbuf.st_size) != stbuf.st_size) {
    printf("file_load:: error reading file [%s]\n", filename);
    free((void *)mem);
    close(fd);
    return 0;
  }
  mem[stbuf.st_size] = '\0';
  *image = mem;
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<file_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0xffffffff,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d4 <file_load+0x54>
cmpq   $0x0,-0x18(%rbp)
je     11d4 <file_load+0x54>
cmpq   $0x0,-0x20(%rbp)
jne    11ee <file_load+0x6e>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130d <file_load+0x18d>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    1222 <file_load+0xa2>
mov    -0x10(%rbp),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130d <file_load+0x18d>
mov    -0x28(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    125f <file_load+0xdf>
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    130d <file_load+0x18d>
mov    -0x10(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12a1 <file_load+0x121>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    130d <file_load+0x18d>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1070 <read@plt>
cmp    -0x28(%rbp),%eax
je     12e8 <file_load+0x168>
mov    -0x10(%rbp),%rsi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    130d <file_load+0x18d>
mov    -0x38(%rbp),%rax
mov    -0x28(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_load>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movq   $0x0,(%rsp)
test   %rdi,%rdi
je     1214 <file_load+0x94>
mov    %rsi,%r14
test   %rsi,%rsi
je     1214 <file_load+0x94>
mov    %rdx,%rbp
test   %rdx,%rdx
je     1214 <file_load+0x94>
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     1224 <file_load+0xa4>
mov    (%rsp),%r12d
mov    %r12d,0x0(%rbp)
lea    0x1(%r12),%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1239 <file_load+0xb9>
mov    %rax,%r15
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1242 <file_load+0xc2>
mov    %eax,%r13d
mov    %eax,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1070 <read@plt>
cmp    %r12d,%eax
jne    125a <file_load+0xda>
movb   $0x0,(%r15,%r12,1)
mov    %r15,(%r14)
mov    %r13d,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
jmp    127d <file_load+0xfd>
lea    0xe30(%rip),%rdi
call   1040 <puts@plt>
xor    %ebp,%ebp
jmp    127d <file_load+0xfd>
lea    0xdd5(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    127d <file_load+0xfd>
lea    0xe41(%rip),%rdi
jmp    121b <file_load+0x9b>
lea    0xe19(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %ebp,%ebp
jmp    127d <file_load+0xfd>
lea    0xdc5(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [24 x i8] c"file_load:: null param\0A\00"
@.str.1 = [38 x i8] c"file_load:: error stat'ing file [%s]\0A\00"
@.str.2 = [37 x i8] c"file_load:: error allocating memory\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [32 x i8] c"file_load:: error loading file\0A\00"
@.str.4 = [37 x i8] c"file_load:: error reading file [%s]\0A\00"
define i32 @file_load(i8* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
%11 = bitcast %struct.stat* %8 to i8*
store i32 -1, i32* %9
store i8* null, i8** %10
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i8**, i8*** %6
%16 = icmp ne i8** %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32*, i32** %7
%19 = icmp ne i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %17, %14, %3
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %78
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = call i32 @stat(i8* %23, %struct.stat* %8)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 0, i32* %4
br label %78
29: ; preds = %22
%30 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i32*, i32** %7
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = add i32 %34, 1
%36 = call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %10
%38 = load i8*, i8** %10
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %29
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %78
42: ; preds = %29
%43 = load i8*, i8** %5
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%50 = load i8*, i8** %10
%51 = call i32 @free(i8* %50)
store i32 0, i32* %4
br label %78
52: ; preds = %42
%53 = load i32, i32* %9
%54 = load i8*, i8** %10
%55 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @read(i32 %53, i8* %54, i32 %56)
%58 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%59 = load i32, i32* %58
%60 = icmp ne i32 %57, %59
br i1 %60, label %61, label %68
61: ; preds = %52
%62 = load i8*, i8** %5
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %62)
%64 = load i8*, i8** %10
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* %9
%67 = call i32 @close(i32 %66)
store i32 0, i32* %4
br label %78
68: ; preds = %52
%69 = load i8*, i8** %10
%70 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%71 = load i32, i32* %70
%72 = zext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %69, i64 %72
store i8 0, i8* %73
%74 = load i8*, i8** %10
%75 = load i8**, i8*** %6
store i8* %74, i8** %75
%76 = load i32, i32* %9
%77 = call i32 @close(i32 %76)
store i32 1, i32* %4
br label %78
78: ; preds = %68, %61, %48, %40, %26, %20
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str.1 = [38 x i8] c"file_load:: error stat'ing file [%s]\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [37 x i8] c"file_load:: error reading file [%s]\0A\00"
@str = [23 x i8] c"file_load:: null param\00"
@str.5 = [31 x i8] c"file_load:: error loading file\00"
@str.6 = [36 x i8] c"file_load:: error allocating memory\00"
define i32 @file_load(i8* %0, i8** writeonly %1, i32* writeonly %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%6 = icmp ne i8* %0, null
%7 = icmp ne i8** %1, null
%8 = and i1 %6, %7
%9 = icmp ne i32* %2, null
%10 = and i1 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %46
13: ; preds = %3
%14 = bitcast i64* %4 to %struct.stat*
%15 = call i32 @stat(i8* %0, %struct.stat* %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %46
19: ; preds = %13
%20 = bitcast i64* %4 to i32*
%21 = load i32, i32* %20
store i32 %21, i32* %2
%22 = add i32 %21, 1
%23 = tail call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
%25 = icmp eq i64 %23, 0
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.6, i64 0, i64 0))
br label %46
28: ; preds = %19
%29 = load i32, i32* @O_RDONLY
%30 = tail call i32 @open(i8* %0, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
%34 = tail call i32 @free(i8* %24)
br label %46
35: ; preds = %28
%36 = tail call i32 @read(i32 %30, i8* %24, i32 %21)
%37 = icmp eq i32 %36, %21
br i1 %37, label %42, label %38
38: ; preds = %35
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%40 = tail call i32 @free(i8* %24)
%41 = tail call i32 @close(i32 %30)
br label %46
42: ; preds = %35
%43 = zext i32 %21 to i64
%44 = getelementptr inbounds i8, i8* %24, i64 %43
store i8 0, i8* %44
store i8* %24, i8** %1
%45 = tail call i32 @close(i32 %30)
br label %46
46: ; preds = %42, %38, %32, %26, %17, %11
%47 = phi i32 [ 0, %17 ], [ 0, %26 ], [ 0, %32 ], [ 0, %38 ], [ 1, %42 ], [ 0, %11 ]
ret i32 %47
}