// File: /workspace/github_cpp_makefile_star2_O1/QFSW[P]MIPS1-Simulator/src/Simulator.cpp
// Function: Clarkitechture::MIPS::Simulator::loadMachineCode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Simulator::loadMachineCode(std::string fileName)
{
    std::ifstream binstream;
    binstream.open(fileName, std::ios_base::binary | std::ios_base::in | std::ios_base::ate);
    if (!binstream.is_open()) { throw BadBinaryIO("could not open file " + fileName); }
    
    size_t size = binstream.tellg();
    if (size <= 0) { throw BadBinaryIO("file was empty"); }
    if (size % 4 > 0) { throw BadBinaryIO("file contained a non integral word count"); }
    if (size > MemoryMap::ADDR_INSTR_LENGTH) { throw BadBinaryIO("file exceeded maximum binary size"); }
    binstream.seekg(0, std::ios::beg);
    
    instrCount = size / 4;
    instrs.resize(instrCount);
    
    uint32_t* words = new uint32_t[instrCount];
    mem.initInstrMemory(reinterpret_cast<byte*>(words), size);
    binstream.read(reinterpret_cast<char*>(words), size);
    
    for (size_t i = 0; i < instrCount; ++i)
    {
        uint32_t flippedWord = __builtin_bswap32(words[i]);
        instrs[i] = std::shared_ptr<Instruction>(BinaryDecoder::decodeInstruction(flippedWord));
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x298, %rsp            # imm = 0x298
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x6230
movq %r14, %rdi
movq %r15, %rsi
movl $0xe, %edx
callq 0x61d0
leaq 0x108(%rsp), %rdi
callq 0x6060
testb %al, %al
je 0xa9ba
leaq 0x90(%rsp), %rdi
callq 0x6390
movq %rax, %r12
testq %rax, %rax
je 0xaa08
testb $0x3, %r12b
jne 0xaa63
cmpq $0x1000001, %r12        # imm = 0x1000001
jae 0xaabb
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x62c0
movq %r12, %rsi
shrq $0x2, %rsi
movq %rsi, (%rbx)
leaq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0xaea0
movq (%rbx), %rax
movq %rax, %rcx
shrq $0x3e, %rcx
shlq $0x2, %rax
xorl %edi, %edi
negq %rcx
sbbq %rdi, %rdi
orq %rax, %rdi
callq 0x6030
movq %rax, %r15
leaq 0xc0(%rbx), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x9dbc
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x6290
cmpq $0x0, (%rbx)
je 0xa99b
movl $0x8, %r13d
leaq 0x8(%rsp), %r12
xorl %ebp, %ebp
movl (%r15,%rbp,4), %edi
bswapl %edi
callq 0x9208
movq %rax, (%rsp)
movq %r12, %rdi
movq %rax, %rsi
callq 0xb132
movq (%r14), %rax
movaps (%rsp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, (%rsp)
movq (%rax,%r13), %rdi
movups %xmm0, -0x8(%rax,%r13)
testq %rdi, %rdi
je 0xa980
callq 0xaf14
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0xa98f
callq 0xaf14
incq %rbp
addq $0x10, %r13
cmpq (%rbx), %rbp
jb 0xa944
leaq 0x90(%rsp), %rdi
callq 0x6070
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x48, %edi
callq 0x6100
movq %rax, %r14
leaq 0x2422(%rip), %rsi      # 0xcdf0
leaq 0x70(%rsp), %rdi
movq %r15, %rdx
callq 0x74b0
movb $0x1, %bpl
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x6e1a
xorl %ebp, %ebp
leaq 0x63a4(%rip), %rsi      # 0x10d98
leaq -0x3247(%rip), %rdx     # 0x77b4
movq %r14, %rdi
callq 0x6380
jmp 0xab11
movl $0x48, %edi
callq 0x6100
movq %rax, %r14
leaq 0x60(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x23e0(%rip), %rsi      # 0xce05
leaq 0x23e7(%rip), %rdx      # 0xce13
leaq 0x50(%rsp), %rdi
callq 0x8e9a
movb $0x1, %bpl
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x6e1a
xorl %ebp, %ebp
leaq 0x6349(%rip), %rsi      # 0x10d98
leaq -0x32a2(%rip), %rdx     # 0x77b4
movq %r14, %rdi
callq 0x6380
jmp 0xab11
movl $0x48, %edi
callq 0x6100
movq %rax, %r14
leaq 0x40(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2394(%rip), %rsi      # 0xce14
leaq 0x23b5(%rip), %rdx      # 0xce3c
leaq 0x30(%rsp), %rdi
callq 0x8e9a
movb $0x1, %bpl
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x6e1a
xorl %ebp, %ebp
leaq 0x62ee(%rip), %rsi      # 0x10d98
leaq -0x32fd(%rip), %rdx     # 0x77b4
movq %r14, %rdi
callq 0x6380
jmp 0xab11
movl $0x48, %edi
callq 0x6100
movq %rax, %r14
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2365(%rip), %rsi      # 0xce3d
leaq 0x237f(%rip), %rdx      # 0xce5e
leaq 0x10(%rsp), %rdi
callq 0x8e9a
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x6e1a
xorl %ebp, %ebp
leaq 0x6296(%rip), %rsi      # 0x10d98
leaq -0x3355(%rip), %rdx     # 0x77b4
movq %r14, %rdi
callq 0x6380
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0xab7f
movq 0x20(%rsp), %rsi
jmp 0xab6f
jmp 0xab79
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0xab7f
movq 0x40(%rsp), %rsi
jmp 0xab6f
jmp 0xab79
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0xab7f
movq 0x60(%rsp), %rsi
jmp 0xab6f
jmp 0xab79
movq %rax, %rbx
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xab7f
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x6210
jmp 0xab7f
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0xab99
movq %r14, %rdi
callq 0x6190
jmp 0xab99
jmp 0xab96
jmp 0xab96
jmp 0xab96
jmp 0xab96
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x6070
movq %rbx, %rdi
callq 0x63a0