// File: /workspace/github_cpp_makefile_star2_O3/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 %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
testb $0x1, %r14b
jne 0x76f9
leaq -0x30000000(%r14), %rax
cmpq $0x2, %rax
ja 0x763d
callq 0x52d0
cmpl $-0x1, %eax
je 0x75c1
movl %eax, %ebx
movq 0x9a17(%rip), %rax      # 0x10fc8
movq (%rax), %rdi
callq 0x5330
testl %eax, %eax
je 0x76dc
movq 0x9a00(%rip), %rax      # 0x10fc8
movq (%rax), %rdi
callq 0x53e0
movw $0xffff, %bp            # imm = 0xFFFF
testl %eax, %eax
je 0x76e8
movl %eax, %r14d
movl $0x50, %edi
callq 0x5100
movq %rax, %rbx
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3e05(%rip), %rsi      # 0xb401
leaq 0x3e03(%rip), %rdx      # 0xb406
leaq 0x28(%rsp), %rdi
callq 0x7e20
movb $0x1, %bpl
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
movl %r14d, %edx
callq 0x5ef2
xorl %ebp, %ebp
leaq 0x87bf(%rip), %rsi      # 0xfde8
leaq -0xdce(%rip), %rdx      # 0x6862
movq %rbx, %rdi
callq 0x5380
jmp 0x77ad
movq %rdi, %rbx
movq %r14, %rax
andq $-0x4000000, %rax       # imm = 0xFC000000
cmpq $0x20000000, %rax       # imm = 0x20000000
jne 0x769d
addl $0xe0000000, %r14d      # imm = 0xE0000000
xorl %eax, %eax
movb $0x1, %r12b
leaq 0x4(%rsp), %r15
xorl %ebp, %ebp
leal (,%rax,8), %r13d
orl %r14d, %eax
movl %eax, 0x4(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x7eb6
movzbl (%rax), %eax
movb $0x8, %cl
subb %r13b, %cl
shll %cl, %eax
orl %eax, %ebp
movl $0x1, %eax
testb $0x1, %r12b
movl $0x0, %r12d
jne 0x7664
jmp 0x76e8
cmpq $0x10000000, %r14       # imm = 0x10000000
jb 0x76c7
movl $0x10000000, %eax       # imm = 0x10000000
addq 0x40(%rbx), %rax
cmpq %r14, %rax
jbe 0x76c7
movq 0x38(%rbx), %rax
movzwl -0x10000000(%rax,%r14), %ebp
rolw $0x8, %bp
jmp 0x76e8
movq %r14, %rax
andq $-0x1000000, %rax       # imm = 0xFF000000
cmpq $0x10000000, %rax       # imm = 0x10000000
jne 0x7754
xorl %ebp, %ebp
jmp 0x76e8
xorl %ebp, %ebp
cmpq $0x30000002, %r14       # imm = 0x30000002
cmovel %ebx, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x50, %edi
callq 0x5100
movq %rax, %rbx
leaq 0x58(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3cca(%rip), %rsi      # 0xb3e0
leaq 0x3ce3(%rip), %rdx      # 0xb400
leaq 0x48(%rsp), %rdi
callq 0x7e20
movb $0x1, %bpl
leaq 0x48(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x58ea
xorl %ebp, %ebp
leaq 0x865d(%rip), %rsi      # 0xfda0
leaq -0x10f8(%rip), %rdx     # 0x6652
movq %rbx, %rdi
callq 0x5380
jmp 0x77ad
movl $0x50, %edi
callq 0x5100
movq %rax, %rbx
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3c96(%rip), %rsi      # 0xb407
leaq 0x3cc1(%rip), %rdx      # 0xb439
leaq 0x8(%rsp), %rdi
callq 0x7e20
movb $0x1, %bpl
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x58ea
xorl %ebp, %ebp
leaq 0x8602(%rip), %rsi      # 0xfda0
leaq -0x1153(%rip), %rdx     # 0x6652
movq %rbx, %rdi
callq 0x5380
movq %rax, %r14
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x77f3
movq 0x18(%rsp), %rsi
jmp 0x77eb
jmp 0x77fa
movq %rax, %r14
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x77f3
movq 0x38(%rsp), %rsi
jmp 0x77eb
jmp 0x77fa
movq %rax, %r14
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x77f3
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x5210
testb %bpl, %bpl
jne 0x77fd
jmp 0x7805
movq %rax, %r14
movq %rbx, %rdi
callq 0x5190
movq %r14, %rdi
callq 0x53a0
nop