// File: /workspace/github_cpp_makefile_star2_O1/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
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movups %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 0x2f9e
xorps %xmm0, %xmm0
movq %rsp, %rsi
movaps %xmm0, (%rsi)
leaq 0x18(%rsp), %rdi
movl $0x10, %edx
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x2fd5
cmpb $0x4e, (%rsp)
jne 0x2ff3
cmpb $0x45, 0x1(%rsp)
jne 0x2ff3
cmpb $0x53, 0x2(%rsp)
jne 0x2ff3
cmpb $0x1a, 0x3(%rsp)
jne 0x2ff3
movzbl 0x4(%rsp), %esi
shll $0xe, %esi
movq %rbx, %rdi
callq 0x30ea
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 0x3025
movzbl 0x5(%rsp), %esi
leaq 0x18(%rbx), %rdi
shll $0xd, %esi
callq 0x30ea
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 0x3059
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 0x11df(%rip), %rsi      # 0x4191
movq %rax, %rdi
callq 0x2080
movq 0x302f(%rip), %rsi      # 0x5ff0
movq 0x2ff8(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
jmp 0x308b
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x11cb(%rip), %rsi      # 0x41b4
movq %rax, %rdi
callq 0x2080
jmp 0x300f
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x11c7(%rip), %rsi      # 0x41ce
movq %rax, %rdi
callq 0x2080
movq 0x2fda(%rip), %rsi      # 0x5ff0
movq 0x2fa3(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x11c8(%rip), %rsi      # 0x4201
movq %rax, %rdi
callq 0x2080
movq 0x2fa8(%rip), %rsi      # 0x5ff0
movq 0x2f71(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
jmp 0x308b
movl $0x10, %edi
callq 0x20c0
movq %rax, %r15
leaq 0x11be(%rip), %rsi      # 0x422b
movq %rax, %rdi
callq 0x2080
movq 0x2f74(%rip), %rsi      # 0x5ff0
movq 0x2f3d(%rip), %rdx      # 0x5fc0
movq %r15, %rdi
callq 0x2260
jmp 0x3095
jmp 0x3095
jmp 0x3095
jmp 0x3095
jmp 0x30ab
movq %rax, %r14
movq %r15, %rdi
callq 0x2130
jmp 0x30ae
movq %rax, %r14
jmp 0x30b8
jmp 0x30ab
jmp 0x30ab
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2d88
movq %r14, %rdi
callq 0x2270