// File: /workspace/github_cpp_makefile_star2_O2/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, -0x38(%rbp)
movl %edi, -0x3c(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x196c4(%rip), %rdx     # 0x2a093
movl $0x2df5a4, %edi         # imm = 0x2DF5A4
movl $0x2df5a0, %esi         # imm = 0x2DF5A0
callq 0xb110
xorl %r15d, %r15d
leaq -0x60(%rbp), %r12
leaq -0x100(%rbp), %rbx
movl $0x0, -0x30(%rbp)
cmpl -0x3c(%rbp), %r15d
jge 0x10a9d
movslq %r15d, %r14
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %r13
movq %r12, %rdi
leaq 0x1968a(%rip), %rsi     # 0x2a09c
leaq -0x2a(%rbp), %rdx
callq 0x10d2c
movq %r13, %rdi
movq %r12, %rsi
callq 0x1102d
testb %al, %al
je 0x10a34
movq %r12, %rdi
callq 0xb9d8
jmp 0x10a76
movq -0x38(%rbp), %rax
movq (%rax,%r14,8), %r13
movq %rbx, %r12
movq %rbx, %rdi
leaq 0x19659(%rip), %rsi     # 0x2a0a2
leaq -0x29(%rbp), %rdx
callq 0x10d2c
movq %r13, %rdi
movq %r12, %rsi
callq 0x1102d
movl %eax, %r13d
movq %r12, %rdi
callq 0xb9d8
leaq -0x60(%rbp), %rdi
callq 0xb9d8
testb %r13b, %r13b
je 0x10a91
movq -0x38(%rbp), %rax
movq 0x8(%rax,%r14,8), %rsi
leaq -0x80(%rbp), %rdi
callq 0xb920
addl $0x2, %r15d
movb $0x1, %al
movl %eax, -0x30(%rbp)
leaq -0x60(%rbp), %r12
incl %r15d
jmp 0x109f3
testb $0x1, -0x30(%rbp)
je 0x10b79
leaq -0xe0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xb430
leaq -0xe0(%rbp), %rdi
callq 0x1063a
leaq -0xe0(%rbp), %rdi
callq 0xb9d8
movl $0x88, %edi
callq 0xb140
movq %rax, %r15
movq %rax, %rdi
callq 0x1b812
movq %r15, -0x60(%rbp)
leaq -0x80(%rbp), %rsi
movq %r15, %rdi
callq 0x1b446
testl %eax, %eax
jne 0x10b7e
leaq -0xa0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xb430
leaq -0xc0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1025b
leaq -0xc0(%rbp), %rdi
callq 0x10512
leaq -0xc0(%rbp), %rdi
callq 0xb9d8
leaq -0xa0(%rbp), %rdi
callq 0xb9d8
movq -0x60(%rbp), %rdi
callq 0x1b10c
leaq -0x60(%rbp), %rdi
callq 0x11046
xorl %ebx, %ebx
callq 0xb840
leaq -0x80(%rbp), %rdi
callq 0xb9d8
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1023c
movq 0x30463(%rip), %rdi     # 0x40fe8
leaq 0x19519(%rip), %rsi     # 0x2a0a5
callq 0xb450
leaq -0x80(%rbp), %rsi
movq %rax, %rdi
callq 0xb280
movq %rax, %rdi
callq 0xb7e0
pushq $0x1
popq %rdi
callq 0xb510
movq %rdx, %rbx
movq %rax, %r14
leaq -0xc0(%rbp), %rdi
callq 0xb9d8
jmp 0x10bc7
movq %rdx, %rbx
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0xb9d8
jmp 0x10c0e
movq %rdx, %rbx
movq %rax, %r14
movl $0x88, %esi
movq %r15, %rdi
callq 0xb0f0
jmp 0x10c17
jmp 0x10c00
movq %rdx, %rbx
movq %rax, %r14
leaq -0xe0(%rbp), %rdi
callq 0xb9d8
jmp 0x10c17
movq %rdx, %rbx
movq %rax, %r14
jmp 0x10c17
movq %rdx, %rbx
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x11046
movq %r14, %rdi
cmpl $0x3, %ebx
jne 0x10c60
callq 0xb060
movq %rax, %rbx
movq 0x303ba(%rip), %rdi     # 0x40fe8
leaq 0x19445(%rip), %rsi     # 0x2a07a
callq 0xb450
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
callq 0xb5d0
jmp 0x10cc6
callq 0xb060
cmpl $0x2, %ebx
jne 0x10ca6
movq %rax, %r14
movq 0x30374(%rip), %rdi     # 0x40fe8
leaq 0x193ff(%rip), %rsi     # 0x2a07a
callq 0xb450
movq %rax, %rbx
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
callq 0xb5d0
jmp 0x10cc6
movq 0x3033b(%rip), %rdi     # 0x40fe8
leaq 0x19406(%rip), %rsi     # 0x2a0ba
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
callq 0xb5d0
pushq $-0x1
popq %rbx
jmp 0x10b5c
jmp 0x10d0a
movq %rax, %rbx
callq 0xb5d0
jmp 0x10d0d
jmp 0x10d0a
movq %rax, %rbx
callq 0xb5d0
jmp 0x10d0d
movq %rax, %rbx
callq 0xb5d0
jmp 0x10d0d
movq %rax, %rdi
callq 0x10d1e
jmp 0x10d0a
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0xb9d8
jmp 0x10d0d
jmp 0x10d0a
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0xb9d8
movq %rbx, %rdi
callq 0xb4e0