// File: rockytriton[P]dreamsys_emu_gb/src/op_handler.cpp
// Function: dsemu::cpu::handleRET(dsemu::cpu::OpCode const&)
int handleRET(const OpCode &op) {
    bool didJump = false;
    ushort location = cpu::spop();

    //cout << "HANDLING RET: " << Short(lastCallAddress) << " - " << Short(location) << endl;
    int ret = conditionalJump(location - 1, op, didJump);

    
    if (regPC == 0x3017) {
        regPC = regPC + 0;
    }

    if (didJump) {
        cout << std::setfill('-') << std::setw(callSize) << "-" << "RET - AFTER RET: " << ret << " - " << Short(regPC) << " / " << Short(location) << " CALLSIZE: " << callSize << " STACK: ";

        if (!cameFromI) {
            //for (int i=0; i<callSize; i++) {
            //    cout << " ";
            //}

            callSize--;
        }

        if (callSize < 0) {
            cout << "OOPS" << endl;
        }

        for (size_t i=0; i<THESTACK.size(); i++) {
            cout << Byte(THESTACK[i]) << "-";
        }
        
        cout << endl;
    }

    if (!didJump) {
        cpu::push(location);
    }

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movb $0x0, -0x9(%rbp)
callq 0x144b0
movw %ax, -0xc(%rbp)
movzwl -0xc(%rbp), %eax
subl $0x1, %eax
movq -0x8(%rbp), %rsi
leaq -0x9(%rbp), %rdx
movzwl %ax, %edi
callq 0x1b160
movl %eax, -0x10(%rbp)
leaq 0x13c64(%rip), %rax     # 0x2f59e
movzwl (%rax), %eax
cmpl $0x3017, %eax           # imm = 0x3017
jne 0x1b95e
leaq 0x13c53(%rip), %rax     # 0x2f59e
movzwl (%rax), %eax
addl $0x0, %eax
movw %ax, %cx
leaq 0x13c43(%rip), %rax     # 0x2f59e
movw %cx, (%rax)
testb $0x1, -0x9(%rbp)
je 0x1bb1f
movl $0x2d, %edi
callq 0x138b0
movb %al, -0x11(%rbp)
movq 0x13654(%rip), %rdi     # 0x2efd0
movzbl -0x11(%rbp), %esi
callq 0x3430
movq %rax, -0x48(%rbp)
movl 0x33d65(%rip), %edi     # 0x4f6f4
callq 0x138d0
movq -0x48(%rbp), %rdi
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
callq 0x3380
movq %rax, %rdi
leaq 0x8b12(%rip), %rsi      # 0x244bf
callq 0x32c0
movq %rax, %rdi
leaq 0x8b2d(%rip), %rsi      # 0x244e9
callq 0x32c0
movq %rax, %rdi
movl -0x10(%rbp), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x8653(%rip), %rsi      # 0x24029
callq 0x32c0
movq %rax, -0x40(%rbp)
leaq -0x1a(%rbp), %rdi
leaq 0x13bb4(%rip), %rax     # 0x2f59e
movzwl (%rax), %esi
callq 0x13840
movq -0x40(%rbp), %rdi
movzwl -0x1a(%rbp), %esi
callq 0x137b0
movq %rax, %rdi
leaq 0x8af2(%rip), %rsi      # 0x244fb
callq 0x32c0
movq %rax, -0x38(%rbp)
leaq -0x1c(%rbp), %rdi
movzwl -0xc(%rbp), %esi
callq 0x13840
movq -0x38(%rbp), %rdi
movzwl -0x1c(%rbp), %esi
callq 0x137b0
movq %rax, %rdi
leaq 0x8a9b(%rip), %rsi      # 0x244d1
callq 0x32c0
movq %rax, %rdi
movl 0x33cb0(%rip), %esi     # 0x4f6f4
callq 0x3490
movq %rax, %rdi
leaq 0x8a8a(%rip), %rsi      # 0x244dd
callq 0x32c0
testb $0x1, 0x33c99(%rip)     # 0x4f6f8
jne 0x1ba70
movl 0x33c8d(%rip), %eax     # 0x4f6f4
addl $-0x1, %eax
movl %eax, 0x33c84(%rip)     # 0x4f6f4
cmpl $0x0, 0x33c7d(%rip)     # 0x4f6f4
jge 0x1ba9b
movq 0x13550(%rip), %rdi     # 0x2efd0
leaq 0x8a78(%rip), %rsi      # 0x244ff
callq 0x32c0
movq %rax, %rdi
movq 0x1351a(%rip), %rsi     # 0x2efb0
callq 0x3310
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq 0x13b06(%rip), %rdi     # 0x2f5b8
callq 0x15e20
movq %rax, %rcx
movq -0x50(%rbp), %rax
cmpq %rcx, %rax
jae 0x1bb0c
movq -0x28(%rbp), %rsi
leaq 0x13aea(%rip), %rdi     # 0x2f5b8
callq 0x1dbd0
leaq -0x29(%rbp), %rdi
movzbl (%rax), %esi
callq 0x13860
movq 0x134ea(%rip), %rdi     # 0x2efd0
movzbl -0x29(%rbp), %esi
callq 0x13720
movq %rax, %rdi
leaq 0x89c6(%rip), %rsi      # 0x244bf
callq 0x32c0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x1baa3
movq 0x134bd(%rip), %rdi     # 0x2efd0
movq 0x13496(%rip), %rsi     # 0x2efb0
callq 0x3310
testb $0x1, -0x9(%rbp)
jne 0x1bb2e
movzwl -0xc(%rbp), %edi
callq 0x14360
movl -0x10(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)