// File: /workspace/github_cpp_makefile_star2_O1/QFSW[P]MIPS1-Simulator/src/MemoryMap.hpp
// Function: unsigned short Clarkitechture::MIPS::MemoryMap::read<unsigned short>(unsigned long)
T read(size_t address)
            {
                if (address % sizeof(T) > 0) { throw BadMemoryAccess(address, "read data address was misaligned"); }
                else if (address >= ADDR_GETC && address <= ADDR_GETC + 4 - sizeof(T))
                {
                    int input = std::getchar();
                    if (input == EOF || feof(stdin))
                    {
                        int error = ferror(stdin);
                        if (error) { throw BadIO("stdin", error); }
                        else { return -1; }
                    }
                    else if (address == ADDR_GETC + 4 - sizeof(T)) { return input; }
                    else { return 0; }
                }
                else if (address >= ADDR_DATA && address < ADDR_DATA + ADDR_DATA_LENGTH)
                {
                    T data = 0;
                    size_t memLocation = address - ADDR_DATA;
                    for (int i = 0; i < sizeof(T); i++)
                    {
                        int shamt = 8 * (sizeof(T) - i - 1);
                        data |= (memory[memLocation + i] << shamt);
                    }   

                    return data;
                }
                else if (address >= ADDR_INSTR && address < ADDR_INSTR + instrMemorySize)
                {
                    byte* memLocation = instrMemory + (address - ADDR_INSTR);
                    T data = *reinterpret_cast<T*>(memLocation);
                    return bswap(data);
                }
                else if (address >= ADDR_INSTR && address < ADDR_INSTR + ADDR_INSTR_LENGTH) { return 0; }
                else { throw BadMemoryAccess(address, "address was outside of the valid R/W memory range."); }
            }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x70, %rsp
movq %rsi, %r14
testb $0x1, %r14b
jne 0x8738
leaq -0x30000000(%r14), %rax
cmpq $0x2, %rax
ja 0x8679
callq 0x62d0
cmpl $-0x1, %eax
je 0x85fd
movl %eax, %ebp
movq 0x99db(%rip), %rax      # 0x11fc8
movq (%rax), %rdi
callq 0x6330
testl %eax, %eax
je 0x871d
movq 0x99c4(%rip), %rax      # 0x11fc8
movq (%rax), %rdi
callq 0x63e0
movw $0xffff, %bx            # imm = 0xFFFF
testl %eax, %eax
je 0x8729
movl %eax, %ebp
movl $0x50, %edi
callq 0x6100
movq %rax, %rbx
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x3dc9(%rip), %rsi      # 0xc401
leaq 0x3dc7(%rip), %rdx      # 0xc406
leaq 0x30(%rsp), %rdi
callq 0x8e9a
movb $0x1, %r15b
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x6ef2
xorl %r15d, %r15d
leaq 0x874b(%rip), %rsi      # 0x10db0
leaq -0xe08(%rip), %rdx      # 0x7864
movq %rbx, %rdi
callq 0x6380
jmp 0x87ec
movq %rdi, %r15
movq %r14, %rax
andq $-0x4000000, %rax       # imm = 0xFC000000
cmpq $0x20000000, %rax       # imm = 0x20000000
jne 0x86c3
addl $0xe0000000, %r14d      # imm = 0xE0000000
movl $0x8, %ebp
xorl %ebx, %ebx
leaq 0xc(%rsp), %r12
movl %r14d, 0xc(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x8f2a
movzbl (%rax), %eax
movl %ebp, %ecx
shll %cl, %eax
orl %eax, %ebx
incl %r14d
addl $-0x8, %ebp
je 0x86a0
jmp 0x8729
cmpq $0x10000000, %r14       # imm = 0x10000000
jb 0x8708
movl $0x10000000, %eax       # imm = 0x10000000
addq 0x40(%r15), %rax
cmpq %r14, %rax
jbe 0x8708
movq 0x38(%r15), %rax
movzwl -0x10000000(%rax,%r14), %ecx
leaq 0xc(%rsp), %rax
movw %cx, (%rax)
movl $0x8, %ecx
xorl %ebx, %ebx
movzbl (%rax), %edx
shll %cl, %edx
orl %edx, %ebx
incq %rax
addq $-0x8, %rcx
je 0x86f6
jmp 0x8729
movq %r14, %rax
andq $-0x1000000, %rax       # imm = 0xFF000000
cmpq $0x10000000, %rax       # imm = 0x10000000
jne 0x8793
xorl %ebx, %ebx
jmp 0x8729
xorl %ebx, %ebx
cmpq $0x30000002, %r14       # imm = 0x30000002
cmovel %ebp, %ebx
movl %ebx, %eax
addq $0x70, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl $0x50, %edi
callq 0x6100
movq %rax, %rbx
leaq 0x60(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3c8b(%rip), %rsi      # 0xc3e0
leaq 0x3ca4(%rip), %rdx      # 0xc400
leaq 0x50(%rsp), %rdi
callq 0x8e9a
movb $0x1, %bpl
leaq 0x50(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68ea
xorl %ebp, %ebp
leaq 0x85e6(%rip), %rsi      # 0x10d68
leaq -0x1135(%rip), %rdx     # 0x7654
movq %rbx, %rdi
callq 0x6380
jmp 0x87ec
movl $0x50, %edi
callq 0x6100
movq %rax, %rbx
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3c57(%rip), %rsi      # 0xc407
leaq 0x3c82(%rip), %rdx      # 0xc439
leaq 0x10(%rsp), %rdi
callq 0x8e9a
movb $0x1, %bpl
leaq 0x10(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68ea
xorl %ebp, %ebp
leaq 0x858b(%rip), %rsi      # 0x10d68
leaq -0x1190(%rip), %rdx     # 0x7654
movq %rbx, %rdi
callq 0x6380
movq %rax, %r14
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x884d
movq 0x20(%rsp), %rsi
jmp 0x883d
jmp 0x8847
movq %rax, %r14
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x8824
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x6210
jmp 0x8824
movq %rax, %r14
movb $0x1, %r15b
testb %r15b, %r15b
jne 0x8852
jmp 0x885a
movq %rax, %r14
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0x884d
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x6210
jmp 0x884d
movq %rax, %r14
movb $0x1, %bpl
testb %bpl, %bpl
je 0x885a
movq %rbx, %rdi
callq 0x6190
movq %r14, %rdi
callq 0x63a0