#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;
unsigned char *calloc(long, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
long fread(unsigned char *, int, long, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
long load_rom(char *file_name, unsigned char *buffer[]) {
  FILE *rom_file;
  long rom_size;
  rom_file = fopen(file_name, "rb");
  if (rom_file == NULL) {
    perror("fopen (rom)");
    exit(EXIT_FAILURE);
  }
  if (fseek(rom_file, 0, SEEK_END) == 0) {
    rom_size = ftell(rom_file);
    rewind(rom_file);
  } else {
    perror("Error getting size of rom.");
    exit(EXIT_FAILURE);
  }
  *buffer = calloc(rom_size, sizeof(char));
  if (*buffer == NULL) {
    perror("Error creating buffer.");
    exit(EXIT_FAILURE);
  }
  if (fread(*buffer, sizeof(char), rom_size, rom_file) != rom_size) {
    perror("Error reading rom to buffer.");
    exit(EXIT_FAILURE);
  }
  fclose(rom_file);
  return rom_size;
}


x86 assembly (clang, -O0):
<load_rom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <load_rom+0x49>
lea    0xe3d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jne    1212 <load_rom+0x82>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
jmp    122c <load_rom+0x9c>
lea    0xdf6(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    126c <load_rom+0xdc>
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x20(%rbp),%rax
je     12a9 <load_rom+0x119>
lea    0xdab(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_rom>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe61(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1215 <load_rom+0x85>
mov    %rax,%rbx
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    121e <load_rom+0x8e>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r15,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1227 <load_rom+0x97>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %r15,%rax
jne    1230 <load_rom+0xa0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xde7(%rip),%rdi
jmp    1237 <load_rom+0xa7>
lea    0xdea(%rip),%rdi
jmp    1237 <load_rom+0xa7>
lea    0xdfc(%rip),%rdi
jmp    1237 <load_rom+0xa7>
lea    0xe0a(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [12 x i8] c"fopen (rom)\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@.str.2 = [27 x i8] c"Error getting size of rom.\00"
@.str.3 = [23 x i8] c"Error creating buffer.\00"
@.str.4 = [29 x i8] c"Error reading rom to buffer.\00"
define i64 @load_rom(i8* %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i64
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** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32*, i32** %5
%17 = load i32, i32* @SEEK_END
%18 = call i64 @fseek(i32* %16, i32 0, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %15
%21 = load i32*, i32** %5
%22 = call i64 @ftell(i32* %21)
store i64 %22, i64* %6
%23 = load i32*, i32** %5
%24 = call i32 @rewind(i32* %23)
br label %29
25: ; preds = %15
%26 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %20
%30 = load i64, i64* %6
%31 = call i8* @calloc(i64 %30, i32 1)
%32 = load i8**, i8*** %4
store i8* %31, i8** %32
%33 = load i8**, i8*** %4
%34 = load i8*, i8** %33
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %29
%41 = load i8**, i8*** %4
%42 = load i8*, i8** %41
%43 = load i64, i64* %6
%44 = load i32*, i32** %5
%45 = call i64 @fread(i8* %42, i32 1, i64 %43, i32* %44)
%46 = load i64, i64* %6
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %52
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %40
%53 = load i32*, i32** %5
%54 = call i32 @fclose(i32* %53)
%55 = load i64, i64* %6
ret i64 %55
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [12 x i8] c"fopen (rom)\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@.str.2 = [27 x i8] c"Error getting size of rom.\00"
@.str.3 = [23 x i8] c"Error creating buffer.\00"
@.str.4 = [29 x i8] c"Error reading rom to buffer.\00"
define i64 @load_rom(i8* nocapture %0, i8** nocapture writeonly %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
%6 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 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 i64 @fseek(i32* %3, i32 0, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = tail call i64 @ftell(i32* %3)
%15 = tail call i32 @rewind(i32* %3)
%16 = tail call i8* @calloc(i64 %14, i32 1)
store i8* %16, i8** %1
%17 = icmp eq i8* %16, null
br i1 %17, label %22, label %26
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %13
%23 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %13
%27 = tail call i64 @fread(i8* %16, i32 1, i64 %14, i32* %3)
%28 = icmp eq i64 %27, %14
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %26
%34 = tail call i32 @fclose(i32* %3)
ret i64 %14
}