// File: rfotino[P]consolite-emulator/src/emu.cpp
// Function: main
int main(int argc, char **argv) {
  if (2 != argc && 3 != argc) {
    usage(argv[0]);
    return 1;
  }

  std::string keymap;
  if (3 == argc) {
    keymap = argv[2];
  } else {
    keymap = DEFAULT_KEYMAP_FILENAME;
  }
  
  EmuVideoMemory vidMem;
  EmuWindow window(&vidMem, keymap);
  EmuProcessor processor(&window, argv[1]);
  if (window.hasError() || processor.hasError()) {
    return 1;
  }

  // Start up separate threads for the UI and processor
  std::thread winThread(win_thread_start, &window);
  std::thread procThread(proc_thread_start, &processor);

  // Join the threads
  winThread.join();
  processor.setRunning(false);
  procThread.join();
  
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x101c0, %rsp          # imm = 0x101C0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x2, %eax
cmpl -0x8(%rbp), %eax
je 0x38e6
movl $0x3, %eax
cmpl -0x8(%rbp), %eax
je 0x38e6
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x101a0(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x10198(%rbp)
callq 0x3530
movq -0x101a0(%rbp), %rsi
movq -0x10198(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x3bb0
jmp 0x388c
leaq -0x30(%rbp), %rdi
callq 0x3780
jmp 0x3897
leaq -0x30(%rbp), %rdi
callq 0x3190
leaq -0x31(%rbp), %rdi
callq 0x3320
movl $0x1, -0x4(%rbp)
jmp 0x3b94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x38d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x3190
leaq -0x31(%rbp), %rdi
callq 0x3320
jmp 0x3ba0
leaq -0x68(%rbp), %rdi
callq 0x3410
movl $0x3, %eax
cmpl -0x8(%rbp), %eax
jne 0x391f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq -0x68(%rbp), %rdi
callq 0x33d0
jmp 0x390c
jmp 0x3933
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3b89
leaq 0x66f7(%rip), %rsi      # 0xa01d
leaq -0x68(%rbp), %rdi
callq 0x33d0
jmp 0x3931
jmp 0x3933
leaq -0x78(%rbp), %rdi
callq 0x4c50
jmp 0x393e
leaq -0x110(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x4de0
jmp 0x3954
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x101b0(%rbp)
leaq -0x10169(%rbp), %rdi
movq %rdi, -0x101a8(%rbp)
callq 0x3530
movq -0x101b0(%rbp), %rsi
movq -0x101a8(%rbp), %rdx
leaq -0x10168(%rbp), %rdi
callq 0x3bb0
jmp 0x3992
leaq -0x10148(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0x10168(%rbp), %rdx
callq 0x8590
jmp 0x39ae
leaq -0x10168(%rbp), %rdi
callq 0x3190
leaq -0x10169(%rbp), %rdi
callq 0x3320
leaq -0x110(%rbp), %rdi
callq 0x3c60
movb %al, -0x101b1(%rbp)
jmp 0x39da
movb -0x101b1(%rbp), %al
testb $0x1, %al
jne 0x3a06
jmp 0x39e6
leaq -0x10148(%rbp), %rdi
callq 0x3c80
movb %al, -0x101b2(%rbp)
jmp 0x39fa
movb -0x101b2(%rbp), %al
testb $0x1, %al
jne 0x3a06
jmp 0x3a75
movl $0x1, -0x4(%rbp)
movl $0x1, -0x10170(%rbp)
jmp 0x3b54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3b80
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3a53
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x10168(%rbp), %rdi
callq 0x3190
leaq -0x10169(%rbp), %rdi
callq 0x3320
jmp 0x3b74
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3b74
leaq -0x110(%rbp), %rax
movq %rax, -0x10180(%rbp)
leaq -0x2aa(%rip), %rsi      # 0x37e0
leaq -0x10178(%rbp), %rdi
leaq -0x10180(%rbp), %rdx
callq 0x3ca0
jmp 0x3a9f
leaq -0x10148(%rbp), %rax
movq %rax, -0x10190(%rbp)
leaq -0x2b4(%rip), %rsi      # 0x3800
leaq -0x10188(%rbp), %rdi
leaq -0x10190(%rbp), %rdx
callq 0x3d60
jmp 0x3ac9
leaq -0x10178(%rbp), %rdi
callq 0x34d0
jmp 0x3ad7
leaq -0x10148(%rbp), %rdi
xorl %esi, %esi
callq 0x3e20
jmp 0x3ae7
leaq -0x10188(%rbp), %rdi
callq 0x34d0
jmp 0x3af5
movl $0x0, -0x4(%rbp)
movl $0x1, -0x10170(%rbp)
leaq -0x10188(%rbp), %rdi
callq 0x3e50
leaq -0x10178(%rbp), %rdi
callq 0x3e50
jmp 0x3b54
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x3b46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x10188(%rbp), %rdi
callq 0x3e50
leaq -0x10178(%rbp), %rdi
callq 0x3e50
jmp 0x3b74
leaq -0x110(%rbp), %rdi
callq 0x54b0
leaq -0x78(%rbp), %rdi
callq 0x4cb0
leaq -0x68(%rbp), %rdi
callq 0x3190
jmp 0x3b94
leaq -0x110(%rbp), %rdi
callq 0x54b0
leaq -0x78(%rbp), %rdi
callq 0x4cb0
leaq -0x68(%rbp), %rdi
callq 0x3190
jmp 0x3ba0
movl -0x4(%rbp), %eax
addq $0x101c0, %rsp          # imm = 0x101C0
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3520
nopl (%rax)