// File: /workspace/github_c_makefile_star2_O0/vtereshkov[P]umka-lang/src/umka_stmt.c
// Function: parseReturnStmt
static void parseReturnStmt(Compiler *comp)
{
    lexEat(&comp->lex, TOK_RETURN);
    comp->blocks.item[comp->blocks.top].hasReturn = true;

    // Get function signature
    const Signature *sig = NULL;
    for (int i = comp->blocks.top; i >= 1; i--)
        if (comp->blocks.item[i].fn)
        {
            sig = &comp->blocks.item[i].fn->type->sig;
            break;
        }

    if (!sig)
        comp->error.handler(comp->error.context, "Function block not found");

    const Type *type = sig->resultType;
    if (comp->lex.tok.kind != TOK_SEMICOLON && comp->lex.tok.kind != TOK_IMPLICIT_SEMICOLON && comp->lex.tok.kind != TOK_RBRACE)
        parseExprList(comp, &type, NULL);
    else
        type = comp->voidType;

    doAssertImplicitTypeConv(comp, sig->resultType, &type, NULL);

    // Check non-64-bit ordinal and real types for overflow
    if (sig->resultType->kind != type->kind && typeNarrow(sig->resultType))
        genAssertRange(&comp->gen, sig->resultType->kind, type);

    // Copy structure to #result
    if (typeStructured(sig->resultType))
    {
        const Ident *result = identAssertFind(&comp->idents, &comp->modules, &comp->blocks, comp->blocks.module, "#result", NULL);

        doPushVarPtr(comp, result);
        genDeref(&comp->gen, TYPE_PTR);

        // Assignment to an anonymous stack area (pointed to by #result) does not require updating reference counts
        genSwapAssign(&comp->gen, sig->resultType->kind, typeSize(&comp->types, sig->resultType));

        doPushVarPtr(comp, result);
        genDeref(&comp->gen, TYPE_PTR);
    }

    if (sig->resultType->kind != TYPE_VOID)
    {
        if (doTryRemoveCopyResultToTempVar(comp))
        {
            // Optimization: if the result expression is a function call, assume its reference count to be already increased before the inner return
            // The outer caller will hold this additional reference, so we can remove the temporary "reference holder" variable
        }
        else
        {
            // General case: increase result reference count
            genChangeRefCnt(&comp->gen, TOK_PLUSPLUS, sig->resultType);
        }
        genPopReg(&comp->gen, REG_RESULT);
    }

    doGarbageCollectionDownToBlock(comp, comp->gen.returns->block);
    genGotosAddStub(&comp->gen, comp->gen.returns);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0xf, %esi
callq 0x1b150
movq -0x8(%rbp), %rax
addq $0x4220, %rax           # imm = 0x4220
movq -0x8(%rbp), %rcx
movslq 0x4b84(%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movb $0x1, 0x14(%rax)
movq $0x0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl 0x4b84(%rax), %eax
movl %eax, -0x14(%rbp)
cmpl $0x1, -0x14(%rbp)
jl 0x1f6af
movq -0x8(%rbp), %rax
addq $0x4220, %rax           # imm = 0x4220
movslq -0x14(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
je 0x1f6a2
movq -0x8(%rbp), %rax
addq $0x4220, %rax           # imm = 0x4220
movslq -0x14(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq 0x108(%rax), %rax
addq $0x28, %rax
movq %rax, -0x10(%rbp)
jmp 0x1f6af
jmp 0x1f6a4
movl -0x14(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1f656
cmpq $0x0, -0x10(%rbp)
jne 0x1f6d7
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
leaq 0x1ba546(%rip), %rsi    # 0x1d9c19
movb $0x0, %al
callq *%rcx
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x40, 0x4bd0(%rax)
je 0x1f720
movq -0x8(%rbp), %rax
cmpl $0x4a, 0x4bd0(%rax)
je 0x1f720
movq -0x8(%rbp), %rax
cmpl $0x3d, 0x4bd0(%rax)
je 0x1f720
movq -0x8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xeea0
jmp 0x1f72f
movq -0x8(%rbp), %rax
movq 0x5218(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rsi
leaq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xd6e0
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl (%rax), %eax
movq -0x20(%rbp), %rcx
cmpl (%rcx), %eax
je 0x1f797
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdi
callq 0x21880
testb $0x1, %al
jne 0x1f776
jmp 0x1f797
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl (%rax), %esi
movq -0x20(%rbp), %rdx
callq 0x17cd0
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdi
callq 0x1ecf0
testb $0x1, %al
jne 0x1f7b0
jmp 0x1f87b
movq -0x8(%rbp), %rdi
addq $0x4e38, %rdi           # imm = 0x4E38
movq -0x8(%rbp), %rsi
addq $0x100, %rsi            # imm = 0x100
movq -0x8(%rbp), %rdx
addq $0x4220, %rdx           # imm = 0x4220
movq -0x8(%rbp), %rax
movl 0x4b88(%rax), %ecx
leaq 0x1b8c6d(%rip), %r8     # 0x1d844f
xorl %eax, %eax
movl %eax, %r9d
callq 0x19880
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xd010
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0x10, %esi
callq 0x16fa0
movq -0x8(%rbp), %rax
addq $0x4e70, %rax           # imm = 0x4E70
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rsi
callq 0x21fe0
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl %eax, %edx
callq 0x17120
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xd010
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0x10, %esi
callq 0x16fa0
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
cmpl $0x2, (%rax)
je 0x1f8ce
movq -0x8(%rbp), %rdi
callq 0xd0f0
testb $0x1, %al
jne 0x1f89a
jmp 0x1f89c
jmp 0x1f8bc
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rdx
movl $0x2c, %esi
callq 0x17270
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
xorl %esi, %esi
callq 0x16df0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x5028(%rax), %rax
movl 0x194(%rax), %esi
callq 0x1d580
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x8(%rbp), %rax
movq 0x5028(%rax), %rsi
callq 0x18ce0
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)