// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %rdx, %r13
movq %rdi, %rbx
movq %rsi, 0x20(%rsp)
callq 0xe239
leaq 0x4ba8(%rbx), %rbp
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 %rbp, 0x40(%rsp)
movl 0x4bd0(%rbx), %eax
cmpl $0x2b, %eax
jne 0xd50f
movq %rbp, %rdi
callq 0x10f6e
testq %r13, %r13
je 0xd424
cmpq $0x0, (%r13)
je 0xd4bd
movq $0x1, (%r13)
jmp 0xd3f3
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0xfca2
movq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0x4b13
movq 0x20(%rsp), %r12
movq (%r12), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq %rbx, %r15
leaq 0x8(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq 0xe239
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r15, %rbx
xorl %ecx, %ecx
xorl %r8d, %r8d
pushq $0x2b
popq %r15
movl %r15d, %r9d
pushq $0x1
pushq $0x0
callq 0x9f5c
popq %rax
popq %rcx
movq %rbx, %rdi
callq 0x12350
movq %r14, %rdi
callq 0x4bcc
movq 0x18(%rsp), %rdi
movl %eax, %esi
callq 0x10c75
movq %r14, %rdi
callq 0x4b91
movq %rbp, %rdi
movq 0x40(%rsp), %rbp
movl %r15d, %esi
callq 0xfcc4
jmp 0xd3f3
movq 0x20(%rsp), %r12
movq (%r12), %rax
movq %rax, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x28(%rsp), %r15
movq %r15, %rsi
leaq 0x8(%rsp), %r14
movq %r14, %rdx
callq 0xe239
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %r14, %r8
pushq $0x2b
popq %r9
pushq $0x1
pushq $0x0
callq 0x9f5c
popq %rax
popq %rcx
movq 0x8(%rsp), %rax
movq %rax, (%r13)
jmp 0xd3f3
movq 0x20(%rsp), %r15
movq (%r15), %rdx
cmpl $0x32, %eax
jne 0xd772
leaq 0x4e18(%rbx), %r14
movq 0x5268(%rbx), %rsi
movq %r14, %rdi
callq 0x15600
movq %rbp, %rdi
callq 0x10f6e
movq (%r15), %rax
movq %rax, 0x8(%rsp)
movq %rax, 0x28(%rsp)
testq %r13, %r13
je 0xd5bd
leaq 0x8(%rsp), %r15
leaq 0x50(%rsp), %r14
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xd3a5
pushq $0x41
popq %rsi
movq %rbp, %rdi
callq 0x118d3
movq (%r15), %rax
leaq 0x28(%rsp), %r12
movq %rax, (%r12)
leaq 0x48(%rsp), %rbp
movq %rbx, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xd3a5
movq (%r15), %rsi
movq 0x20(%rsp), %r15
movq %rbx, %rdi
movq %r12, %rdx
movq %rbp, %rcx
callq 0x97b3
cmpq $0x0, (%r13)
cmoveq %rbp, %r14
movq (%r14), %rax
movq %rax, (%r13)
jmp 0xd76a
movq %r14, 0x30(%rsp)
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0xf9ff
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0x4b13
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xd3a5
movq (%r14), %rdi
callq 0xd79c
testb %al, %al
je 0xd675
movq %rbp, %rdi
callq 0x4b91
leaq 0x100(%rbx), %rdx
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %rbp, %rcx
xorl %r9d, %r9d
callq 0x10abb
movq %rax, %r13
movq %rbp, %rdi
callq 0x4bc5
movq %r12, %rdi
callq 0xedd1
movq 0x8(%rsp), %rdx
pushq $0x2c
popq %rsi
movq %r12, %rdi
callq 0xef90
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8c92
movq 0x108(%r13), %rsi
movl (%rsi), %r14d
movq 0x30(%rsp), %rdi
callq 0x15124
movq %r12, %rdi
movl %r14d, %esi
movl %eax, %edx
callq 0xeee9
movq %rbx, %rdi
callq 0x12350
movq %rbp, %rdi
callq 0x4bcc
movq 0x18(%rsp), %rdi
movl %eax, %esi
callq 0x10c75
movq %rbp, %rdi
callq 0x4b91
pushq $0x41
popq %rsi
movq 0x40(%rsp), %rdi
callq 0x118d3
movq %r12, %rdi
callq 0xfa54
movq %rbp, %rdi
callq 0x4b13
movq 0x8(%rsp), %rax
leaq 0x28(%rsp), %r14
movq %rax, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xd3a5
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x97b3
movq 0x8(%rsp), %rdi
callq 0xd79c
testb %al, %al
je 0xd73e
movq %r12, %rdi
callq 0xedd1
movq 0x8(%rsp), %rdx
pushq $0x2c
popq %rsi
movq %r12, %rdi
callq 0xef90
movq %rbx, %rdi
movq %r13, %rsi
callq 0x8c92
movq 0x108(%r13), %rsi
movl (%rsi), %ebp
movq 0x30(%rsp), %rdi
callq 0x15124
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movq 0x38(%rsp), %rbp
movq 0x10(%rsp), %r12
movl %eax, %edx
callq 0xeee9
movq %rbx, %rdi
callq 0x12350
movq %rbp, %rdi
callq 0x4bcc
movq 0x18(%rsp), %rdi
movl %eax, %esi
callq 0x10c75
movq %rbp, %rdi
callq 0x4b91
movq %r12, %rdi
callq 0xfa87
movq 0x8(%rsp), %rdx
movq %rdx, (%r15)
cmpl $0x2, (%rdx)
jne 0xd78d
movq 0x5118(%rbx), %rdi
leaq 0x5d01a(%rip), %rsi     # 0x6a79f
xorl %eax, %eax
callq *0x50f8(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq