// File: /workspace/github_cpp_makefile_star2_O3/StanHash[P]nesdump/nes.cc
// Function: NesRom::from_ines_file(char const*)
NesRom NesRom::from_ines_file(char const* filename)
{
    static constexpr std::size_t INES_PRG_BANK_SIZE = KIBI*16;
    static constexpr std::size_t INES_CHR_BANK_SIZE = KIBI*8;

    NesRom result {};

    std::ifstream f(filename, std::ios::in | std::ios::binary);

    if (!f.is_open())
        throw std::runtime_error("Couldn't open file for binary read");

    std::array<byte_type, 16> ines_header {};

    if (!f.read(reinterpret_cast<char*>(ines_header.data()), ines_header.size()))
        throw std::runtime_error("Couldn't read iNES header");

    if (ines_header[0] != 'N' || ines_header[1] != 'E' || ines_header[2] != 'S' || ines_header[3] != '\x1A')
        throw std::runtime_error("file doesn't contain valid iNES header (bad magic)");

    std::size_t const prg_banks = ines_header[4];
    result.m_prg.resize(prg_banks*INES_PRG_BANK_SIZE);

    if (!f.read(reinterpret_cast<char*>(result.m_prg.data()), result.m_prg.size()))
        throw std::runtime_error("Couldn't read PRG-ROM (reached EOF early)");

    std::size_t const chr_banks = ines_header[5];
    result.m_chr.resize(chr_banks*INES_CHR_BANK_SIZE);

    if (!f.read(reinterpret_cast<char*>(result.m_chr.data()), result.m_chr.size()))
        throw std::runtime_error("Couldn't read CHR-ROM (reached EOF early)");

    result.m_mapper_id = ((ines_header[6] & 0xF0) >> 4) | (ines_header[7] & 0xF0);
    result.m_ines_flags = (ines_header[6] & 0x0F) | ((ines_header[7] & 0x0F) << 4);

    return result;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x220, %rsp            # imm = 0x220
movq %rdi, %rbx
pxor %xmm0, %xmm0
movdqu %xmm0, 0x20(%rdi)
movdqu %xmm0, 0x10(%rdi)
movdqu %xmm0, (%rdi)
movq $0x0, 0x30(%rdi)
leaq 0x18(%rsp), %rdi
movl $0xc, %edx
callq 0x2290
leaq 0x90(%rsp), %rdi
callq 0x2050
testb %al, %al
je 0x2f61
pxor %xmm0, %xmm0
movq %rsp, %rsi
movdqa %xmm0, (%rsi)
leaq 0x18(%rsp), %rdi
movl $0x10, %edx
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2f98
movdqa (%rsp), %xmm0
movd %xmm0, %eax
cmpl $0x1a53454e, %eax       # imm = 0x1A53454E
jne 0x2fb6
movzbl 0x4(%rsp), %esi
shll $0xe, %esi
movq %rbx, %rdi
callq 0x30ac
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
subq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2fe8
movzbl 0x5(%rsp), %esi
leaq 0x18(%rbx), %rdi
shll $0xd, %esi
callq 0x30ac
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
subq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x301c
movb 0x6(%rsp), %al
movb 0x7(%rsp), %cl
movl %eax, %edx
shrb $0x4, %dl
movl %ecx, %esi
andb $-0x10, %sil
orb %dl, %sil
movb %sil, 0x30(%rbx)
andb $0xf, %al
shlb $0x4, %cl
orb %al, %cl
movb %cl, 0x31(%rbx)
leaq 0x18(%rsp), %rdi
callq 0x2060
movq %rbx, %rax
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %r15
retq
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x121c(%rip), %rsi      # 0x4191
movq %rax, %rdi
callq 0x2080
movq 0x306c(%rip), %rsi      # 0x5ff0
movq 0x3035(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
jmp 0x304e
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x1208(%rip), %rsi      # 0x41b4
movq %rax, %rdi
callq 0x2080
jmp 0x2fd2
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x1204(%rip), %rsi      # 0x41ce
movq %rax, %rdi
callq 0x2080
movq 0x3017(%rip), %rsi      # 0x5ff0
movq 0x2fe0(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x1205(%rip), %rsi      # 0x4201
movq %rax, %rdi
callq 0x2080
movq 0x2fe5(%rip), %rsi      # 0x5ff0
movq 0x2fae(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
jmp 0x304e
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x11fb(%rip), %rsi      # 0x422b
movq %rax, %rdi
callq 0x2080
movq 0x2fb1(%rip), %rsi      # 0x5ff0
movq 0x2f7a(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
jmp 0x3058
jmp 0x3058
jmp 0x3058
jmp 0x3058
jmp 0x306e
movq %rax, %r14
movq %r15, %rdi
callq 0x2130
jmp 0x3071
movq %rax, %r14
jmp 0x307b
jmp 0x306e
jmp 0x306e
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2d54
movq %r14, %rdi
callq 0x2270