// File: /workspace/github_cpp_makefile_star2_O2/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 0x5210
pushq $0xe
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x51b0
leaq 0x108(%rsp), %rdi
callq 0x5050
testb %al, %al
je 0x8cb0
leaq 0x90(%rsp), %rdi
callq 0x5370
movq %rax, %r15
testq %rax, %rax
je 0x8cfc
testb $0x3, %r15b
jne 0x8d48
cmpq $0x1000001, %r15        # imm = 0x1000001
jae 0x8d91
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x52a0
movq %r15, %rsi
shrq $0x2, %rsi
movq %rsi, (%rbx)
leaq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x90d0
movq (%rbx), %rax
movq %rax, %rcx
shrq $0x3e, %rcx
shlq $0x2, %rax
xorl %edi, %edi
negq %rcx
sbbq %rdi, %rdi
orq %rax, %rdi
callq 0x5030
movq %rax, %r12
leaq 0xc0(%rbx), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x8186
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x5260
movq %rbx, %r15
xorl %ebp, %ebp
movq %rsp, %r13
xorl %ebx, %ebx
cmpq (%r15), %rbx
jae 0x8c91
movl (%r12,%rbx,4), %edi
bswapl %edi
callq 0x77cc
movq %r13, %rdi
movq %rax, %rsi
callq 0x942e
movq (%r14), %rdi
addq %rbp, %rdi
movq %r13, %rsi
callq 0x94ca
leaq 0x8(%rsp), %rdi
callq 0x9100
incq %rbx
addq $0x10, %rbp
jmp 0x8c55
leaq 0x90(%rsp), %rdi
callq 0x5070
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0x50f0
movq %rax, %r14
leaq 0x2117(%rip), %rsi      # 0xadd9
leaq 0x70(%rsp), %rdi
movq %r15, %rdx
callq 0x5cf4
movb $0x1, %bpl
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x59b2
xorl %ebp, %ebp
leaq 0x70f0(%rip), %rsi      # 0xfdd8
leaq -0x2d47(%rip), %rdx     # 0x5fa8
movq %r14, %rdi
callq 0x5360
jmp 0x8dd8
pushq $0x48
popq %rdi
callq 0x50f0
movq %rax, %r14
leaq 0x20e0(%rip), %rsi      # 0xadee
leaq 0x50(%rsp), %rdi
movq %rsp, %rdx
callq 0x67c8
movb $0x1, %bpl
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x59b2
xorl %ebp, %ebp
leaq 0x70a4(%rip), %rsi      # 0xfdd8
leaq -0x2d93(%rip), %rdx     # 0x5fa8
movq %r14, %rdi
callq 0x5360
jmp 0x8dd8
pushq $0x48
popq %rdi
callq 0x50f0
movq %rax, %r14
leaq 0x20a3(%rip), %rsi      # 0xadfd
leaq 0x30(%rsp), %rdi
movq %rsp, %rdx
callq 0x67c8
movb $0x1, %bpl
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x59b2
xorl %ebp, %ebp
leaq 0x7058(%rip), %rsi      # 0xfdd8
leaq -0x2ddf(%rip), %rdx     # 0x5fa8
movq %r14, %rdi
callq 0x5360
jmp 0x8dd8
pushq $0x48
popq %rdi
callq 0x50f0
movq %rax, %r14
leaq 0x2083(%rip), %rsi      # 0xae26
leaq 0x10(%rsp), %rdi
movq %rsp, %rdx
callq 0x67c8
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x59b2
xorl %ebp, %ebp
leaq 0x700f(%rip), %rsi      # 0xfdd8
leaq -0x2e28(%rip), %rdx     # 0x5fa8
movq %r14, %rdi
callq 0x5360
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
jmp 0x8e04
jmp 0x8e10
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x8e04
jmp 0x8e10
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
jmp 0x8e04
jmp 0x8e10
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x50d0
testb %bpl, %bpl
jne 0x8e13
jmp 0x8e28
movq %rax, %rbx
movq %r14, %rdi
callq 0x5170
jmp 0x8e28
jmp 0x8e25
jmp 0x8e25
jmp 0x8e25
jmp 0x8e25
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x5070
movq %rbx, %rdi
callq 0x5390
nop