// File: /workspace/github_c_makefile_star2_O3/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, %r15d
xorl %ebp, %ebp
movl %ebp, %edi
movq %rdi, %rsi
movq %rdi, %rdx
callq 0xd56e
movq %rax, %rbx
movq (%r14), %rax
movq %rax, 0x9c10(%rip)      # 0x121e8
cmpl $0x2, %r15d
jne 0x861b
movq 0x8(%r14), %rdx
cmpb $0x2d, (%rdx)
je 0x8621
movq 0x8(%rbx), %rdi
xorl %ebp, %ebp
movl %ebp, %esi
callq 0x8d86
testl %eax, %eax
jne 0x8c88
movq 0x8(%r14), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8e21
testl %eax, %eax
jne 0x8c88
movq 0x8(%r14), %rdi
callq 0x82f5
jl 0x8c90
xorl %r13d, %r13d
leaq 0xc7d(%rip), %r12       # 0x92a8
movl %r13d, %eax
movl %r13d, %edx
movl %eax, %ebp
addq $0x8, %r14
movl %edx, %ecx
movl %r15d, %edi
movl %ecx, %r15d
movq (%r14), %rdx
cmpb $0x2d, (%rdx)
jne 0x8779
cmpb $0x42, 0x1(%rdx)
jne 0x8671
cmpb $0x0, 0x2(%rdx)
jne 0x8671
movl %edi, %esi
decl %esi
addq $0x8, %r14
movl $0x1, %ecx
movl %esi, %r15d
cmpl $0x1, %esi
ja 0x8639
jmp 0x8c84
cmpb $0x63, 0x1(%rdx)
jne 0x8681
cmpb $0x0, 0x2(%rdx)
je 0x87e4
cmpb $0x64, 0x1(%rdx)
jne 0x8691
cmpb $0x0, 0x2(%rdx)
je 0x87f0
cmpb $0x47, 0x1(%rdx)
jne 0x86a1
cmpb $0x0, 0x2(%rdx)
je 0x8832
cmpb $0x68, 0x1(%rdx)
jne 0x86b1
cmpb $0x0, 0x2(%rdx)
je 0x8d3f
cmpb $0x69, 0x1(%rdx)
jne 0x86c1
cmpb $0x0, 0x2(%rdx)
je 0x885b
cmpb $0x49, 0x1(%rdx)
jne 0x86d1
cmpb $0x0, 0x2(%rdx)
je 0x8887
cmpb $0x6c, 0x1(%rdx)
jne 0x86e1
cmpb $0x0, 0x2(%rdx)
je 0x8a6a
cmpb $0x4d, 0x1(%rdx)
jne 0x86f1
cmpb $0x0, 0x2(%rdx)
je 0x8aad
cmpb $0x4e, 0x1(%rdx)
jne 0x8701
cmpb $0x0, 0x2(%rdx)
je 0x8ae7
cmpb $0x50, 0x1(%rdx)
jne 0x8711
cmpb $0x0, 0x2(%rdx)
je 0x8b16
cmpb $0x52, 0x1(%rdx)
jne 0x8721
cmpb $0x0, 0x2(%rdx)
je 0x8b74
cmpb $0x73, 0x1(%rdx)
jne 0x8731
cmpb $0x0, 0x2(%rdx)
je 0x8ba3
cmpb $0x76, 0x1(%rdx)
jne 0x8741
cmpb $0x0, 0x2(%rdx)
je 0x8d65
cmpb $0x58, 0x1(%rdx)
jne 0x8d46
cmpb $0x0, 0x2(%rdx)
jne 0x8d46
movl %ebp, %r13d
movl %edi, %eax
movl %edi, %ebp
leal -0x1(%rax), %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x8f80
movl %ebp, %edi
movl $0x1, %edx
movl %r13d, %ecx
jmp 0x8b4c
testl %r15d, %r15d
je 0x8d52
cmpl $0x0, 0x9977(%rip)      # 0x12100
js 0x8d31
movl %ebp, %r13d
movl %edi, %ebp
movq %rbx, %rdi
movl $0x8000, %esi           # imm = 0x8000
callq 0x8e21
testl %eax, %eax
je 0x8d29
movslq 0x9950(%rip), %rax      # 0x12100
movq %rax, %rdi
decl %eax
movl %eax, 0x9945(%rip)      # 0x12100
shlq $0xe, %rdi
leaq 0x9a2a(%rip), %rax      # 0x121f0
addq %rax, %rdi
movq 0x8(%rbx), %rsi
movl $0x8000, %eax           # imm = 0x8000
addq %rax, %rsi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3140
jmp 0x8b42
xorl %edx, %edx
movl $0x1, %ecx
jmp 0x8b4f
movl %r15d, 0xc(%rsp)
movl %edi, 0x8(%rsp)
cmpl $0x3, %edi
leaq 0x588c(%rip), %r15      # 0xe08f
jbe 0x8d52
movq 0x8(%r14), %rdi
callq 0x9229
movq %rax, %r12
movq 0x10(%r14), %rdi
cmpb $0x2b, (%rdi)
jne 0x88b9
incq %rdi
callq 0x9229
addq %r12, %rax
jmp 0x88be
cmpl $0x2, %edi
je 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movq 0x10(%rbx), %rcx
movl %eax, %eax
leaq 0xa0b(%rip), %rdx       # 0x9261
jmp 0x8b3a
cmpl $0x3, %edi
jbe 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movq 0x10(%r14), %rdx
movq 0x8(%rbx), %rdi
movzwl %ax, %esi
callq 0x8d86
jmp 0x8a90
cmpl $0x2, %edi
je 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movl %eax, %ecx
movq 0x8(%rbx), %rdx
movb %al, 0xfffe(%rdx)
movq 0x8(%rbx), %rax
movb %ch, 0xffff(%rax)
jmp 0x8b42
callq 0x9229
movq %rax, %rcx
movl $0x2, %edx
movl %ecx, 0x24(%rsp)
cmpl %ecx, %r12d
jae 0x8a57
movl %ebp, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movl %r12d, %r14d
leaq 0x30(%rsp), %r12
movzwl %r14w, %esi
movq %rbx, %rdi
movq %r12, %rdx
callq 0xc573
movl %eax, %r12d
movl %eax, %eax
movl %eax, %ebp
movl %r13d, %eax
leaq 0x5785(%rip), %rdi      # 0xe089
movl %r14d, %esi
callq 0x30a0
movl %r14d, 0x20(%rsp)
movl %r14d, 0x14(%rsp)
testl %r12d, %r12d
jle 0x8964
movl 0x14(%rsp), %eax
movl %r13d, %ecx
movl %eax, %r14d
movq %rcx, %rax
leaq 0x6434(%rip), %r12      # 0xed63
movq %rax, %r15
movq 0x8(%rbx), %rax
movl %r15d, %ecx
movl %r14d, %edx
addl %edx, %ecx
movzbl (%rax,%rcx), %esi
movl %r13d, %eax
movq %r12, %rdi
callq 0x30a0
incq %r15
movq %r15, %rax
cmpq %r15, %rbp
jne 0x892f
cmpl $0x3, %ebp
leaq 0x572d(%rip), %r15      # 0xe08f
jae 0x8987
testl %ebp, %ebp
movl %r13d, %ecx
cmovgl %ebp, %ecx
addl $-0x3, %ecx
movl %ecx, %eax
movl %eax, %r14d
movl %r13d, %eax
movq %r15, %rdi
callq 0x30a0
incl %r14d
movl %r14d, %eax
jne 0x8971
movq %rbp, %r12
movl $0x20, %r13d
movl %r13d, %edi
callq 0x3040
testl %ebp, %ebp
jle 0x89fa
callq 0x31d0
movq %rax, %r13
xorl %ecx, %ecx
movl 0x14(%rsp), %eax
movl %ecx, %r15d
movl %eax, %r14d
movq (%r13), %rcx
movq 0x8(%rbx), %rax
movl %r15d, %edx
movl %r14d, %esi
addl %esi, %edx
movzbl (%rax,%rdx), %eax
movl %eax, %edx
cmpw $0x0, (%rcx,%rdx,2)
movl $0x20, %edi
jns 0x89d4
movl %eax, %edi
callq 0x3040
movq %r15, %rax
incq %rax
movq %rax, %r15
cmpq %rax, %r12
jne 0x89b0
cmpl $0x3, %r12d
leaq 0x569d(%rip), %r15      # 0xe08f
movl $0x20, %r13d
jae 0x8a1e
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
addl $-0x3, %ecx
movl %ecx, %eax
movl %eax, %r14d
movl %r13d, %edi
callq 0x3040
incl %r14d
movl %r14d, %eax
jne 0x8a0b
xorl %r13d, %r13d
movl %r13d, %eax
leaq 0x56f0(%rip), %rdi      # 0xe11b
leaq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x30a0
addl 0x20(%rsp), %ebp
movl %ebp, %r14d
cmpl 0x24(%rsp), %ebp
jb 0x88e4
movl 0x10(%rsp), %ecx
movl $0x2, %edx
jmp 0x8c67
movl %ebp, %ecx
leaq 0x848(%rip), %r12       # 0x92a8
movl 0xc(%rsp), %r15d
jmp 0x8c7b
cmpl $0x3, %edi
jbe 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movq 0x10(%r14), %rdx
movzwl %ax, %esi
movq %rbx, %rdi
callq 0x8e21
movl $0x2, %edx
movl %r13d, %ecx
testl %eax, %eax
movl %ebp, %edi
movl $0x0, %r13d
jne 0x8b4f
jmp 0x8d5c
cmpl $0x2, %edi
je 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movq 0x10(%rbx), %rcx
movl %eax, %eax
shlq $0x3, %rax
leaq 0x7ce(%rip), %rdx       # 0x92a3
movq %rdx, (%rcx,%rax)
movq 0x10(%rbx), %rcx
movq %r12, 0x80000(%rcx,%rax)
jmp 0x8b42
cmpl $0x2, %edi
je 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movl %eax, %ecx
movq 0x8(%rbx), %rdx
movb %al, 0xfffa(%rdx)
movq 0x8(%rbx), %rax
movb %ch, 0xfffb(%rax)
jmp 0x8b42
cmpl $0x2, %edi
je 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movq 0x10(%rbx), %rcx
movl %eax, %eax
leaq 0x775(%rip), %rdx       # 0x92af
movq %rdx, 0x100000(%rcx,%rax,8)
movl $0x1, %edx
movl %r13d, %ecx
movl %ebp, %edi
xorl %r13d, %r13d
movl %edx, %eax
notl %edx
movl %edx, %esi
addl %edi, %esi
leaq (%r14,%rax,8), %r14
movl %ecx, %eax
movl %r15d, %edx
movl %r15d, %edi
movl %esi, %r15d
cmpl $0x1, %esi
jg 0x8631
jmp 0x8cfb
cmpl $0x2, %edi
je 0x8d52
movl %ebp, %r13d
movl %edi, %ebp
movq 0x8(%r14), %rdi
callq 0x9229
movl %eax, %ecx
movq 0x8(%rbx), %rdx
movb %al, 0xfffc(%rdx)
movq 0x8(%rbx), %rax
movb %ch, 0xfffd(%rax)
jmp 0x8b42
movl %ebp, 0x10(%rsp)
movl %edi, 0x8(%rsp)
cmpl $0x4, %edi
jbe 0x8d52
movl %r15d, 0xc(%rsp)
movq 0x8(%r14), %rdi
callq 0x9229
movq %rax, %r15
movq 0x10(%r14), %rdi
callq 0x9229
movq %rax, %r12
movq %r14, 0x18(%rsp)
movq 0x18(%r14), %rdi
leaq 0x54c4(%rip), %rsi      # 0xe0a5
callq 0x3170
testq %rax, %rax
movl $0x1, %esi
je 0x8d78
movq %r15, %r14
movq %r12, %r13
movq %rax, %r12
movzwl %r14w, %eax
movl %eax, %edi
addq 0x8(%rbx), %rdi
movl %r13d, %eax
movl %eax, %edx
movq %r12, %rcx
callq 0x31c0
testl %eax, %eax
movl $0x1, %r15d
je 0x8c56
movl %r14d, %esi
movq %rax, %rcx
movl %r13d, %edx
movl %edx, %ebp
subl %eax, %ebp
movl %ecx, %eax
movl %esi, %ecx
addl %eax, %ecx
movl %ecx, %r14d
movzwl %cx, %eax
movl %eax, %edi
addq 0x8(%rbx), %rdi
movl %ebp, %edx
movq %r15, %rsi
movq %r12, %rcx
callq 0x31c0
movq %rax, %rcx
movl %ebp, %edx
movl %r14d, %esi
testl %eax, %eax
jne 0x8c27
movq %r12, %rdi
callq 0x3080
movl $0x3, %edx
movl 0x10(%rsp), %ecx
xorl %r13d, %r13d
leaq 0x637(%rip), %r12       # 0x92a8
movl 0xc(%rsp), %r15d
movq 0x18(%rsp), %r14
movl 0x8(%rsp), %edi
jmp 0x8b4f
movl %ebp, %eax
movl %eax, %ebp
movq %rbx, %rdi
callq 0x8e95
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
leaq 0x947d(%rip), %rdi      # 0x12120
callq 0x30f0
testl %eax, %eax
je 0x8cc9
testl %ebp, %ebp
jne 0x8d05
movq %rbx, %rdi
callq 0xd65c
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x93fa
testl %ebp, %ebp
je 0x8cf1
movq %rbx, %rdi
callq 0x9429
incq 0x30(%rsp)
movzbl 0x1c(%rbx), %eax
addq %rax, 0x28(%rsp)
movb $0x0, 0x1c(%rbx)
jmp 0x8cd5
movq %rbx, %rdi
callq 0x327e
jmp 0x8cb0
movl %ecx, %eax
movl %ecx, %ebp
testl %edi, %edi
jne 0x8c86
jmp 0x8c90
movq 0x92ec(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
movq 0x30(%rsp), %rdx
movq 0x28(%rsp), %rcx
leaq 0x534a(%rip), %rsi      # 0xe06a
xorl %eax, %eax
callq 0x3120
jmp 0x8cb0
movq (%r14), %rdi
callq 0x82f5
leaq 0x5322(%rip), %rdi      # 0xe05a
xorl %eax, %eax
callq 0x823c
xorl %edi, %edi
callq 0x8fb3
cmpb $0x78, 0x1(%rdx)
jne 0x8d52
cmpb $0x0, 0x2(%rdx)
je 0x8d71
movl $0x1, %edi
callq 0x8fb3
movq 0x10(%r14), %rdi
callq 0x82f5
leaq 0x533b(%rip), %rdi      # 0xe0a7
callq 0x3060
xorl %edi, %edi
callq 0x31b0
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0x82f5