// File: /workspace/github_c_makefile_star2_O3/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 0x1263c
movl 0x4bd0(%rbx), %eax
cmpl $0x2b, %eax
jne 0x11330
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 0x15914
testq %r12, %r12
je 0x11245
cmpq $0x0, (%r12)
je 0x112d4
movq $0x1, (%r12)
jmp 0x11321
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x14434
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0x8503
movq 0x8(%rsp), %r14
movq (%r14), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
movq %rsp, %r13
movq %r13, %rsi
xorl %edx, %edx
callq 0x1263c
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x2b, %r9d
pushq $0x1
pushq $0x0
callq 0xd9c7
addq $0x10, %rsp
movq %rbx, %rdi
callq 0x16e84
movq %rbp, %rdi
callq 0x85bf
movq 0x18(%rsp), %rdi
movl %eax, %esi
callq 0x1559f
movq %rbp, %rdi
callq 0x8584
movq %r15, %rdi
movq 0x28(%rsp), %r15
movl $0x2b, %esi
callq 0x14490
jmp 0x11321
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 0x1263c
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r13, %r8
movl $0x2b, %r9d
pushq $0x1
pushq $0x0
callq 0xd9c7
addq $0x10, %rsp
movq (%rsp), %rax
movq %rax, (%r12)
movl 0x4bd0(%rbx), %eax
cmpl $0x2b, %eax
je 0x11220
movq 0x8(%rsp), %r14
movq (%r14), %rdx
cmpl $0x32, %eax
jne 0x115ed
leaq 0x4ba8(%rbx), %rbp
leaq 0x4e18(%rbx), %r15
movq 0x5268(%rbx), %rsi
movq %r15, %rdi
callq 0x1a277
movq %rbp, %rdi
callq 0x15914
movq (%r14), %rax
movq %rax, (%rsp)
movq %rax, 0x20(%rsp)
testq %r12, %r12
je 0x11401
movq %rsp, %r13
leaq 0x40(%rsp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x111c3
movq %rbp, %rdi
movl $0x41, %esi
callq 0x1637d
movq (%r13), %rax
leaq 0x20(%rsp), %rbp
movq %rax, (%rbp)
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x111c3
movq (%r13), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xcabc
movq (%rbp), %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x1a277
cmpq $0x0, (%r12)
leaq 0x30(%rsp), %rax
cmoveq %rax, %r14
movq (%r14), %rax
movq %rax, (%r12)
jmp 0x115e1
movq %r15, 0x18(%rsp)
leaq 0x4e70(%rbx), %r12
movq %r12, %rdi
callq 0x13f8b
leaq 0x4220(%rbx), %r13
movq %r13, %rdi
callq 0x8503
movq $0x0, 0x10(%rsp)
movq %rsp, %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x111c3
movq (%r14), %rdi
xorl %esi, %esi
callq 0x19ef5
testb %al, %al
je 0x114cf
movq %r13, %rdi
callq 0x8584
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 0x153ba
movq %rax, %r14
movq %r13, %rdi
callq 0x85b8
movq %r12, %rdi
callq 0x13273
movq (%rsp), %rdx
movq %r12, %rdi
movl $0x2c, %esi
callq 0x13476
movq %rbx, %rdi
movq %r14, %rsi
callq 0xc9da
movq %r14, 0x10(%rsp)
movq 0x108(%r14), %rsi
movl (%rsi), %r14d
movq %r15, %rdi
callq 0x19e29
movq %r12, %rdi
movl %r14d, %esi
movl %eax, %edx
callq 0x133bf
movq %rbx, %rdi
callq 0x16e84
leaq 0x4e38(%rbx), %r14
movq %r13, %rdi
callq 0x85bf
movq %r14, 0x28(%rsp)
movq %r14, %rdi
movl %eax, %esi
callq 0x1559f
movq %r13, %rdi
callq 0x8584
movq %rbp, %rdi
movl $0x41, %esi
callq 0x1637d
movq %r12, %rdi
callq 0x13ffd
movq %r13, %rdi
callq 0x8503
movq (%rsp), %rax
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x111c3
movq (%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xcabc
movq (%r14), %rdx
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x1a277
movq (%rsp), %rdi
xorl %esi, %esi
callq 0x19ef5
testb %al, %al
je 0x115b5
movq %r12, %rdi
callq 0x13273
movq (%rsp), %rdx
movq %r12, %rdi
movl $0x2c, %esi
callq 0x13476
movq %rbx, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0xc9da
movq 0x108(%r15), %rsi
movl (%rsi), %ebp
movq %r14, %rdi
callq 0x19e29
movq %r12, %rdi
movl %ebp, %esi
movl %eax, %edx
callq 0x133bf
movq %rbx, %rdi
callq 0x16e84
movq %r13, %rdi
callq 0x85bf
movq 0x28(%rsp), %rdi
movl %eax, %esi
callq 0x1559f
movq %r13, %rdi
callq 0x8584
movq %r12, %rdi
callq 0x14071
movq 0x8(%rsp), %rax
movq (%rsp), %rdx
movq %rdx, (%rax)
cmpl $0x2, (%rdx)
jne 0x11608
movq 0x5118(%rbx), %rdi
leaq 0x651fb(%rip), %rsi     # 0x767fb
xorl %eax, %eax
callq *0x50f8(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq