#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 *, char *);
int /*<<< orphan*/ fread(unsigned char *, unsigned int, int,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ print_opcode(unsigned char *, unsigned int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(char);
int main(int argc, char *argv[]) {
  FILE *rom_file;
  unsigned int pc = 0x200;
  unsigned int file_size;
  unsigned char *buffer;
  if ((rom_file = fopen(argv[1], "rb")) == NULL) {
    printf("[ERROR] - Couldn't open file!\n");
    exit(EXIT_FAILURE);
  }
  fseek(rom_file, 0L, SEEK_END);
  file_size = ftell(rom_file);
  fseek(rom_file, 0L, SEEK_SET);
  buffer = malloc(file_size + pc);
  fread(buffer + pc, file_size, 1, rom_file);
  fclose(rom_file);
  while (pc < (file_size + 0x200)) {
    print_opcode(buffer, pc);
    pc += 2;
  }
  putchar('\n');
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x200,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe24(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1209 <main+0x59>
lea    0xe0d(%rip),%rdi
call   1070 <printf@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%edi
add    -0x1c(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
add    $0x200,%ecx
cmp    %ecx,%eax
jae    12a2 <main+0xf2>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10d0 <print_opcode@plt>
mov    -0x1c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
jmp    1274 <main+0xc4>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
lea    0xe3e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1270 <main+0xc0>
mov    %rax,%r14
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebx
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x200(%rbx),%ebp
mov    %ebp,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
lea    0x200(%rax),%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fread@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
cmp    $0x201,%ebp
jb     1251 <main+0xa1>
mov    $0x200,%ebx
xchg   %ax,%ax
mov    %r15,%rdi
mov    %ebx,%esi
call   10d0 <print_opcode@plt>
add    $0x2,%ebx
cmp    %ebp,%ebx
jb     1240 <main+0x90>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8c(%rip),%rdi
call   1050 <puts@plt>
mov    0x2d4d(%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] - Couldn't open file!\0A\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 512, i32* %7
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i32*, i32** %6
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i64 0, i32 %21)
%23 = load i32*, i32** %6
%24 = call i32 @ftell(i32* %23)
store i32 %24, i32* %8
%25 = load i32*, i32** %6
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @fseek(i32* %25, i64 0, i32 %26)
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = add i32 %28, %29
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = load i32, i32* %7
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i32, i32* %8
%37 = load i32*, i32** %6
%38 = call i32 @fread(i8* %35, i32 %36, i32 1, i32* %37)
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
br label %41
41: ; preds = %46, %19
%42 = load i32, i32* %7
%43 = load i32, i32* %8
%44 = add i32 %43, 512
%45 = icmp ult i32 %42, %44
br i1 %45, label %46, label %52
46: ; preds = %41
%47 = load i8*, i8** %9
%48 = load i32, i32* %7
%49 = call i32 @print_opcode(i8* %47, i32 %48)
%50 = load i32, i32* %7
%51 = add i32 %50, 2
store i32 %51, i32* %7
br label %41
52: ; preds = %41
%53 = call i32 @putchar(i8 signext 10)
%54 = load i8*, i8** %9
%55 = call i32 @free(i8* %54)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@EXIT_FAILURE = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@str = [30 x i8] c"[ERROR] - Couldn't open file!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @fseek(i32* %5, i64 0, i32 %12)
%14 = tail call i32 @ftell(i32* %5)
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %5, i64 0, i32 %15)
%17 = add i32 %14, 512
%18 = tail call i8* @malloc(i32 %17)
%19 = getelementptr inbounds i8, i8* %18, i64 512
%20 = tail call i32 @fread(i8* %19, i32 %14, i32 1, i32* %5)
%21 = tail call i32 @fclose(i32* %5)
%22 = icmp ugt i32 %17, 512
br i1 %22, label %23, label %28
23: ; preds = %11, %23
%24 = phi i32 [ %26, %23 ], [ 512, %11 ]
%25 = tail call i32 @print_opcode(i8* %18, i32 %24)
%26 = add i32 %24, 2
%27 = icmp ult i32 %26, %17
br i1 %27, label %23, label %28
28: ; preds = %23, %11
%29 = tail call i32 @putchar(i8 signext 10)
%30 = tail call i32 @free(i8* %18)
ret i32 0
}