// File: /workspace/github_cpp_makefile_star2_O2/mindbeast[P]rnes/nes.cpp
// Function: Rnes::Nes::loadRom(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
int Nes::loadRom(const std::string &filename) {
  int result = mapRom(filename);
  if (result == -1) {
    return -1;
  }
  romFile = filename;
  const NesHeader *header = (const NesHeader *)rom;
  std::cerr << "Loading rom.. " << std::endl;
  std::cerr << "rom banks: " << (int)header->numRomBanks << std::endl;
  std::cerr << "vrom banks: " << (int)header->numVRomBanks << std::endl;
  std::cerr << "ram banks: " << (int)header->numPrgRamBanks << std::endl;
  if (header->info[0] & (1 << 2)) {
    std::cerr << "trainer present" << std::endl;
  }
  uint8_t mapper = (header->info[0] & 0xf0) >> 4;
  mapper |= (header->info[1] & 0xf0);
  std::cerr << "mapper: " << (int)mapper << std::endl;

  std::vector<uint8_t *> prgRoms;
  std::vector<uint8_t *> chrRoms;

  uint8_t *prgRomBase = (uint8_t *)rom + sizeof(NesHeader);
  for (int i = 0; i < header->numRomBanks; i++) {
    std::cerr << "loading rom section " << std::endl;
    uint8_t *prgRom = prgRomBase + i * prgRomSize;
    prgRoms.push_back(prgRom);
  }

  uint8_t *chrRomBase = prgRomBase + header->numRomBanks * prgRomSize;
  for (int i = 0; i < header->numVRomBanks; i++) {
    uint8_t *chrRom = chrRomBase + i * chrRomSize;
    chrRoms.push_back(chrRom);
  }

  bool verticalMirroring = (header->info[0] & 1) != 0;
  switch (mapper) {
  case 0: {
    std::cout << "Loading no mmc game." << std::endl;
    std::unique_ptr<Mmc> mmcLocal(
        new MmcNone(prgRoms, chrRoms, header->numPrgRamBanks, verticalMirroring));
    mmc = std::move(mmcLocal);
    break;
  }
  case 1: {
    std::cout << "Loading MMC1 game." << std::endl;
    std::unique_ptr<Mmc> mmcLocal(new Mmc1(prgRoms, chrRoms, header->numPrgRamBanks,
                                           verticalMirroring, cpuMemory.get(), videoMemory.get()));
    mmc = std::move(mmcLocal);
    break;
  }
  case 4: {
    std::cout << "Loading MMC3 game." << std::endl;
    std::unique_ptr<Mmc> mmcLocal(new Mmc3(prgRoms, chrRoms, header->numPrgRamBanks,
                                           verticalMirroring, cpuMemory.get(), videoMemory.get()));
    mmc = std::move(mmcLocal);
    break;
  }
  default:
    std::cout << "Unsupported mapper: " << std::hex << mapper << std::endl;
    assert(0);
    break;
  }
  cpuMemory->setMmc(mmc.get());
  videoMemory->setMmc(mmc.get());
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %rbx
movq %rdi, %r14
callq 0x1b3ba
cmpl $-0x1, %eax
je 0x1b681
leaq 0x10(%r14), %rdi
movq %rbx, %rsi
callq 0xb8e0
movq (%r14), %r15
movq 0x25b67(%rip), %rbx     # 0x40fe8
leaq 0x10194(%rip), %rsi     # 0x2b61c
movq %rbx, %rdi
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
leaq 0x1018d(%rip), %rsi     # 0x2b62c
movq %rbx, %rdi
callq 0xb450
movzbl 0x4(%r15), %esi
movq %rax, %rdi
callq 0xb2c0
movq %rax, %rdi
callq 0xb7e0
leaq 0x10168(%rip), %rsi     # 0x2b62b
movq %rbx, %rdi
callq 0xb450
movzbl 0x5(%r15), %esi
movq %rax, %rdi
callq 0xb2c0
movq %rax, %rdi
callq 0xb7e0
leaq 0x10151(%rip), %rsi     # 0x2b638
movq %rbx, %rdi
callq 0xb450
movzbl 0x8(%r15), %esi
movq %rax, %rdi
callq 0xb2c0
movq %rax, %rdi
callq 0xb7e0
movb 0x6(%r15), %al
testb $0x4, %al
je 0x1b52b
movq 0x25ad5(%rip), %rdi     # 0x40fe8
leaq 0x1012a(%rip), %rsi     # 0x2b644
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
movb 0x6(%r15), %al
movzbl %al, %eax
shrl $0x4, %eax
movq %r15, %r13
movzbl 0x7(%r15), %ebx
andl $-0x10, %ebx
orl %eax, %ebx
movq 0x25aa3(%rip), %rdi     # 0x40fe8
leaq 0x10164(%rip), %rsi     # 0x2b6b0
callq 0xb450
movq %rax, %rdi
movl %ebx, -0x2c(%rbp)
movl %ebx, %esi
callq 0xb2c0
movq %rax, %rdi
callq 0xb7e0
leaq -0x80(%rbp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
andq $0x0, -0x50(%rbp)
movaps %xmm0, (%rax)
movaps %xmm0, -0x60(%rbp)
movq %r14, -0x40(%rbp)
movq (%r14), %r14
addq $0x10, %r14
xorl %r15d, %r15d
leaq -0x38(%rbp), %rbx
movq %r14, %r12
movzbl 0x4(%r13), %eax
cmpq %rax, %r15
jae 0x1b5d4
movq 0x25a44(%rip), %rdi     # 0x40fe8
leaq 0x100a9(%rip), %rsi     # 0x2b654
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
movq %r12, -0x38(%rbp)
leaq -0x80(%rbp), %rdi
movq %rbx, %rsi
callq 0x1ba50
incq %r15
addq $0x4000, %r12           # imm = 0x4000
jmp 0x1b593
shll $0xe, %eax
addq %rax, %r14
xorl %r12d, %r12d
leaq -0x60(%rbp), %rbx
leaq -0x38(%rbp), %r15
movzbl 0x5(%r13), %eax
cmpq %rax, %r12
jae 0x1b60a
movq %r14, -0x38(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1ba50
incq %r12
addq $0x2000, %r14           # imm = 0x2000
jmp 0x1b5e5
movb 0x6(%r13), %bl
movl -0x2c(%rbp), %eax
testb %al, %al
je 0x1b6d9
movzbl %al, %eax
cmpl $0x1, %eax
movq -0x40(%rbp), %r14
je 0x1b689
cmpl $0x4, %eax
jne 0x1b770
movq 0x25973(%rip), %rdi     # 0x40fa8
leaq 0x10055(%rip), %rsi     # 0x2b691
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
pushq $0x60
popq %rdi
callq 0xb140
movq %rax, %r15
andb $0x1, %bl
movzbl 0x8(%r13), %ecx
movq 0x70(%r14), %r9
movq 0x78(%r14), %rax
movq %rax, (%rsp)
movzbl %bl, %r8d
leaq -0x80(%rbp), %rsi
leaq -0x60(%rbp), %rdx
movq %r15, %rdi
callq 0x1c990
jmp 0x1b71f
pushq $-0x1
popq %rax
jmp 0x1b761
movq 0x25918(%rip), %rdi     # 0x40fa8
leaq 0xffe7(%rip), %rsi      # 0x2b67e
callq 0xb450
movq %rax, %rdi
callq 0xb7e0
pushq $0x58
popq %rdi
callq 0xb140
movq %rax, %r15
andb $0x1, %bl
movzbl 0x8(%r13), %ecx
movq 0x70(%r14), %r9
movq 0x78(%r14), %rax
movq %rax, (%rsp)
movzbl %bl, %r8d
leaq -0x80(%rbp), %rsi
leaq -0x60(%rbp), %rdx
movq %r15, %rdi
callq 0x1c4de
jmp 0x1b71f
movq 0x258c8(%rip), %rdi     # 0x40fa8
leaq 0xff82(%rip), %rsi      # 0x2b669
callq 0xb450
movq -0x40(%rbp), %r14
movq %rax, %rdi
callq 0xb7e0
pushq $0x40
popq %rdi
callq 0xb140
movq %rax, %r15
andb $0x1, %bl
movzbl 0x8(%r13), %ecx
movzbl %bl, %r8d
leaq -0x80(%rbp), %rsi
leaq -0x60(%rbp), %rdx
movq %rax, %rdi
callq 0x1c280
movq 0x80(%r14), %rdi
movq %r15, 0x80(%r14)
testq %rdi, %rdi
je 0x1b738
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x70(%r14), %rax
movq 0x80(%r14), %rcx
movq %rcx, (%rax)
movq 0x78(%r14), %rax
movq %rcx, (%rax)
leaq -0x60(%rbp), %rdi
callq 0x1bd2e
leaq -0x80(%rbp), %rdi
callq 0x1bd2e
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x25831(%rip), %rdi     # 0x40fa8
leaq 0xff26(%rip), %rsi      # 0x2b6a4
callq 0xb450
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movsbl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0xb4f0
movq %rax, %rdi
callq 0xb7e0
leaq 0xecf1(%rip), %rdi      # 0x2a4a4
leaq 0xfdc5(%rip), %rsi      # 0x2b57f
leaq 0xfef8(%rip), %rcx      # 0x2b6b9
movl $0x174, %edx            # imm = 0x174
callq 0xb170
movq %rax, %rbx
pushq $0x40
jmp 0x1b7de
movq %rax, %rbx
pushq $0x58
jmp 0x1b7de
movq %rax, %rbx
pushq $0x60
popq %rsi
movq %r15, %rdi
callq 0xb0f0
jmp 0x1b7f8
jmp 0x1b7f5
jmp 0x1b7f5
jmp 0x1b7f5
jmp 0x1b7f5
jmp 0x1b7f5
jmp 0x1b7f5
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x1bd2e
leaq -0x80(%rbp), %rdi
callq 0x1bd2e
movq %rbx, %rdi
callq 0xb4e0