#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 uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int *calloc(int, int);
int /*<<< orphan*/ emulate(int *, int *);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s ROM.bin\n\n", argv[0]);
    return 1;
  }
  FILE *file = fopen(argv[1], "rb");
  if (!file) {
    int err = errno;
    perror("Error opening ROM file");
    return err;
  }
  uint8_t *memory = calloc(0x10000, 1);
  if (!memory) {
    int err = errno;
    perror("Error allocating memory");
    return err;
  }
  fread(memory, 1, 0x10000, file);
  if (ferror(file)) {
    int err = errno;
    perror("Error reading ROM file");
    return err;
  }
  fclose(file);
  uint8_t registers[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0};
  emulate(memory, registers);
  free(memory);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11e8 <main+0x48>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f9 <main+0x159>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122e <main+0x8e>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xdf9(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f9 <main+0x159>
mov    $0x10000,%edi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126f <main+0xcf>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    0xdcf(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f9 <main+0x159>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1050 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12bc <main+0x11c>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    0xd9a(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12f9 <main+0x159>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1070 <memset@plt>
movl   $0xff,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1090 <emulate@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x48,%rsp
cmp    $0x1,%edi
jg     11be <main+0x2e>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe51(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%ebx
jmp    1252 <main+0xc2>
mov    0x8(%rsi),%rdi
lea    0xe4b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     125c <main+0xcc>
mov    %rax,%rbx
mov    $0x10000,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
test   %rax,%rax
je     1273 <main+0xe3>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
jne    128a <main+0xfa>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0xff,0x38(%rsp)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1080 <emulate@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d75(%rip),%rax
mov    (%rax),%ebx
lea    0xdab(%rip),%rdi
call   10b0 <perror@plt>
jmp    1252 <main+0xc2>
mov    0x2d5e(%rip),%rax
mov    (%rax),%ebx
lea    0xdab(%rip),%rdi
call   10b0 <perror@plt>
jmp    1252 <main+0xc2>
mov    0x2d47(%rip),%rax
mov    (%rax),%ebx
lea    0xdac(%rip),%rdi
call   10b0 <perror@plt>
jmp    1252 <main+0xc2>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s ROM.bin\0A\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Error opening ROM file\00"
@.str.3 = [24 x i8] c"Error allocating memory\00"
@.str.4 = [23 x i8] c"Error reading ROM file\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [16 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 1, i32* %3
br label %61
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %6
%25 = load i32*, i32** %6
%26 = icmp ne i32* %25, null
br i1 %26, label %31, label %27
27: ; preds = %20
%28 = load i32, i32* @errno
store i32 %28, i32* %7
%29 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* %7
store i32 %30, i32* %3
br label %61
31: ; preds = %20
%32 = call i32* @calloc(i32 65536, i32 1)
store i32* %32, i32** %8
%33 = load i32*, i32** %8
%34 = icmp ne i32* %33, null
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* @errno
store i32 %36, i32* %9
%37 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* %9
store i32 %38, i32* %3
br label %61
39: ; preds = %31
%40 = load i32*, i32** %8
%41 = load i32*, i32** %6
%42 = call i32 @fread(i32* %40, i32 1, i32 65536, i32* %41)
%43 = load i32*, i32** %6
%44 = call i64 @ferror(i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %39
%47 = load i32, i32* @errno
store i32 %47, i32* %10
%48 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* %10
store i32 %49, i32* %3
br label %61
50: ; preds = %39
%51 = load i32*, i32** %6
%52 = call i32 @fclose(i32* %51)
%53 = bitcast [16 x i32]* %11 to i8*
%54 = bitcast i8* %53 to [16 x i32]*
%55 = getelementptr inbounds [16 x i32], [16 x i32]* %54, i32 0, i32 14
store i32 255, i32* %55
%56 = load i32*, i32** %8
%57 = getelementptr inbounds [16 x i32], [16 x i32]* %11, i64 0, i64 0
%58 = call i32 @emulate(i32* %56, i32* %57)
%59 = load i32*, i32** %8
%60 = call i32 @free(i32* %59)
store i32 0, i32* %3
br label %61
61: ; preds = %50, %46, %35, %27, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Usage: %s ROM.bin\0A\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Error opening ROM file\00"
@.str.3 = [24 x i8] c"Error allocating memory\00"
@.str.4 = [23 x i8] c"Error reading ROM file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i32]
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %37
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %37
17: ; preds = %9
%18 = tail call i32* @calloc(i32 65536, i32 1)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %37
23: ; preds = %17
%24 = tail call i32 @fread(i32* %18, i32 1, i32 65536, i32* %12)
%25 = tail call i64 @ferror(i32* %12)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %37
30: ; preds = %23
%31 = tail call i32 @fclose(i32* %12)
%32 = bitcast [16 x i32]* %3 to i8*
%33 = getelementptr inbounds [16 x i32], [16 x i32]* %3, i64 0, i64 14
store i32 255, i32* %33
%34 = getelementptr inbounds [16 x i32], [16 x i32]* %3, i64 0, i64 0
%35 = call i32 @emulate(i32* %18, i32* %34)
%36 = call i32 @free(i32* %18)
br label %37
37: ; preds = %14, %30, %27, %20, %5
%38 = phi i32 [ 1, %5 ], [ %15, %14 ], [ %28, %27 ], [ 0, %30 ], [ %21, %20 ]
ret i32 %38
}