#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*/ DisassembleChip8Op(unsigned char *, int);
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  FILE *f = fopen(argv[1], "rb");
  if (f == NULL) {
    printf("error: Couldn't open %s\n", argv[1]);
    exit(1);
  }
  fseek(f, 0L, SEEK_END);
  int fsize = ftell(f);
  fseek(f, 0L, SEEK_SET);
  unsigned char *buffer = malloc(fsize + 0x200);
  fread(buffer + 0x200, fsize, 1, f);
  fclose(f);
  int pc = 0x200;
  while (pc < (fsize + 0x200)) {
    DisassembleChip8Op(buffer, pc);
    pc += 2;
    printf("\n");
  }
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e9 <main+0x59>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x1c(%rbp),%edi
add    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x200,%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x200,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x1c(%rbp),%ecx
add    $0x200,%ecx
cmp    %ecx,%eax
jge    129b <main+0x10b>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1060 <DisassembleChip8Op@plt>
mov    -0x2c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    125f <main+0xcf>
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    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0xe4b(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1259 <main+0xb9>
mov    %rax,%r14
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
lea    0x200(%rbp),%r15d
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
lea    0x200(%rax),%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %ebp,%ebp
jle    124c <main+0xac>
mov    $0x200,%ebp
nop
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <DisassembleChip8Op@plt>
add    $0x2,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    %r15d,%ebp
jl     1230 <main+0x90>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rsi
lea    0xd9f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"error: Couldn't open %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [2 x i8] c"\0A\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 i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%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 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i32*, i32** %6
%24 = load i32, i32* @SEEK_END
%25 = call i32 @fseek(i32* %23, i64 0, i32 %24)
%26 = load i32*, i32** %6
%27 = call i32 @ftell(i32* %26)
store i32 %27, i32* %7
%28 = load i32*, i32** %6
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %28, i64 0, i32 %29)
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 512
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i64 512
%36 = load i32, i32* %7
%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)
store i32 512, i32* %9
br label %41
41: ; preds = %46, %22
%42 = load i32, i32* %9
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 512
%45 = icmp slt i32 %42, %44
br i1 %45, label %46, label %53
46: ; preds = %41
%47 = load i8*, i8** %8
%48 = load i32, i32* %9
%49 = call i32 @DisassembleChip8Op(i8* %47, i32 %48)
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 2
store i32 %51, i32* %9
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %41
53: ; preds = %41
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [25 x i8] c"error: Couldn't open %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
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 = load i8*, i8** %3
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
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 nsw 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 sgt i32 %14, 0
br i1 %22, label %23, label %29
23: ; preds = %11, %23
%24 = phi i32 [ %26, %23 ], [ 512, %11 ]
%25 = tail call i32 @DisassembleChip8Op(i8* %18, i32 %24)
%26 = add nuw nsw i32 %24, 2
%27 = tail call i32 @putchar(i32 10)
%28 = icmp slt i32 %26, %17
br i1 %28, label %23, label %29
29: ; preds = %23, %11
ret i32 0
}