#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(int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int load_rom(char *file_name, unsigned char *buffer[]) {
  FILE *rom_file;
  int rom_size;
  rom_file = fopen(file_name, "rb");
  if (rom_file == NULL) {
    perror("Error opening 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    1211 <load_rom+0x81>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
jmp    122b <load_rom+0x9b>
lea    0xdfe(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
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    126a <load_rom+0xda>
lea    0xdda(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x1c(%rbp),%eax
je     12a5 <load_rom+0x115>
lea    0xdb6(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_rom>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe62(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120e <load_rom+0x7e>
mov    %rax,%rbx
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
jne    1217 <load_rom+0x87>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1220 <load_rom+0x90>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    %ebp,%eax
jne    1229 <load_rom+0x99>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdee(%rip),%rdi
jmp    1230 <load_rom+0xa0>
lea    0xdf8(%rip),%rdi
jmp    1230 <load_rom+0xa0>
lea    0xe0a(%rip),%rdi
jmp    1230 <load_rom+0xa0>
lea    0xe18(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Error opening 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 i32 @load_rom(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** %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 ([19 x i8], [19 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 i32 @ftell(i32* %21)
store i32 %22, i32* %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 i32, i32* %6
%31 = call i8* @calloc(i32 %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 i32, i32* %6
%44 = load i32*, i32** %5
%45 = call i32 @fread(i8* %42, i32 1, i32 %43, i32* %44)
%46 = load i32, i32* %6
%47 = icmp ne i32 %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 i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Error opening 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 i32 @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 ([19 x i8], [19 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 i32 @ftell(i32* %3)
%15 = tail call i32 @rewind(i32* %3)
%16 = tail call i8* @calloc(i32 %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 i32 @fread(i8* %16, i32 1, i32 %14, i32* %3)
%28 = icmp eq i32 %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 i32 %14
}