// File: /workspace/github_c_makefile_star2_O2/ShonFrazier[P]lib6502/run6502.c
// Function: main
int main(int argc, char **argv)
{
  M6502 *mpu= M6502_new(0, 0, 0);
  int bTraps= 0;
  int countCycles = 0;

  program= argv[0];

  if ((2 == argc) && ('-' != *argv[1]))
    {
      if ((!loadInterpreter(mpu, 0, argv[1])) && (!load(mpu, 0, argv[1])))
	pfail(argv[1]);
      doBtraps(0, 0, mpu);
    }
  else
    while (++argv, --argc > 0)
      {
	int n= 0;
	if      (!strcmp(*argv, "-B"))  bTraps= 1;
	else if (!strcmp(*argv, "-c"))	countCycles = 1;
	else if (!strcmp(*argv, "-d"))	n= doDisassemble(argc, argv, mpu);
	else if (!strcmp(*argv, "-G"))	n= doGtrap(argc, argv, mpu);
	else if (!strcmp(*argv, "-h"))	n= doHelp(argc, argv, mpu);
	else if (!strcmp(*argv, "-i"))	n= doLoadInterpreter(argc, argv, mpu);
	else if (!strcmp(*argv, "-I"))	n= doIRQ(argc, argv, mpu);
	else if (!strcmp(*argv, "-l"))	n= doLoad(argc, argv, mpu);
	else if (!strcmp(*argv, "-M"))	n= doMtrap(argc, argv, mpu);
	else if (!strcmp(*argv, "-N"))	n= doNMI(argc, argv, mpu);
	else if (!strcmp(*argv, "-P"))	n= doPtrap(argc, argv, mpu);
	else if (!strcmp(*argv, "-R"))	n= doRST(argc, argv, mpu);
	else if (!strcmp(*argv, "-s"))	n= doSave(argc, argv, mpu);
	else if (!strcmp(*argv, "-v"))	n= doVersion(argc, argv, mpu);
	else if (!strcmp(*argv, "-X"))	n= doXtrap(argc, argv, mpu);
	else if (!strcmp(*argv, "-x"))	exit(0);
	else if ('-' == **argv)		usage(1);
	else
	  {
	    /* doBtraps() left 0x8000+0x4000 in bank 0, so load */
	    /* additional images starting at 15 and work down */
	    static int bankSel= 0x0F;
	    if (!bTraps)			usage(1);
	    if (bankSel < 0)			fail("too many images");
	    if (!load(mpu, 0x8000, argv[0]))	pfail(argv[0]);
	    memcpy(bank[bankSel--],
		   0x8000 + mpu->memory,
		   0x4000);
	    n= 1;
	  }
	argc -= n;
	argv += n;
      }

  if (bTraps)
    doBtraps(0, 0, mpu);

  volatile unsigned long isns = 0, cycles = 0;

  if (!setjmp(exit_6502_env)){
    M6502_reset(mpu);
    if (countCycles){
      while (1){
	M6502_step(mpu);
	isns++;
	cycles += mpu->ticks;
	mpu->ticks = 0;
      }
    } else {
      M6502_run(mpu);
    }
  }
  if (countCycles){
    fprintf(stderr, "Instructions: %lu Cycles: %lu\n", isns, cycles);
  }
  M6502_delete(mpu);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, %r14
movl %edi, %r13d
xorl %r12d, %r12d
movl %r12d, %edi
movq %rdi, %rsi
movq %rdi, %rdx
callq 0xbc58
movq %rax, %rbx
movq (%r14), %rax
movq %rax, 0xcb11(%rip)      # 0x101e8
cmpl $0x2, %r13d
jne 0x36ea
movq 0x8(%r14), %rdx
cmpb $0x2d, (%rdx)
jne 0x3c8e
pushq $0x1
popq %rbp
leaq 0x30(%rsp), %r15
movl %r12d, %eax
movl %r12d, %ecx
movl %eax, 0x8(%rsp)
movl %r13d, %eax
cmpl $0x2, %eax
jl 0x3c81
movq %r14, %r13
movl %eax, %r14d
leaq 0x8(%r13), %rax
movq (%rax), %rdx
cmpb $0x2d, (%rdx)
movq %rax, 0x20(%rsp)
jne 0x383e
cmpb $0x42, 0x1(%rdx)
jne 0x3733
cmpb $0x0, 0x2(%rdx)
je 0x38b0
cmpb $0x63, 0x1(%rdx)
jne 0x3743
cmpb $0x0, 0x2(%rdx)
je 0x38bb
cmpb $0x64, 0x1(%rdx)
jne 0x3753
cmpb $0x0, 0x2(%rdx)
je 0x38c8
cmpb $0x47, 0x1(%rdx)
jne 0x3763
cmpb $0x0, 0x2(%rdx)
je 0x3904
cmpb $0x68, 0x1(%rdx)
jne 0x3773
cmpb $0x0, 0x2(%rdx)
je 0x3d6c
cmpb $0x69, 0x1(%rdx)
jne 0x3783
cmpb $0x0, 0x2(%rdx)
je 0x392b
cmpb $0x49, 0x1(%rdx)
jne 0x3793
cmpb $0x0, 0x2(%rdx)
je 0x3954
cmpb $0x6c, 0x1(%rdx)
jne 0x37a3
cmpb $0x0, 0x2(%rdx)
je 0x3abf
cmpb $0x4d, 0x1(%rdx)
jne 0x37b3
cmpb $0x0, 0x2(%rdx)
je 0x3afc
cmpb $0x4e, 0x1(%rdx)
jne 0x37c3
cmpb $0x0, 0x2(%rdx)
je 0x3b3b
cmpb $0x50, 0x1(%rdx)
jne 0x37d3
cmpb $0x0, 0x2(%rdx)
je 0x3b68
cmpb $0x52, 0x1(%rdx)
jne 0x37e3
cmpb $0x0, 0x2(%rdx)
je 0x3bb9
cmpb $0x73, 0x1(%rdx)
jne 0x37f3
cmpb $0x0, 0x2(%rdx)
je 0x3be6
cmpb $0x76, 0x1(%rdx)
jne 0x3803
cmpb $0x0, 0x2(%rdx)
je 0x3d90
cmpb $0x58, 0x1(%rdx)
jne 0x3d73
cmpb $0x0, 0x2(%rdx)
jne 0x3d73
cmpl $0x2, %r14d
je 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movq 0x10(%rbx), %rcx
movl %eax, %eax
leaq 0xa88(%rip), %rdx       # 0x42c1
jmp 0x3b8a
cmpl $0x0, 0x8(%rsp)
je 0x3d7f
cmpl $0x0, 0xc8b0(%rip)      # 0x10100
js 0x3d51
movl %ecx, %r13d
movq %rbx, %rdi
movl $0x8000, %esi           # imm = 0x8000
callq 0x3e4c
testl %eax, %eax
je 0x3d5f
movslq 0xc88b(%rip), %rax      # 0x10100
movq %rax, %rdi
decl %eax
movl %eax, 0xc880(%rip)      # 0x10100
shlq $0xe, %rdi
leaq 0xc965(%rip), %rax      # 0x101f0
addq %rax, %rdi
movq 0x8(%rbx), %rsi
movl $0x8000, %eax           # imm = 0x8000
addq %rax, %rsi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3140
movl 0x8(%rsp), %ecx
movl %r13d, %edx
jmp 0x3b9b
xorl %esi, %esi
movl %ecx, %edx
movl %ebp, %ecx
jmp 0x3b9d
xorl %esi, %esi
movl 0x8(%rsp), %ecx
movl %ebp, %edx
jmp 0x3b9d
movl %ecx, 0xc(%rsp)
movl %r14d, 0x10(%rsp)
cmpl $0x3, %r14d
jbe 0x3d7f
movq 0x10(%r13), %rdi
callq 0x41f7
movq %rax, %r14
movq 0x18(%r13), %rdi
cmpb $0x2b, (%rdi)
jne 0x3984
incq %rdi
callq 0x41f7
addq %r14, %rax
jmp 0x3989
cmpl $0x2, %r14d
je 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movq 0x10(%rbx), %rcx
movl %eax, %eax
leaq 0x907(%rip), %rdx       # 0x422d
jmp 0x3b8a
cmpl $0x3, %r14d
jbe 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movq 0x18(%r13), %rdx
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x3db5
jmp 0x3ae3
cmpl $0x2, %r14d
je 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movl %eax, %ecx
movq 0x8(%rbx), %rdx
movb %al, 0xfffe(%rdx)
movq 0x8(%rbx), %rax
movb %ch, 0xffff(%rax)
jmp 0x3b92
callq 0x41f7
movl %r14d, %r13d
movl %eax, 0x1c(%rsp)
cmpl 0x1c(%rsp), %r13d
jae 0x3aa7
movzwl %r13w, %esi
movq %rbx, %rdi
movq %r15, %rdx
callq 0xac77
movl %eax, %ebp
movl %r12d, %eax
leaq 0x86d3(%rip), %rdi      # 0xc089
movl %r13d, %esi
callq 0x30a0
testl %ebp, %ebp
movl %r12d, %eax
movl %ebp, 0x18(%rsp)
cmovgl %ebp, %eax
movl %eax, %r15d
movl %r13d, %eax
movl %eax, 0x14(%rsp)
movl %r12d, %r14d
movl %eax, %ebp
cmpq %r14, %r15
je 0x3a01
movq 0x8(%rbx), %rax
movl %r14d, %ecx
movl %ebp, %edx
addl %edx, %ecx
movzbl (%rax,%rcx), %esi
movl %r12d, %eax
leaq 0x936c(%rip), %rdi      # 0xcd63
callq 0x30a0
incq %r14
jmp 0x39d9
movl %r14d, %eax
incl %eax
leaq 0x8682(%rip), %r14      # 0xc08f
cmpl $0x4, %eax
jae 0x3a25
movl %eax, %ebp
incl %ebp
movl %r12d, %eax
movq %r14, %rdi
callq 0x30a0
movl %ebp, %eax
jmp 0x3a0d
movl 0x14(%rsp), %eax
movl %r12d, %ebp
movl %eax, %r14d
pushq $0x20
popq %rdi
callq 0x3040
cmpq %rbp, %r15
je 0x3a68
callq 0x31d0
movq (%rax), %rcx
movq 0x8(%rbx), %rax
movl %ebp, %edx
movl %r14d, %esi
addl %esi, %edx
movzbl (%rax,%rdx), %eax
movl %eax, %edx
cmpw $0x0, (%rcx,%rdx,2)
pushq $0x20
popq %rcx
jns 0x3a61
movl %eax, %ecx
movl %ecx, %edi
incq %rbp
jmp 0x3a32
movl %ebp, %eax
incl %eax
leaq 0x30(%rsp), %r15
cmpl $0x3, %eax
ja 0x3a86
movl %eax, %ebp
incl %ebp
pushq $0x20
popq %rdi
callq 0x3040
movl %ebp, %eax
jmp 0x3a71
movl %r12d, %eax
leaq 0x868b(%rip), %rdi      # 0xc11b
movq %r15, %rsi
callq 0x30a0
movl 0x18(%rsp), %eax
addl %r13d, %eax
movl %eax, %r13d
jmp 0x3990
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %edx
pushq $0x2
popq %rsi
pushq $0x1
popq %rbp
movl 0x10(%rsp), %r14d
jmp 0x3b9d
cmpl $0x3, %r14d
jbe 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movq 0x18(%r13), %rdx
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x3e4c
movl 0x8(%rsp), %ecx
movl %ebp, %edx
pushq $0x2
popq %rsi
testl %eax, %eax
pushq $0x1
popq %rbp
jne 0x3b9d
jmp 0x3d87
cmpl $0x2, %r14d
je 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movq 0x10(%rbx), %rcx
movl %eax, %eax
shlq $0x3, %rax
leaq 0x74d(%rip), %rdx       # 0x426f
movq %rdx, (%rcx,%rax)
movq 0x10(%rbx), %rcx
leaq 0x743(%rip), %rdx       # 0x4274
movq %rdx, 0x80000(%rcx,%rax)
jmp 0x3b92
cmpl $0x2, %r14d
je 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movl %eax, %ecx
movq 0x8(%rbx), %rdx
movb %al, 0xfffa(%rdx)
movq 0x8(%rbx), %rax
movb %ch, 0xfffb(%rax)
jmp 0x3b92
cmpl $0x2, %r14d
je 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movq 0x10(%rbx), %rcx
movl %eax, %eax
leaq 0x6f1(%rip), %rdx       # 0x427b
movq %rdx, 0x100000(%rcx,%rax,8)
movl 0x8(%rsp), %ecx
movl %ebp, %edx
pushq $0x1
popq %rbp
movl %ebp, %esi
movl %ecx, %eax
movl %esi, %ecx
notl %esi
movl %r14d, %r13d
addl %esi, %r13d
movq 0x20(%rsp), %rsi
leaq (%rsi,%rcx,8), %r14
movl %edx, %ecx
jmp 0x36f8
cmpl $0x2, %r14d
je 0x3d7f
movl %ecx, %ebp
movq 0x10(%r13), %rdi
callq 0x41f7
movl %eax, %ecx
movq 0x8(%rbx), %rdx
movb %al, 0xfffc(%rdx)
movq 0x8(%rbx), %rax
movb %ch, 0xfffd(%rax)
jmp 0x3b92
movl %ecx, 0xc(%rsp)
movl %r14d, 0x10(%rsp)
cmpl $0x4, %r14d
jbe 0x3d7f
movq 0x10(%r13), %rdi
callq 0x41f7
movq %rax, %r15
movq 0x18(%r13), %rdi
callq 0x41f7
movq %rax, %r14
movq 0x20(%r13), %rdi
leaq 0x8489(%rip), %rsi      # 0xc0a5
callq 0x3170
testq %rax, %rax
je 0x3da3
movq %r14, %rdx
movq %rax, %r14
movl %edx, %ebp
pushq $0x1
popq %r13
movzwl %r15w, %eax
movl %eax, %edi
addq 0x8(%rbx), %rdi
movl %ebp, %eax
movl %eax, %edx
movq %r13, %rsi
movq %r14, %rcx
callq 0x31c0
testl %eax, %eax
je 0x3c61
movl %eax, %ecx
movl %r15d, %edx
addl %eax, %edx
movl %edx, %r15d
subl %ecx, %ebp
jmp 0x3c36
movq %r14, %rdi
callq 0x3080
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %edx
pushq $0x3
popq %rsi
pushq $0x1
popq %rbp
leaq 0x30(%rsp), %r15
jmp 0x3ab5
movl %ecx, %ebp
movl %ecx, %eax
cmpl $0x0, 0x8(%rsp)
jne 0x3cb6
jmp 0x3cc0
xorl %ebp, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3db5
testl %eax, %eax
jne 0x3cb6
movq 0x8(%r14), %rdx
xorl %ebp, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x3e4c
testl %eax, %eax
je 0x3dac
movq %rbx, %rdi
callq 0x3ebf
movl %ebp, %eax
movl %eax, %ebp
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
leaq 0xc44b(%rip), %rdi      # 0x10120
callq 0x30f0
testl %eax, %eax
je 0x3cfb
testl %ebp, %ebp
jne 0x3d2d
movq %rbx, %rdi
callq 0xbd34
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x43c6
testl %ebp, %ebp
je 0x3d23
movq %rbx, %rdi
callq 0x7ef2
incq 0x30(%rsp)
movzbl 0x1c(%rbx), %eax
addq %rax, 0x28(%rsp)
movb $0x0, 0x1c(%rbx)
jmp 0x3d07
movq %rbx, %rdi
callq 0x43e5
jmp 0x3ce2
movq 0xc2c4(%rip), %rax      # 0xfff8
movq (%rax), %rdi
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rcx
leaq 0x8322(%rip), %rsi      # 0xc06a
xorl %eax, %eax
callq 0x3120
jmp 0x3ce2
leaq 0x8302(%rip), %rdi      # 0xc05a
xorl %eax, %eax
callq 0x32dc
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x3391
xorl %edi, %edi
callq 0x3fb5
cmpb $0x78, 0x1(%rdx)
jne 0x3d7f
cmpb $0x0, 0x2(%rdx)
je 0x3d9c
pushq $0x1
popq %rdi
callq 0x3fb5
movq 0x18(%r13), %rdi
callq 0x3391
leaq 0x8310(%rip), %rdi      # 0xc0a7
callq 0x3060
xorl %edi, %edi
callq 0x31b0
movq 0x20(%r13), %rdi
callq 0x3391
movq 0x8(%r14), %rdi
callq 0x3391