#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
unsigned int fread(char *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ smalloc(int);
int load_file_to_memory(const char *filename, char **result) {
  unsigned int size;
  FILE *f = fopen(filename, "rb");
  if (f == NULL) {
    *result = NULL;
    printf("Error: Could not read file %s\n", filename);
    exit(EXIT_FAILURE);
  }
  fseek(f, 0, SEEK_END);
  size = ftell(f);
  fseek(f, 0, SEEK_SET);
  *result = (char *)smalloc(sizeof(char) * (size + 1));
  if (size != fread(*result, sizeof(char), size, f)) {
    free(*result);
    printf("Error: read of kernel failed\n");
    exit(EXIT_FAILURE);
  }
  fclose(f);
  (*result)[size] = 0;
  return size;
}


x86 assembly (clang, -O0):
<load_file_to_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ea <load_file_to_memory+0x5a>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <smalloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
je     128e <load_file_to_memory+0xfe>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x14(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file_to_memory>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe4e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1229 <load_file_to_memory+0x89>
mov    %rax,%r15
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebx
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
lea    0x1(%rbx),%edi
call   10b0 <smalloc@plt>
mov    %rax,(%r14)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
cmp    %eax,%ebx
jne    1243 <load_file_to_memory+0xa3>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    (%r14),%rax
mov    %ebx,%ecx
movb   $0x0,(%rax,%rcx,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
movq   $0x0,(%r14)
lea    0xdcc(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1257 <load_file_to_memory+0xb7>
mov    (%r14),%rdi
call   1030 <free@plt>
lea    0xdd0(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"Error: Could not read file %s\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [30 x i8] c"Error: read of kernel failed\0A\00"
define i32 @load_file_to_memory(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %3
store i8** %1, i8*** %4
%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 %17
11: ; preds = %2
%12 = load i8**, i8*** %4
store i8* null, i8** %12
%13 = load i8*, i8** %3
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %6
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %5
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i32, i32* %5
%27 = add i32 %26, 1
%28 = zext i32 %27 to i64
%29 = mul i64 1, %28
%30 = trunc i64 %29 to i32
%31 = call i64 @smalloc(i32 %30)
%32 = inttoptr i64 %31 to i8*
%33 = load i8**, i8*** %4
store i8* %32, i8** %33
%34 = load i32, i32* %5
%35 = load i8**, i8*** %4
%36 = load i8*, i8** %35
%37 = load i32, i32* %5
%38 = load i32*, i32** %6
%39 = call i32 @fread(i8* %36, i32 1, i32 %37, i32* %38)
%40 = icmp ne i32 %34, %39
br i1 %40, label %41, label %48
41: ; preds = %17
%42 = load i8**, i8*** %4
%43 = load i8*, i8** %42
%44 = call i32 @free(i8* %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %17
%49 = load i32*, i32** %6
%50 = call i32 @fclose(i32* %49)
%51 = load i8**, i8*** %4
%52 = load i8*, i8** %51
%53 = load i32, i32* %5
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 0, i8* %55
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"Error: Could not read file %s\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [29 x i8] c"Error: read of kernel failed\00"
define i32 @load_file_to_memory(i8* %0, i8** nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
store i8* null, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %3, i32 0, i32 %10)
%12 = tail call i32 @ftell(i32* %3)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %3, i32 0, i32 %13)
%15 = add i32 %12, 1
%16 = tail call i64 @smalloc(i32 %15)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %1
%18 = tail call i32 @fread(i8* %17, i32 1, i32 %12, i32* %3)
%19 = icmp eq i32 %12, %18
br i1 %19, label %26, label %20
20: ; preds = %9
%21 = load i8*, i8** %1
%22 = tail call i32 @free(i8* %21)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %9
%27 = tail call i32 @fclose(i32* %3)
%28 = load i8*, i8** %1
%29 = zext i32 %12 to i64
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 0, i8* %30
ret i32 %12
}