// File: /workspace/github_cpp_makefile_star2_O2/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)
andq $0x0, 0x30(%rdi)
leaq 0x18(%rsp), %rdi
pushq $0xc
popq %rdx
callq 0x2250
leaq 0x90(%rsp), %rdi
callq 0x2040
testb %al, %al
je 0x2e6a
pxor %xmm0, %xmm0
movq %rsp, %rsi
movdqa %xmm0, (%rsi)
leaq 0x18(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x21b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2e9f
movdqa (%rsp), %xmm0
movd %xmm0, %eax
cmpl $0x1a53454e, %eax       # imm = 0x1A53454E
jne 0x2ebb
movzbl 0x4(%rsp), %esi
shll $0xe, %esi
movq %rbx, %rdi
callq 0x2fac
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
subq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0x21b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2eeb
movzbl 0x5(%rsp), %esi
leaq 0x18(%rbx), %rdi
shll $0xd, %esi
callq 0x2fac
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
subq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0x21b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2f1d
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 0x2050
movq %rbx, %rax
addq $0x220, %rsp            # imm = 0x220
popq %rbx
popq %r14
popq %r15
retq
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r15
leaq 0x1315(%rip), %rsi      # 0x4191
movq %rax, %rdi
callq 0x2080
movq 0x3165(%rip), %rsi      # 0x5ff0
movq 0x3126(%rip), %rdx      # 0x5fb8
movq %r15, %rdi
callq 0x2220
jmp 0x2f4d
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r15
leaq 0x1303(%rip), %rsi      # 0x41b4
movq %rax, %rdi
callq 0x2080
jmp 0x2ed5
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r15
leaq 0x1301(%rip), %rsi      # 0x41ce
movq %rax, %rdi
callq 0x2080
movq 0x3114(%rip), %rsi      # 0x5ff0
movq 0x30d5(%rip), %rdx      # 0x5fb8
movq %r15, %rdi
callq 0x2220
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r15
leaq 0x1304(%rip), %rsi      # 0x4201
movq %rax, %rdi
callq 0x2080
movq 0x30e4(%rip), %rsi      # 0x5ff0
movq 0x30a5(%rip), %rdx      # 0x5fb8
movq %r15, %rdi
callq 0x2220
jmp 0x2f4d
pushq $0x10
popq %rdi
callq 0x20e0
movq %rax, %r15
leaq 0x12fc(%rip), %rsi      # 0x422b
movq %rax, %rdi
callq 0x2080
movq 0x30b2(%rip), %rsi      # 0x5ff0
movq 0x3073(%rip), %rdx      # 0x5fb8
movq %r15, %rdi
callq 0x2220
jmp 0x2f57
jmp 0x2f57
jmp 0x2f57
jmp 0x2f57
jmp 0x2f6d
movq %rax, %r14
movq %r15, %rdi
callq 0x2140
jmp 0x2f70
movq %rax, %r14
jmp 0x2f7a
jmp 0x2f6d
jmp 0x2f6d
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x2050
movq %rbx, %rdi
callq 0x2aa4
movq %r14, %rdi
callq 0x2230