// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0xb(%rsp), %r14
movb $0x0, (%r14)
callq 0xc5a3
movl %eax, %r15d
leal -0x1(%r15), %eax
movzwl %ax, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xf267
movl %eax, %ebx
movb (%r14), %al
movzwl %r15w, %r14d
cmpb $0x1, %al
jne 0xfd6b
movb %al, 0xa(%rsp)
movq 0x85ab(%rip), %r15      # 0x17fd0
movq (%r15), %rax
movq -0x18(%rax), %rax
leaq (%r15,%rax), %r12
cmpb $0x0, 0xe1(%r15,%rax)
jne 0xfa59
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x2d, 0xe0(%r12)
movslq 0x28beb(%rip), %rax     # 0x38654
movq (%r15), %rcx
movq -0x18(%rcx), %rcx
movq %rax, 0x10(%r15,%rcx)
leaq 0x39ec(%rip), %rsi      # 0x13468
movl $0x1, %edx
movq %r15, %rdi
callq 0x3300
leaq 0x3a02(%rip), %rsi      # 0x13492
movl $0x11, %edx
movq %r15, %rdi
callq 0x3300
movq %r15, %rdi
movl %ebx, %esi
callq 0x3420
movq %rax, %r12
leaq 0x3568(%rip), %rsi      # 0x13019
movl $0x3, %edx
movq %rax, %rdi
callq 0x3300
leaq 0x8a39(%rip), %rax      # 0x184fe
movzwl (%rax), %r13d
movq (%r12), %rax
movq -0x18(%rax), %rcx
movl 0x18(%r12,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%r12,%rcx)
movq -0x18(%rax), %rax
leaq (%r12,%rax), %rbp
cmpb $0x0, 0xe1(%r12,%rax)
jne 0xfb0e
movq %rbp, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%rbp)
movb $0x1, 0xe1(%rbp)
movb $0x30, 0xe0(%rbp)
movq (%r12), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%r12,%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3190
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x3957(%rip), %rsi      # 0x134a4
movl $0x3, %edx
movq %r12, %rdi
callq 0x3300
movq (%r12), %rax
movq -0x18(%rax), %rcx
movl 0x18(%r12,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%r12,%rcx)
movq -0x18(%rax), %rax
movq %r12, %r13
addq %rax, %r13
cmpb $0x0, 0xe1(%r12,%rax)
jne 0xfba3
movq %r13, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r13)
movb $0x1, 0xe1(%r13)
movb $0x30, 0xe0(%r13)
movq (%r12), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%r12,%rax)
movq %r12, %rdi
movq %r14, %rsi
callq 0x3190
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x3897(%rip), %rsi      # 0x1347a
movl $0xb, %edx
movq %r12, %rdi
callq 0x3300
movl 0x28a5e(%rip), %esi     # 0x38654
movq %r12, %rdi
callq 0x3420
leaq 0x3881(%rip), %rsi      # 0x13486
movl $0x8, %edx
movq %rax, %rdi
callq 0x3300
cmpb $0x0, 0x28a3f(%rip)     # 0x38658
jne 0xfc21
decl 0x28a33(%rip)           # 0x38654
movq %r14, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
cmpl $0x0, 0x28a23(%rip)     # 0x38654
jns 0xfc6e
leaq 0x386e(%rip), %rsi      # 0x134a8
movl $0x4, %edx
movq %r15, %rdi
callq 0x3300
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
leaq 0x88a3(%rip), %rbx      # 0x18518
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0xfd34
xorl %r14d, %r14d
leaq 0x37dc(%rip), %r12      # 0x13468
movzbl (%rax,%r14), %ebp
movq (%r15), %rax
movq -0x18(%rax), %rcx
movl 0x18(%r15,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%r15,%rcx)
movq -0x18(%rax), %rax
leaq (%r15,%rax), %r13
cmpb $0x0, 0xe1(%r15,%rax)
jne 0xfcd7
movq %r13, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r13)
movb $0x1, 0xe1(%r13)
movb $0x30, 0xe0(%r13)
movq (%r15), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%r15,%rax)
movq %r15, %rdi
movl %ebp, %esi
callq 0x3420
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movl $0x1, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x3300
incq %r14
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
subq %rax, %rcx
cmpq %rcx, %r14
jb 0xfc8c
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
cmpb $0x0, 0xa(%rsp)
movl 0xc(%rsp), %ebx
movq 0x10(%rsp), %r14
jne 0xfd73
movl %r14d, %edi
callq 0xc4e4
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq