// File: /workspace/github_cpp_makefile_star2_O3/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 0x5230
movq %r14, %rdi
movq %r15, %rsi
movl $0xe, %edx
callq 0x51d0
leaq 0x108(%rsp), %rdi
callq 0x5060
testb %al, %al
je 0x99b6
leaq 0x90(%rsp), %rdi
callq 0x5390
movq %rax, %r12
testq %rax, %rax
je 0x9a04
testb $0x3, %r12b
jne 0x9a5f
cmpq $0x1000001, %r12        # imm = 0x1000001
jae 0x9ab7
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x52c0
movq %r12, %rsi
shrq $0x2, %rsi
movq %rsi, (%rbx)
leaq 0x8(%rbx), %r14
movq %r14, %rdi
callq 0x9e8e
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, %r15
leaq 0xc0(%rbx), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x8da6
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x5290
cmpq $0x0, (%rbx)
je 0x9997
movl $0x8, %r13d
leaq 0x8(%rsp), %r12
xorl %ebp, %ebp
movl (%r15,%rbp,4), %edi
bswapl %edi
callq 0x8160
movq %rax, (%rsp)
movq %r12, %rdi
movq %rax, %rsi
callq 0xa114
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 0x997c
callq 0x9f02
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x998b
callq 0x9f02
incq %rbp
addq $0x10, %r13
cmpq (%rbx), %rbp
jb 0x9940
leaq 0x90(%rsp), %rdi
callq 0x5070
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x48, %edi
callq 0x5100
movq %rax, %r14
leaq 0x2426(%rip), %rsi      # 0xbdf0
leaq 0x70(%rsp), %rdi
movq %r15, %rdx
callq 0x64ae
movb $0x1, %bpl
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x5e1a
xorl %ebp, %ebp
leaq 0x63e0(%rip), %rsi      # 0xfdd0
leaq -0x3245(%rip), %rdx     # 0x67b2
movq %r14, %rdi
callq 0x5380
jmp 0x9b0d
movl $0x48, %edi
callq 0x5100
movq %rax, %r14
leaq 0x60(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x23e4(%rip), %rsi      # 0xbe05
leaq 0x23eb(%rip), %rdx      # 0xbe13
leaq 0x50(%rsp), %rdi
callq 0x7e20
movb $0x1, %bpl
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x5e1a
xorl %ebp, %ebp
leaq 0x6385(%rip), %rsi      # 0xfdd0
leaq -0x32a0(%rip), %rdx     # 0x67b2
movq %r14, %rdi
callq 0x5380
jmp 0x9b0d
movl $0x48, %edi
callq 0x5100
movq %rax, %r14
leaq 0x40(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2398(%rip), %rsi      # 0xbe14
leaq 0x23b9(%rip), %rdx      # 0xbe3c
leaq 0x30(%rsp), %rdi
callq 0x7e20
movb $0x1, %bpl
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x5e1a
xorl %ebp, %ebp
leaq 0x632a(%rip), %rsi      # 0xfdd0
leaq -0x32fb(%rip), %rdx     # 0x67b2
movq %r14, %rdi
callq 0x5380
jmp 0x9b0d
movl $0x48, %edi
callq 0x5100
movq %rax, %r14
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x2369(%rip), %rsi      # 0xbe3d
leaq 0x2383(%rip), %rdx      # 0xbe5e
leaq 0x10(%rsp), %rdi
callq 0x7e20
movb $0x1, %bpl
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x5e1a
xorl %ebp, %ebp
leaq 0x62d2(%rip), %rsi      # 0xfdd0
leaq -0x3353(%rip), %rdx     # 0x67b2
movq %r14, %rdi
callq 0x5380
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x9b73
movq 0x20(%rsp), %rsi
jmp 0x9b6b
jmp 0x9b7a
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0x9b73
movq 0x40(%rsp), %rsi
jmp 0x9b6b
jmp 0x9b7a
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0x9b73
movq 0x60(%rsp), %rsi
jmp 0x9b6b
jmp 0x9b7a
movq %rax, %rbx
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9b73
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5210
testb %bpl, %bpl
jne 0x9b7d
jmp 0x9b92
movq %rax, %rbx
movq %r14, %rdi
callq 0x5190
jmp 0x9b92
jmp 0x9b8f
jmp 0x9b8f
jmp 0x9b8f
jmp 0x9b8f
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x5070
movq %rbx, %rdi
callq 0x53a0
nop