#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 fread(void *, unsigned int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
void *malloc(unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void *load_file(const char *filename) {
  unsigned int size;
  void *buffer = NULL;
  FILE *datafile = fopen(filename, "rb");
  if (datafile == NULL) {
    printf("ERROR: %s couldn't be opened!\n", filename);
    return NULL;
  }
  fseek(datafile, 0, SEEK_END);
  size = ftell(datafile);
  if (size == 0) {
    printf("ERROR: Size of %s is 0!\n", filename);
    fclose(datafile);
    return NULL;
  }
  rewind(datafile);
  buffer = malloc(size);
  if (buffer == NULL) {
    printf("ERROR: Not enought memory to load %s!\n", filename);
    fclose(datafile);
    return NULL;
  }
  if (fread(buffer, size, 1, datafile) != 1) {
    printf("ERROR: Error while reading.\n");
    fclose(datafile);
    free(buffer);
    return NULL;
  }
  fclose(datafile);
  return buffer;
}


x86 assembly (clang, -O0):
<load_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e2 <load_file+0x52>
mov    -0x10(%rbp),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <load_file+0x148>
mov    -0x28(%rbp),%rdi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1234 <load_file+0xa4>
mov    -0x10(%rbp),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <load_file+0x148>
mov    -0x28(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x14(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    127c <load_file+0xec>
mov    -0x10(%rbp),%rsi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <load_file+0x148>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     12c7 <load_file+0x137>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d8 <load_file+0x148>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
lea    0xe4f(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     122e <load_file+0x8e>
mov    %rax,%rbx
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1090 <ftell@plt>
test   %eax,%eax
je     1244 <load_file+0xa4>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <rewind@plt>
mov    %ebp,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     124d <load_file+0xad>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     1261 <load_file+0xc1>
lea    0xe4e(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    1269 <load_file+0xc9>
lea    0xdce(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1269 <load_file+0xc9>
lea    0xdd7(%rip),%rdi
jmp    1254 <load_file+0xb4>
lea    0xde7(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"ERROR: %s couldn't be opened!\0A\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"ERROR: Size of %s is 0!\0A\00"
@.str.3 = [39 x i8] c"ERROR: Not enought memory to load %s!\0A\00"
@.str.4 = [29 x i8] c"ERROR: Error while reading.\0A\00"
define i8* @load_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %3
store i8* null, i8** %5
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = load i32*, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i8* null, i8** %2
br label %55
14: ; preds = %1
%15 = load i32*, i32** %6
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i32 0, i32 %16)
%18 = load i32*, i32** %6
%19 = call i32 @ftell(i32* %18)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %14
%23 = load i8*, i8** %3
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
store i8* null, i8** %2
br label %55
27: ; preds = %14
%28 = load i32*, i32** %6
%29 = call i32 @rewind(i32* %28)
%30 = load i32, i32* %4
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %39
34: ; preds = %27
%35 = load i8*, i8** %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
store i8* null, i8** %2
br label %55
39: ; preds = %27
%40 = load i8*, i8** %5
%41 = load i32, i32* %4
%42 = load i32*, i32** %6
%43 = call i32 @fread(i8* %40, i32 %41, i32 1, i32* %42)
%44 = icmp ne i32 %43, 1
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = load i8*, i8** %5
%50 = call i32 @free(i8* %49)
store i8* null, i8** %2
br label %55
51: ; preds = %39
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = load i8*, i8** %5
store i8* %54, i8** %2
br label %55
55: ; preds = %51, %45, %34, %22, %11
%56 = load i8*, i8** %2
ret i8* %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"ERROR: %s couldn't be opened!\0A\00"
@SEEK_END = global i32 0
@.str.2 = [25 x i8] c"ERROR: Size of %s is 0!\0A\00"
@.str.3 = [39 x i8] c"ERROR: Not enought memory to load %s!\0A\00"
@str = [28 x i8] c"ERROR: Error while reading.\00"
define noalias i8* @load_file(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %30
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i32 0, i32 %7)
%9 = tail call i32 @ftell(i32* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%13 = tail call i32 @fclose(i32* %2)
br label %30
14: ; preds = %6
%15 = tail call i32 @rewind(i32* %2)
%16 = tail call i8* @malloc(i32 %9)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%20 = tail call i32 @fclose(i32* %2)
br label %30
21: ; preds = %14
%22 = tail call i32 @fread(i8* %16, i32 %9, i32 1, i32* %2)
%23 = icmp eq i32 %22, 1
br i1 %23, label %28, label %24
24: ; preds = %21
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @fclose(i32* %2)
%27 = tail call i32 @free(i8* %16)
br label %30
28: ; preds = %21
%29 = tail call i32 @fclose(i32* %2)
br label %30
30: ; preds = %28, %24, %18, %11, %4
%31 = phi i8* [ null, %4 ], [ null, %11 ], [ null, %18 ], [ null, %24 ], [ %16, %28 ]
ret i8* %31
}