#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  unsigned char rom[1500];
  unsigned char ram[65536];
  FILE *arch;
  unsigned char c, opcode, flag, exit = 0;
  char buf[80];
  int ptr = 0, oldptr, siz, count, scan, index;
  arch = fopen("65C02.bin", "rb");
  if (arch == NULL) {
    printf("\n***Problem opening Opcode List '65C02.bin'!***\n");
    return -1;
  }
  fseek(arch, 0, SEEK_END);
  siz = ftell(arch);
  fseek(arch, 0, SEEK_SET);
  fread(rom, siz, 1, arch);
  ptr = 0x400;
  do {
    printf("%04x: ", ptr);
    gets(buf);
    scan = 0;
    opcode = 0;
    flag = 0;
    while (opcode < 256 && !flag) {
      index = 0;
      while (buf[index] != (rom[scan] & 127) && buf[index]) {
        index++;
        scan++;
      }
      if (buf[index] == rom[scan]) {
        flag = 1;
      } else {
        while (rom[scan] >= 128)
          scan++;
        scan++;
        opcode++;
      }
    }
    if (opcode < 256)
      printf("OPCODE: %02x\n", opcode);
  } while (!exit);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10660,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x105ec(%rbp)
movl   $0x0,-0x10644(%rbp)
lea    0xe76(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x105e8(%rbp)
cmpq   $0x0,-0x105e8(%rbp)
jne    11c5 <main+0x65>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ff <main+0x29f>
mov    -0x105e8(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
mov    -0x105e8(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x1064c(%rbp)
mov    -0x105e8(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
lea    -0x5e0(%rbp),%rdi
mov    -0x1064c(%rbp),%esi
mov    -0x105e8(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
movl   $0x400,-0x10644(%rbp)
mov    -0x10644(%rbp),%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x10640(%rbp),%rdi
call   1060 <gets@plt>
movl   $0x0,-0x10654(%rbp)
movb   $0x0,-0x105ea(%rbp)
movb   $0x0,-0x105eb(%rbp)
movzbl -0x105ea(%rbp),%ecx
xor    %eax,%eax
cmp    $0x100,%ecx
mov    %al,-0x10659(%rbp)
jge    1292 <main+0x132>
cmpb   $0x0,-0x105eb(%rbp)
setne  %al
xor    $0xff,%al
mov    %al,-0x10659(%rbp)
mov    -0x10659(%rbp),%al
test   $0x1,%al
jne    12a5 <main+0x145>
jmp    13b8 <main+0x258>
movl   $0x0,-0x10658(%rbp)
movslq -0x10658(%rbp),%rax
movsbl -0x10640(%rbp,%rax,1),%ecx
ff
movslq -0x10654(%rbp),%rax
movzbl -0x5e0(%rbp,%rax,1),%edx
ff
and    $0x7f,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x1065a(%rbp)
je     12fb <main+0x19b>
movslq -0x10658(%rbp),%rax
movsbl -0x10640(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1065a(%rbp)
mov    -0x1065a(%rbp),%al
test   $0x1,%al
jne    130e <main+0x1ae>
jmp    1331 <main+0x1d1>
mov    -0x10658(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10658(%rbp)
mov    -0x10654(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10654(%rbp)
jmp    12af <main+0x14f>
movslq -0x10658(%rbp),%rax
movsbl -0x10640(%rbp,%rax,1),%eax
ff
movslq -0x10654(%rbp),%rcx
movzbl -0x5e0(%rbp,%rcx,1),%ecx
ff
cmp    %ecx,%eax
jne    1363 <main+0x203>
movb   $0x1,-0x105eb(%rbp)
jmp    13b3 <main+0x253>
jmp    1368 <main+0x208>
movslq -0x10654(%rbp),%rax
movzbl -0x5e0(%rbp,%rax,1),%eax
ff
cmp    $0x80,%eax
jl     1396 <main+0x236>
mov    -0x10654(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10654(%rbp)
jmp    1368 <main+0x208>
mov    -0x10654(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10654(%rbp)
mov    -0x105ea(%rbp),%al
add    $0x1,%al
mov    %al,-0x105ea(%rbp)
jmp    1265 <main+0x105>
movzbl -0x105ea(%rbp),%eax
cmp    $0x100,%eax
jge    13df <main+0x27f>
movzbl -0x105ea(%rbp),%esi
lea    0xc6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    13e4 <main+0x284>
cmpb   $0x0,-0x105ec(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    122d <main+0xcd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10660,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x638,%rsp
lea    0xe78(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12a8 <main+0x138>
mov    %rax,%rbx
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1060 <ftell@plt>
mov    %eax,%ebp
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
lea    0x50(%rsp),%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
lea    0x51(%rsp),%r13
lea    0xe1d(%rip),%r14
mov    %rsp,%r15
lea    0xe1a(%rip),%r12
jmp    120d <main+0x9d>
nopl   0x0(%rax)
movzbl %al,%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    $0x400,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1070 <gets@plt>
movsbl (%rsp),%r8d
xor    %edx,%edx
xor    %eax,%eax
nopl   (%rax)
movslq %edx,%rbx
movzbl 0x50(%rsp,%rbx,1),%ecx
mov    %ecx,%esi
and    $0x7f,%esi
test   %r8b,%r8b
je     1270 <main+0x100>
cmp    %esi,%r8d
je     1270 <main+0x100>
add    %r13,%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
movsbl 0x1(%rsp,%rbp,1),%esi
movzbl (%rbx,%rbp,1),%ecx
add    $0x1,%rbp
mov    %ecx,%edi
and    $0x7f,%edi
test   %esi,%esi
je     126a <main+0xfa>
cmp    %edi,%esi
jne    1250 <main+0xe0>
add    %ebp,%edx
jmp    1273 <main+0x103>
xchg   %ax,%ax
mov    %r8d,%esi
movzbl %cl,%edi
cmp    %edi,%esi
je     1200 <main+0x90>
movslq %edx,%rcx
lea    (%rsp,%rcx,1),%rbp
add    $0x50,%rbp
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%edx
cmpb   $0x0,0x0(%rbp)
lea    0x1(%rbp),%rbp
js     1290 <main+0x120>
add    $0x1,%al
cmp    %edi,%esi
jne    1230 <main+0xc0>
jmp    1200 <main+0x90>
lea    0xd73(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x638,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"65C02.bin\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [49 x i8] c"\0A***Problem opening Opcode List '65C02.bin'!***\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"%04x: \00"
@.str.4 = [14 x i8] c"OPCODE: %02x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1500 x i8]
%3 = alloca [65536 x i8]
%4 = alloca i32*
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca [80 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %1
store i8 0, i8* %8
store i32 0, i32* %10
%16 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %4
%17 = load i32*, i32** %4
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %121
21: ; preds = %0
%22 = load i32*, i32** %4
%23 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i32*, i32** %4
%26 = call i32 @ftell(i32* %25)
store i32 %26, i32* %12
%27 = load i32*, i32** %4
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = getelementptr inbounds [1500 x i8], [1500 x i8]* %2, i64 0, i64 0
%31 = load i32, i32* %12
%32 = load i32*, i32** %4
%33 = call i32 @fread(i8* %30, i32 %31, i32 1, i32* %32)
store i32 1024, i32* %10
br label %34
34: ; preds = %116, %21
%35 = load i32, i32* %10
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%38 = call i32 @gets(i8* %37)
store i32 0, i32* %14
store i8 0, i8* %6
store i8 0, i8* %7
br label %39
39: ; preds = %106, %34
%40 = load i8, i8* %6
%41 = zext i8 %40 to i32
%42 = icmp slt i32 %41, 256
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i8, i8* %7
%45 = icmp ne i8 %44, 0
%46 = xor i1 %45, true
br label %47
47: ; preds = %43, %39
%48 = phi i1 [ false, %39 ], [ %46, %43 ]
br i1 %48, label %49, label %107
49: ; preds = %47
store i32 0, i32* %15
br label %50
50: ; preds = %72, %49
%51 = load i32, i32* %15
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = load i32, i32* %14
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [1500 x i8], [1500 x i8]* %2, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = and i32 %60, 127
%62 = icmp ne i32 %55, %61
br i1 %62, label %63, label %70
63: ; preds = %50
%64 = load i32, i32* %15
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 %65
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = icmp ne i32 %68, 0
br label %70
70: ; preds = %63, %50
%71 = phi i1 [ false, %50 ], [ %69, %63 ]
br i1 %71, label %72, label %77
72: ; preds = %70
%73 = load i32, i32* %15
%74 = add nsw i32 %73, 1
store i32 %74, i32* %15
%75 = load i32, i32* %14
%76 = add nsw i32 %75, 1
store i32 %76, i32* %14
br label %50
77: ; preds = %70
%78 = load i32, i32* %15
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 %79
%81 = load i8, i8* %80
%82 = sext i8 %81 to i32
%83 = load i32, i32* %14
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [1500 x i8], [1500 x i8]* %2, i64 0, i64 %84
%86 = load i8, i8* %85
%87 = zext i8 %86 to i32
%88 = icmp eq i32 %82, %87
br i1 %88, label %89, label %90
89: ; preds = %77
store i8 1, i8* %7
br label %106
90: ; preds = %77
br label %91
91: ; preds = %98, %90
%92 = load i32, i32* %14
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [1500 x i8], [1500 x i8]* %2, i64 0, i64 %93
%95 = load i8, i8* %94
%96 = zext i8 %95 to i32
%97 = icmp sge i32 %96, 128
br i1 %97, label %98, label %101
98: ; preds = %91
%99 = load i32, i32* %14
%100 = add nsw i32 %99, 1
store i32 %100, i32* %14
br label %91
101: ; preds = %91
%102 = load i32, i32* %14
%103 = add nsw i32 %102, 1
store i32 %103, i32* %14
%104 = load i8, i8* %6
%105 = add i8 %104, 1
store i8 %105, i8* %6
br label %106
106: ; preds = %101, %89
br label %39
107: ; preds = %47
%108 = load i8, i8* %6
%109 = zext i8 %108 to i32
%110 = icmp slt i32 %109, 256
br i1 %110, label %111, label %115
111: ; preds = %107
%112 = load i8, i8* %6
%113 = zext i8 %112 to i32
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %113)
br label %115
115: ; preds = %111, %107
br label %116
116: ; preds = %115
%117 = load i8, i8* %8
%118 = icmp ne i8 %117, 0
%119 = xor i1 %118, true
br i1 %119, label %34, label %120
120: ; preds = %116
store i32 0, i32* %1
br label %121
121: ; preds = %120, %19
%122 = load i32, i32* %1
ret i32 %122
}

LLVM IR (-O2):
@.str = [10 x i8] c"65C02.bin\00"
@.str.1 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"%04x: \00"
@.str.4 = [14 x i8] c"OPCODE: %02x\0A\00"
@str = [48 x i8] c"\0A***Problem opening Opcode List '65C02.bin'!***\00"
define i32 @main() {
%1 = alloca [1500 x i8]
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
ret i32 -1
9: ; preds = %0
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @fseek(i32* %5, i32 0, i32 %10)
%12 = tail call i32 @ftell(i32* %5)
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @fseek(i32* %5, i32 0, i32 %13)
%15 = call i32 @fread(i8* %3, i32 %12, i32 1, i32* %5)
br label %16
16: ; preds = %68, %9
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 1024)
%18 = call i32 @gets(i8* %4)
%19 = load i8, i8* %4
%20 = sext i8 %19 to i32
%21 = icmp eq i8 %19, 0
br label %22
22: ; preds = %16, %64
%23 = phi i32 [ 0, %16 ], [ %66, %64 ]
%24 = phi i8 [ 0, %16 ], [ %67, %64 ]
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = and i8 %27, 127
%29 = zext i8 %28 to i32
%30 = icmp eq i32 %20, %29
%31 = or i1 %21, %30
br i1 %31, label %50, label %32
32: ; preds = %22, %32
%33 = phi i64 [ %36, %32 ], [ %25, %22 ]
%34 = phi i64 [ %35, %32 ], [ 0, %22 ]
%35 = add nuw nsw i64 %34, 1
%36 = add i64 %33, 1
%37 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 %35
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 %36
%41 = load i8, i8* %40
%42 = and i8 %41, 127
%43 = zext i8 %42 to i32
%44 = icmp eq i32 %39, %43
%45 = icmp eq i8 %38, 0
%46 = or i1 %45, %44
br i1 %46, label %47, label %32
47: ; preds = %32
%48 = sext i8 %38 to i32
%49 = trunc i64 %36 to i32
br label %50
50: ; preds = %47, %22
%51 = phi i32 [ %48, %47 ], [ %20, %22 ]
%52 = phi i32 [ %49, %47 ], [ %23, %22 ]
%53 = phi i8 [ %41, %47 ], [ %27, %22 ]
%54 = zext i8 %53 to i32
%55 = icmp eq i32 %51, %54
br i1 %55, label %68, label %56
56: ; preds = %50
%57 = sext i32 %52 to i64
br label %58
58: ; preds = %56, %58
%59 = phi i64 [ %57, %56 ], [ %63, %58 ]
%60 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = icmp slt i8 %61, 0
%63 = add i64 %59, 1
br i1 %62, label %58, label %64
64: ; preds = %58
%65 = trunc i64 %59 to i32
%66 = add nsw i32 %65, 1
%67 = add i8 %24, 1
br i1 %55, label %68, label %22
68: ; preds = %50, %64
%69 = phi i8 [ %67, %64 ], [ %24, %50 ]
%70 = zext i8 %69 to i32
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %70)
br label %16
}