// File: /workspace/github_cpp_makefile_star2_O2/jmbjorndalen[P]Malbolge-gen/mbolge-gen.cpp
// Function: MBSearch::getMem(unsigned short)
unsigned short MBSearch::getMem(unsigned short addr)
{
    if (addr >= MAX_TRIT)
    { 
	//std::cerr << "MBSearch::getMem called with addr " << addr << " with is outside legal bounds\n";
	throw ILLEGAL_ADDR_EXCEPTION; // program made an illegal reference, which will not fare well with the original interpreter, so abort this branch
    }
    if (addr >= maxAllowedProgLen) 
	throw MAX_APLEN_EXCEPTION;
    
    if (addr > highestAddrRead)
	highestAddrRead = addr; 
    
    MBMemory::iterator pos = mem.find(addr); 
    if (pos != mem.end())
	return pos->second;  // already have it

    // Reading a new memory location that has never been visited before. 
    // First, check whether we have branched out too deeply before omitting chars
    ++odepth; 
    if (odepth > MAX_OSEARCH)
	throw BRANCH_DEPTH_EXCEPTION; 

    // Simply fill in the first possible OP, then store this object on the stack, allowing the main loop to 
    // pick it up again and modify it later (restarting the object with the next possible instruction). 
    curNewMem = addr;         // keep track of addr, so main loop can modify this instruction
    curNewOP  = 0;            // we're currently using the first one
    orig[addr] = mem[addr]  = encode_op(MalbolgeOps[curNewOP], addr);
    ++nFetches;

    // Store a copy of this one on the stack
    globalStack.push(new MBSearch(*this));     // default copy constructor is ok

    return mem[addr];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movl %esi, %ebx
cmpw $0xe6a9, %bx            # imm = 0xE6A9
jae 0x404830
cmpl %ebx, 0x1ed9dd(%rip)    # 0x5f2130
jle 0x404840
movq %rdi, %r15
cmpl %ebx, 0x78(%rdi)
jge 0x404765
movl %ebx, 0x78(%r15)
leaq 0x18(%r15), %r14
movq %rsp, %rsi
movl %ebx, (%rsi)
movq %r14, %rdi
callq 0x4059c2
leaq 0x20(%r15), %rcx
cmpq %rcx, %rax
je 0x404787
movl 0x24(%rax), %eax
jmp 0x404823
movl 0x14(%r15), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x14(%r15)
cmpl $0xa, %eax
jge 0x404850
movl %ebx, 0xc(%r15)
andl $0x0, 0x10(%r15)
movzwl 0x1ed995(%rip), %eax    # 0x5f2140
subl %ebx, %eax
pushq $0x5e
popq %rcx
cltd
idivl %ecx
leal 0x5e(%rdx), %eax
testl %edx, %edx
cmovnsl %edx, %eax
addl $0x21, %eax
movzwl %ax, %ebp
movq %rsp, %rsi
movl %ebx, (%rsi)
movq %r14, %rdi
callq 0x405340
movl %ebp, (%rax)
leaq 0x48(%r15), %rdi
leaq 0xc(%rsp), %rsi
movl %ebx, (%rsi)
callq 0x405340
movl %ebp, (%rax)
incl 0x8(%r15)
movl $0xa8, %edi
callq 0x407310
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x40538e
movq %rsp, %r15
movq %r12, (%r15)
leaq 0x1efd6d(%rip), %rdi    # 0x5f4578
movq %r15, %rsi
callq 0x405c20
movl %ebx, (%r15)
movq %rsp, %rsi
movq %r14, %rdi
callq 0x405340
movl (%rax), %eax
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x4
popq %rdi
callq 0x4061c0
movl $0x2b, (%rax)
jmp 0x40485e
pushq $0x4
popq %rdi
callq 0x4061c0
movl $0x2c, (%rax)
jmp 0x40485e
pushq $0x4
popq %rdi
callq 0x4061c0
movl $0x2a, (%rax)
leaq 0x1e614b(%rip), %rsi    # 0x5ea9b0
movq %rax, %rdi
xorl %edx, %edx
callq 0x407210
movq %rax, %rbx
movl $0xa8, %esi
movq %r12, %rdi
callq 0x405ee0
movq %rbx, %rdi
callq 0x4b1a40
nop