// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %rdi, %rbx
leaq 0x3(%rsp), %r14
movb $0x0, (%r14)
callq 0xc554
movl %eax, %r15d
leal -0x1(%r15), %eax
movzwl %ax, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0xf0fe
movl %eax, %ebx
cmpb $0x1, (%r14)
jne 0xfb96
movq 0x875b(%rip), %r14      # 0x17fd0
movq (%r14), %rax
movq -0x18(%rax), %rcx
leaq (%r14,%rcx), %r12
cmpb $0x0, 0xe1(%r14,%rcx)
jne 0xf8a4
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r12)
movq (%r14), %rax
movb $0x2d, 0xe0(%r12)
movslq 0x28da0(%rip), %rcx     # 0x38654
movq -0x18(%rax), %rax
movq %rcx, 0x10(%r14,%rax)
leaq 0x3ba4(%rip), %rsi      # 0x13468
movl $0x1, %edx
movq %r14, %rdi
callq 0x3300
leaq 0x3bba(%rip), %rsi      # 0x13492
movl $0x11, %edx
movq %r14, %rdi
callq 0x3300
movq %r14, %rdi
movl %ebx, %esi
callq 0x3420
movq %rax, %r12
leaq 0x3720(%rip), %rsi      # 0x13019
movl $0x3, %edx
movq %rax, %rdi
callq 0x3300
leaq 0x8bf1(%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), %rcx
leaq (%r12,%rcx), %rbp
cmpb $0x0, 0xe1(%r12,%rcx)
jne 0xf954
movq %rbp, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%rbp)
movq (%r12), %rax
movb $0x30, 0xe0(%rbp)
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 0x3b15(%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), %rcx
movq %r12, %r13
addq %rcx, %r13
cmpb $0x0, 0xe1(%r12,%rcx)
jne 0xf9e2
movq %r13, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r13)
movq (%r12), %rax
movb $0x30, 0xe0(%r13)
movq -0x18(%rax), %rax
movq $0x4, 0x10(%r12,%rax)
movzwl %r15w, %esi
movq %r12, %rdi
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 0x3a5b(%rip), %rsi      # 0x1347a
movl $0xb, %edx
movq %r12, %rdi
callq 0x3300
movl 0x28c22(%rip), %esi     # 0x38654
movq %r12, %rdi
callq 0x3420
leaq 0x3a45(%rip), %rsi      # 0x13486
movl $0x8, %edx
movq %rax, %rdi
callq 0x3300
movl 0x28c00(%rip), %eax     # 0x38654
cmpb $0x0, 0x28bfd(%rip)     # 0x38658
movl %ebx, 0x4(%rsp)
jne 0xfa69
decl %eax
movl %eax, 0x28beb(%rip)     # 0x38654
testl %eax, %eax
jns 0xfaa8
leaq 0x3a34(%rip), %rsi      # 0x134a8
movl $0x4, %edx
movq %r14, %rdi
callq 0x3300
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
leaq 0x8a69(%rip), %r13      # 0x18518
movq (%r13), %rax
cmpq %rax, 0x8(%r13)
je 0xfb69
xorl %ebx, %ebx
leaq 0x39a2(%rip), %r15      # 0x13468
movzbl (%rax,%rbx), %ebp
movq (%r14), %rax
movq -0x18(%rax), %rcx
movl 0x18(%r14,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%r14,%rcx)
movq -0x18(%rax), %rcx
leaq (%r14,%rcx), %r12
cmpb $0x0, 0xe1(%r14,%rcx)
jne 0xfb0d
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r12)
movq (%r14), %rax
movb $0x30, 0xe0(%r12)
movq -0x18(%rax), %rax
movq $0x2, 0x10(%r14,%rax)
movq %r14, %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 %r14, %rdi
movq %r15, %rsi
callq 0x3300
incq %rbx
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
cmpq %rcx, %rbx
jb 0xfac6
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movl 0x4(%rsp), %ebx
jmp 0xfb9f
movzwl %r15w, %edi
callq 0xc490
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq