#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 {
  size_t st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ oom();
scalar_t__ stat(char const *, struct stat *);
char *load_file(const char *fname, int *sizeptr) {
  FILE *f;
  struct stat fs;
  size_t size;
  char *data;
  if (stat(fname, &fs) != 0)
    return NULL;
  size = fs.st_size;
  if (sizeptr)
    *sizeptr = size;
  data = malloc(size + 1);
  if (!data)
    oom();
  memset(data, 0, size + 1);
  f = fopen(fname, "rb");
  if (!f) {
    free(data);
    return NULL;
  }
  if (fread(data, size, 1, f) < 1) {
    free(data);
    return NULL;
  }
  fclose(f);
  return data;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     11b4 <load_file+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1284 <load_file+0x104>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d3 <load_file+0x53>
mov    -0x30(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f6 <load_file+0x76>
mov    $0x0,%al
call   1040 <oom@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    123e <load_file+0xbe>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1284 <load_file+0x104>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
jge    1273 <load_file+0xf3>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1284 <load_file+0x104>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
jne    120f <load_file+0x8f>
mov    (%rsp),%r14
test   %rbx,%rbx
je     11a7 <load_file+0x27>
mov    %r14d,(%rbx)
lea    0x1(%r14),%r12
mov    %r12,%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11c2 <load_file+0x42>
xor    %eax,%eax
call   1040 <oom@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1070 <memset@plt>
lea    0xe2a(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1207 <load_file+0x87>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
test   %eax,%eax
jle    1207 <load_file+0x87>
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    1211 <load_file+0x91>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
define i8* @load_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.stat
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%10 = load i8*, i8** %4
%11 = call i64 @stat(i8* %10, %struct.stat* %7)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %56
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%16 = load i64, i64* %15
store i64 %16, i64* %8
%17 = load i32*, i32** %5
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i64, i64* %8
%21 = trunc i64 %20 to i32
%22 = load i32*, i32** %5
store i32 %21, i32* %22
br label %23
23: ; preds = %19, %14
%24 = load i64, i64* %8
%25 = add i64 %24, 1
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %9
%27 = load i8*, i8** %9
%28 = icmp ne i8* %27, null
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = call i32 (...) @oom()
br label %31
31: ; preds = %29, %23
%32 = load i8*, i8** %9
%33 = load i64, i64* %8
%34 = add i64 %33, 1
%35 = call i32 @memset(i8* %32, i32 0, i64 %34)
%36 = load i8*, i8** %4
%37 = call i32* @fopen(i8* %36, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %37, i32** %6
%38 = load i32*, i32** %6
%39 = icmp ne i32* %38, null
br i1 %39, label %43, label %40
40: ; preds = %31
%41 = load i8*, i8** %9
%42 = call i32 @free(i8* %41)
store i8* null, i8** %3
br label %56
43: ; preds = %31
%44 = load i8*, i8** %9
%45 = load i64, i64* %8
%46 = load i32*, i32** %6
%47 = call i32 @fread(i8* %44, i64 %45, i32 1, i32* %46)
%48 = icmp slt i32 %47, 1
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load i8*, i8** %9
%51 = call i32 @free(i8* %50)
store i8* null, i8** %3
br label %56
52: ; preds = %43
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %9
store i8* %55, i8** %3
br label %56
56: ; preds = %52, %49, %40, %13
%57 = load i8*, i8** %3
ret i8* %57
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [3 x i8] c"rb\00"
define i8* @load_file(i8* %0, i32* writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %32
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i64, i64* %8
%10 = icmp eq i32* %1, null
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = trunc i64 %9 to i32
store i32 %12, i32* %1
br label %13
13: ; preds = %11, %7
%14 = add i64 %9, 1
%15 = call i8* @malloc(i64 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 (...) @oom()
br label %19
19: ; preds = %17, %13
%20 = call i32 @memset(i8* %15, i32 0, i64 %14)
%21 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @free(i8* %15)
br label %32
25: ; preds = %19
%26 = call i32 @fread(i8* %15, i64 %9, i32 1, i32* %21)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @free(i8* %15)
br label %32
30: ; preds = %25
%31 = call i32 @fclose(i32* %21)
br label %32
32: ; preds = %2, %30, %28, %23
%33 = phi i8* [ null, %28 ], [ %15, %30 ], [ null, %23 ], [ null, %2 ]
ret i8* %33
}