// File: /workspace/github_c_makefile_star2_O1/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: parseExpr
void parseExpr(Compiler *comp, const Type **type, Const *constant)
{
    parseLogicalExpr(comp, type, constant);

    // "?"
    if (comp->lex.tok.kind == TOK_QUESTION)
    {
        typeAssertCompatible(&comp->types, comp->boolType, *type);
        lexNext(&comp->lex);

        const Type *leftType = *type, *rightType = *type;

        if (constant)
        {
            Const leftConstantBuf, *leftConstant = &leftConstantBuf;
            parseExpr(comp, &leftType, leftConstant);

            // ":"
            lexEat(&comp->lex, TOK_COLON);

            Const rightConstantBuf, *rightConstant = &rightConstantBuf;
            rightType = leftType;
            parseExpr(comp, &rightType, rightConstant);
            doAssertImplicitTypeConv(comp, leftType, &rightType, rightConstant);

            *constant = constant->intVal ? (*leftConstant) : (*rightConstant);
        }
        else
        {
            genIfCondEpilog(&comp->gen);

            // Left-hand side expression
            blocksEnter(&comp->blocks);

            parseExpr(comp, &leftType, NULL);

            const Ident *result = NULL;
            if (typeGarbageCollected(leftType))
            {
                // Create a temporary result variable in the outer block, so that it could outlive both left- and right-hand side expression blocks
                blocksLeave(&comp->blocks);
                result = identAllocTempVar(&comp->idents, &comp->types, &comp->modules, &comp->blocks, leftType, false);
                blocksReenter(&comp->blocks);

                // Copy result to temporary variable
                genDup(&comp->gen);
                genChangeRefCnt(&comp->gen, TOK_PLUSPLUS, leftType);
                doPushVarPtr(comp, result);
                genSwapAssign(&comp->gen, result->type->kind, typeSize(&comp->types, result->type));
            }

            doGarbageCollection(comp);
            identWarnIfUnusedAll(&comp->idents, blocksCurrent(&comp->blocks));
            blocksLeave(&comp->blocks);

            // ":"
            lexEat(&comp->lex, TOK_COLON);
            genElseProlog(&comp->gen);

            // Right-hand side expression
            blocksEnter(&comp->blocks);

            rightType = leftType;
            parseExpr(comp, &rightType, NULL);
            doAssertImplicitTypeConv(comp, leftType, &rightType, NULL);

            if (typeGarbageCollected(leftType))
            {
                // Copy result to temporary variable
                genDup(&comp->gen);
                genChangeRefCnt(&comp->gen, TOK_PLUSPLUS, leftType);
                doPushVarPtr(comp, result);
                genSwapAssign(&comp->gen, result->type->kind, typeSize(&comp->types, result->type));
            }

            doGarbageCollection(comp);
            identWarnIfUnusedAll(&comp->idents, blocksCurrent(&comp->blocks));
            blocksLeave(&comp->blocks);

            genIfElseEpilog(&comp->gen);
        }

        *type = leftType;
    }

    if ((*type)->kind == TYPE_VOID)
        comp->error.handler(comp->error.context, "Void expression is not allowed");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdx, %r12
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
callq 0x1762d
movl 0x4bd0(%rbx), %eax
cmpl $0x2b, %eax
jne 0x162e8
leaq 0x4ba8(%rbx), %r15
leaq 0x4e70(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x4220(%rbx), %rax
movq %rax, 0x38(%rsp)
leaq 0x4e38(%rbx), %rax
movq %rax, 0x18(%rsp)
movq %r15, 0x28(%rsp)
movq %r15, %rdi
callq 0x1a984
testq %r12, %r12
je 0x161fd
cmpq $0x0, (%r12)
je 0x1628c
movq $0x1, (%r12)
jmp 0x162d9
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x19470
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0xd774
movq 0x8(%rsp), %r14
movq (%r14), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %rsp, %r13
movq %r13, %rsi
xorl %edx, %edx
callq 0x1762d
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x2b, %r9d
pushq $0x1
pushq $0x0
callq 0x12aaf
addq $0x10, %rsp
movq %rbx, %rdi
callq 0x1bef8
movq %rbp, %rdi
callq 0xd824
movq 0x18(%rsp), %rdi
movl %eax, %esi
callq 0x1a60e
movq %rbp, %rdi
callq 0xd7f2
movq %r15, %rdi
movq 0x28(%rsp), %r15
movl $0x2b, %esi
callq 0x194cc
jmp 0x162d9
movq 0x8(%rsp), %r14
movq (%r14), %rax
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
movq %rsp, %r13
movq %r13, %rdx
callq 0x1762d
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r13, %r8
movl $0x2b, %r9d
pushq $0x1
pushq $0x0
callq 0x12aaf
addq $0x10, %rsp
movq (%rsp), %rax
movq %rax, (%r12)
movl 0x4bd0(%rbx), %eax
cmpl $0x2b, %eax
je 0x161d8
cmpl $0x32, %eax
jne 0x165a5
leaq 0x4ba8(%rbx), %rbp
leaq 0x4e18(%rbx), %r15
movq 0x5268(%rbx), %rsi
movq 0x8(%rsp), %r14
movq (%r14), %rdx
movq %r15, %rdi
callq 0x1f2a6
movq %rbp, %rdi
callq 0x1a984
movq (%r14), %rax
movq %rax, (%rsp)
movq %rax, 0x20(%rsp)
testq %r12, %r12
je 0x163b9
movq %rsp, %r13
leaq 0x40(%rsp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x1617b
movq %rbp, %rdi
movl $0x41, %esi
callq 0x1b41f
movq (%r13), %rax
leaq 0x20(%rsp), %rbp
movq %rax, (%rbp)
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x1617b
movq (%r13), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x11c15
movq (%rbp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1f2a6
cmpq $0x0, (%r12)
leaq 0x30(%rsp), %rax
cmoveq %rax, %r14
movq (%r14), %rax
movq %rax, (%r12)
jmp 0x16599
movq %r15, 0x18(%rsp)
leaq 0x4e70(%rbx), %r12
movq %r12, %rdi
callq 0x18fd3
leaq 0x4220(%rbx), %r13
movq %r13, %rdi
callq 0xd774
movq $0x0, 0x10(%rsp)
movq %rsp, %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1617b
movq (%r14), %rdi
xorl %esi, %esi
callq 0x1eeaf
testb %al, %al
je 0x16487
movq %r13, %rdi
callq 0xd7f2
leaq 0x4e38(%rbx), %rdi
leaq 0x100(%rbx), %rdx
movq (%rsp), %r8
movq 0x18(%rsp), %r15
movq %r15, %rsi
movq %r13, %rcx
xorl %r9d, %r9d
callq 0x1a429
movq %rax, %r14
movq %r13, %rdi
callq 0xd81d
movq %r12, %rdi
callq 0x18271
movq (%rsp), %rdx
movq %r12, %rdi
movl $0x2c, %esi
callq 0x18496
movq %rbx, %rdi
movq %r14, %rsi
callq 0x11b32
movq %r14, 0x10(%rsp)
movq 0x108(%r14), %rsi
movl (%rsi), %r14d
movq %r15, %rdi
callq 0x1ede7
movq %r12, %rdi
movl %r14d, %esi
movl %eax, %edx
callq 0x183d5
movq %rbx, %rdi
callq 0x1bef8
leaq 0x4e38(%rbx), %r14
movq %r13, %rdi
callq 0xd824
movq %r14, 0x28(%rsp)
movq %r14, %rdi
movl %eax, %esi
callq 0x1a60e
movq %r13, %rdi
callq 0xd7f2
movq %rbp, %rdi
movl $0x41, %esi
callq 0x1b41f
movq %r12, %rdi
callq 0x19045
movq %r13, %rdi
callq 0xd774
movq (%rsp), %rax
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x1617b
movq (%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x11c15
movq (%r14), %rdx
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x1f2a6
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x1eeaf
testb %al, %al
je 0x1656d
movq %r12, %rdi
callq 0x18271
movq (%rsp), %rdx
movq %r12, %rdi
movl $0x2c, %esi
callq 0x18496
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x11b32
movq 0x108(%r15), %rsi
movl (%rsi), %ebp
movq %r14, %rdi
callq 0x1ede7
movq %r12, %rdi
movl %ebp, %esi
movl %eax, %edx
callq 0x183d5
movq %rbx, %rdi
callq 0x1bef8
movq %r13, %rdi
callq 0xd824
movq 0x28(%rsp), %rdi
movl %eax, %esi
callq 0x1a60e
movq %r13, %rdi
callq 0xd7f2
movq %r12, %rdi
callq 0x190b9
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rsp), %rax
movq (%rax), %rax
cmpl $0x2, (%rax)
jne 0x165c8
movq 0x5118(%rbx), %rdi
leaq 0x6123b(%rip), %rsi     # 0x777fb
xorl %eax, %eax
callq *0x50f8(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq