#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 */
scalar_t__ Disassemble6502Op(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 *, 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);
  fread(buffer, fsize, 1, f);
  fclose(f);
  int pc = 0;
  while (pc < fsize) {
    pc += Disassemble6502Op(buffer, pc);
  }
  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    11e7 <main+0x57>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2b(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2df6(%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    0x2dc4(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x1c(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    127a <main+0xea>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <Disassemble6502Op@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
jmp    1250 <main+0xc0>
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    0xe5b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1240 <main+0xb0>
mov    %rax,%r14
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %eax,%r15d
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
test   %r15d,%r15d
jle    1233 <main+0xa3>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbp,%rdi
mov    %ebx,%esi
call   1040 <Disassemble6502Op@plt>
add    %ebx,%eax
mov    %eax,%ebx
cmp    %r15d,%eax
jl     1220 <main+0x90>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rsi
lea    0xdb8(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10b0 <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
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 @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 = call i8* @malloc(i32 %31)
store i8* %32, i8** %8
%33 = load i8*, i8** %8
%34 = load i32, i32* %7
%35 = load i32*, i32** %6
%36 = call i32 @fread(i8* %33, i32 %34, i32 1, i32* %35)
%37 = load i32*, i32** %6
%38 = call i32 @fclose(i32* %37)
store i32 0, i32* %9
br label %39
39: ; preds = %43, %22
%40 = load i32, i32* %9
%41 = load i32, i32* %7
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i8*, i8** %8
%45 = load i32, i32* %9
%46 = call i64 @Disassemble6502Op(i8* %44, i32 %45)
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = add nsw i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* %9
br label %39
51: ; preds = %39
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 @printf(i8* 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 = tail call i8* @malloc(i32 %14)
%18 = tail call i32 @fread(i8* %17, i32 %14, i32 1, i32* %5)
%19 = tail call i32 @fclose(i32* %5)
%20 = icmp sgt i32 %14, 0
br i1 %20, label %21, label %27
21: ; preds = %11, %21
%22 = phi i32 [ %25, %21 ], [ 0, %11 ]
%23 = tail call i64 @Disassemble6502Op(i8* %17, i32 %22)
%24 = trunc i64 %23 to i32
%25 = add i32 %22, %24
%26 = icmp slt i32 %25, %14
br i1 %26, label %21, label %27
27: ; preds = %21, %11
ret i32 0
}