// File: /workspace/github_cpp_makefile_star2_O3/mindbeast[P]rnes/main.cpp
// Function: main
int main(int argc, char *argv[]) {
  using namespace std;
  namespace fs = boost::filesystem;
  string romFile;
  bool romFileSpecified = false;

  // Verify that the version of the library that we linked against is
  // compatible with the version of the headers we compiled against.
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  // TODO: convert argument parsing to boost program options
  for (int i = 0; i < argc; i++) {
    if (argv[i] == string("--rom") || argv[i] == string("-r")) {
      romFile = argv[i + 1];
      romFileSpecified = true;
      i += 2;
    }
  }

  if (!romFileSpecified) {
    displayHelpAndQuit();
  }

  try {
    // verify the rom file exists.
    verifyRomExists(romFile);

    auto nes = unique_ptr<Nes>{new Nes{}};
    int res = nes->loadRom(romFile);
    if (res) {
      cerr << "Failed to load rom: " << romFile << endl;
      exit(1);
    }

    // Setup the rnes directories.
    setupDirectories(md5OfFile(romFile));

    // Start the emulator loop.
    nes->run();
  } catch (const fs::filesystem_error &exception) {
    cerr << "Exception: " << exception.what() << endl;
    return -1;
  } catch (const std::exception &exception) {
    cerr << "Exception: " << exception.what() << endl;
    return -1;
  } catch (...) {
    cerr << "Unknown Exception!" << endl;
    return -1;
  }

  google::protobuf::ShutdownProtobufLibrary();
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r14
movl %edi, %r15d
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x1e262(%rip), %rdx     # 0x33093
movl $0x2df5a4, %edi         # imm = 0x2DF5A4
movl $0x2df5a0, %esi         # imm = 0x2DF5A0
callq 0xa100
testl %r15d, %r15d
jle 0x150b1
xorl %eax, %eax
leaq -0x70(%rbp), %r12
leaq -0x100(%rbp), %r13
xorl %ebx, %ebx
movq %r14, -0x78(%rbp)
movl %r15d, -0x2c(%rbp)
movq %rax, -0x80(%rbp)
movslq %ebx, %rax
leaq (%r14,%rax,8), %r14
movq (%r14), %r15
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movq %r12, %rdi
leaq 0x1e21c(%rip), %rsi     # 0x3309c
leaq 0x1e21a(%rip), %rdx     # 0x330a1
callq 0x17200
movq %r12, %rdi
movq %r15, %rsi
callq 0xa6f0
testl %eax, %eax
je 0x14efe
movq %r13, %r12
movq (%r14), %r15
leaq -0xf0(%rbp), %r13
movq %r13, -0x100(%rbp)
movq %r12, %rdi
leaq 0x1e1e9(%rip), %rsi     # 0x330a2
leaq 0x1e1e4(%rip), %rdx     # 0x330a4
callq 0x17200
movq %r12, %rdi
movq %r15, %rsi
callq 0xa6f0
testl %eax, %eax
sete %r15b
movq -0x100(%rbp), %rdi
cmpq %r13, %rdi
je 0x14ef1
movq -0xf0(%rbp), %rsi
incq %rsi
callq 0xa0e0
leaq -0x70(%rbp), %r12
leaq -0x100(%rbp), %r13
jmp 0x14f01
movb $0x1, %r15b
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x14f1a
movq -0x60(%rbp), %rsi
incq %rsi
callq 0xa0e0
testb %r15b, %r15b
jne 0x14f34
incl %ebx
addq $0x8, %r14
movl -0x2c(%rbp), %r15d
cmpl %r15d, %ebx
jl 0x14e6b
jmp 0x14f70
movq 0x8(%r14), %r15
movq -0x48(%rbp), %r14
movq %r15, %rdi
callq 0xa330
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0xa3d0
addl $0x3, %ebx
movb $0x1, %al
movl -0x2c(%rbp), %r15d
cmpl %r15d, %ebx
movq -0x78(%rbp), %r14
jl 0x14e60
jmp 0x14f7a
testb $0x1, -0x80(%rbp)
je 0x150b1
leaq -0xb0(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %rdi
callq 0x17162
leaq -0xc0(%rbp), %rdi
callq 0x1491d
movq -0xc0(%rbp), %rdi
cmpq %r15, %rdi
je 0x14fc3
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0xa0e0
movl $0x88, %edi
callq 0xa130
movq %rax, %r15
movq %rax, %rdi
callq 0x213a4
movq %r15, -0x70(%rbp)
leaq -0x50(%rbp), %rsi
movq %r15, %rdi
callq 0x20db4
testl %eax, %eax
jne 0x150ce
leaq -0x90(%rbp), %r15
movq %r15, -0x10(%r15)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %rdi
callq 0x17162
leaq -0xe0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1425d
leaq -0xe0(%rbp), %rdi
callq 0x14702
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15050
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0xa0e0
movq -0xa0(%rbp), %rdi
cmpq %r15, %rdi
je 0x1506b
movq -0x90(%rbp), %rsi
incq %rsi
callq 0xa0e0
movq -0x70(%rbp), %rdi
callq 0x20916
leaq -0x70(%rbp), %rdi
callq 0x1560e
xorl %ebx, %ebx
callq 0xa860
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1509d
movq -0x40(%rbp), %rsi
incq %rsi
callq 0xa0e0
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x31f38(%rip), %rdi     # 0x46ff0
leaq 0x326d1(%rip), %rsi     # 0x47790
callq 0xa250
movl $0x1, %edi
callq 0xa4f0
movq 0x31f1b(%rip), %rdi     # 0x46ff0
leaq 0x1dfc9(%rip), %rsi     # 0x330a5
movl $0x14, %edx
callq 0xa7a0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x31efb(%rip), %rdi     # 0x46ff0
callq 0xa7a0
movq %rax, %rdi
callq 0xa800
jmp 0x150c4
jmp 0x152c7
movq %rdx, %rbx
movq %rax, %r14
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x15136
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0xa0e0
jmp 0x15136
movq %rdx, %rbx
movq %rax, %r14
movq -0xa0(%rbp), %rdi
cmpq %r15, %rdi
je 0x15196
movq -0x90(%rbp), %rsi
incq %rsi
callq 0xa0e0
jmp 0x15196
movq %rdx, %rbx
movq %rax, %r14
movl $0x88, %esi
movq %r15, %rdi
jmp 0x15181
jmp 0x15188
movq %rdx, %rbx
movq %rax, %r14
movq -0xc0(%rbp), %rdi
cmpq %r15, %rdi
je 0x1519f
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0xa0e0
jmp 0x1519f
movq %rdx, %rbx
movq %rax, %r14
jmp 0x1519f
movq %rdx, %rbx
movq %rax, %r14
leaq -0x70(%rbp), %rdi
callq 0x1560e
movq %r14, %rdi
cmpl $0x3, %ebx
jne 0x151f6
callq 0xa060
movq %rax, %rbx
movq 0x31e3a(%rip), %rdi     # 0x46ff0
leaq 0x1debd(%rip), %rsi     # 0x3307a
movl $0xb, %edx
callq 0xa7a0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x31e19(%rip), %rdi     # 0x46ff0
movq %rax, %rsi
callq 0xa430
movq %rax, %rdi
callq 0xa800
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0xa5e0
jmp 0x15084
callq 0xa060
cmpl $0x2, %ebx
jne 0x1524a
movq %rax, %r14
movq 0x31de6(%rip), %rdi     # 0x46ff0
leaq 0x1de69(%rip), %rsi     # 0x3307a
movl $0xb, %edx
callq 0xa7a0
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq 0x31dc5(%rip), %rdi     # 0x46ff0
movq %rax, %rsi
callq 0xa430
movq %rax, %rdi
callq 0xa800
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0xa5e0
jmp 0x15084
movq 0x31d9f(%rip), %rdi     # 0x46ff0
leaq 0x1de62(%rip), %rsi     # 0x330ba
movl $0x12, %edx
callq 0xa7a0
movq 0x31d87(%rip), %rdi     # 0x46ff0
callq 0xa800
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
callq 0xa5e0
jmp 0x15084
jmp 0x152c7
movq %rax, %rbx
callq 0xa5e0
jmp 0x152ca
jmp 0x152c7
movq %rax, %rbx
callq 0xa5e0
jmp 0x152ca
movq %rax, %rbx
callq 0xa5e0
jmp 0x152ca
movq %rax, %rdi
callq 0x152eb
jmp 0x152c7
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x152ca
movq -0x60(%rbp), %rsi
incq %rsi
callq 0xa0e0
jmp 0x152ca
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x152e3
movq -0x40(%rbp), %rsi
incq %rsi
callq 0xa0e0
movq %rbx, %rdi
callq 0xa4d0