// File: /workspace/github_c_makefile_star2_O0/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: parseBuiltinIOCall
static void parseBuiltinIOCall(Compiler *comp, const Type **type, Const *constant, BuiltinFunc builtin)
{
    if (constant)
        comp->error.handler(comp->error.context, "Function is not allowed in constant expressions");

    // Parameters: count, stream, format, value

    // Count (number of characters for printf(), number of items for scanf())
    genPushIntConst(&comp->gen, 0);

    // Stream (file/string pointer)
    if (builtin == BUILTIN_FPRINTF || builtin == BUILTIN_FSCANF  || builtin == BUILTIN_SSCANF)
    {
        const Type *expectedType = (builtin == BUILTIN_FPRINTF || builtin == BUILTIN_FSCANF) ? comp->fileType : comp->strType;
        *type = expectedType;
        parseExpr(comp, type, constant);
        doAssertImplicitTypeConv(comp, expectedType, type, constant);
        lexEat(&comp->lex, TOK_COMMA);
    }
    else
        genPushGlobalPtr(&comp->gen, NULL);

    // Format string
    *type = comp->strType;
    parseExpr(comp, type, constant);
    typeAssertCompatible(&comp->types, comp->strType, *type);

    // Values, if any
    while (comp->lex.tok.kind == TOK_COMMA)
    {
        lexNext(&comp->lex);
        *type = NULL;
        parseExpr(comp, type, constant);

        if (builtin == BUILTIN_PRINTF || builtin == BUILTIN_FPRINTF || builtin == BUILTIN_SPRINTF)
        {
            typeAssertCompatibleBuiltin(&comp->types, *type, builtin, (*type)->kind != TYPE_VOID);
            genCallTypedBuiltin(&comp->gen, *type, builtin);
        }
        else  // BUILTIN_SCANF, BUILTIN_FSCANF, BUILTIN_SSCANF
        {
            typeAssertCompatibleBuiltin(&comp->types, *type, builtin, (*type)->kind == TYPE_PTR && (typeOrdinal((*type)->base) || typeReal((*type)->base) || (*type)->base->kind == TYPE_STR));
            genCallTypedBuiltin(&comp->gen, (*type)->base, builtin);
        }
    } // while

    // The rest of format string
    genPushIntConst(&comp->gen, 0);
    genCallTypedBuiltin(&comp->gen, comp->voidType, builtin);

    genPop(&comp->gen);  // Remove format string

    // Result
    if (builtin == BUILTIN_SPRINTF)
    {
        genSwap(&comp->gen);                    // Swap stream and count
        genPop(&comp->gen);                     // Remove count, keep stream
        *type = comp->strType;
    }
    else
    {
        genPop(&comp->gen);                     // Remove stream, keep count
        *type = comp->intType;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x1104f
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
leaq 0x1c7c90(%rip), %rsi    # 0x1d8cdb
movb $0x0, %al
callq *%rcx
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
xorl %eax, %eax
movl %eax, %esi
callq 0x16850
cmpl $0x1, -0x1c(%rbp)
je 0x11075
cmpl $0x4, -0x1c(%rbp)
je 0x11075
cmpl $0x5, -0x1c(%rbp)
jne 0x110f1
cmpl $0x1, -0x1c(%rbp)
je 0x11081
cmpl $0x4, -0x1c(%rbp)
jne 0x11092
movq -0x8(%rbp), %rax
movq 0x52b0(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x110a1
movq -0x8(%rbp), %rax
movq 0x5288(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0xe8c0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0xd6e0
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x3f, %esi
callq 0x1b150
jmp 0x11105
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
xorl %eax, %eax
movl %eax, %esi
callq 0x16950
movq -0x8(%rbp), %rax
movq 0x5288(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0xe8c0
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rax
movq 0x5288(%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
callq 0x22870
movq -0x8(%rbp), %rax
cmpl $0x3f, 0x4bd0(%rax)
jne 0x112a3
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
callq 0x1ad90
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0xe8c0
cmpl $0x0, -0x1c(%rbp)
je 0x11199
cmpl $0x1, -0x1c(%rbp)
je 0x11199
cmpl $0x2, -0x1c(%rbp)
jne 0x111e5
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl -0x1c(%rbp), %edx
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x2, (%rax)
setne %al
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x229e0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movl -0x1c(%rbp), %edx
callq 0x182e0
jmp 0x1129e
movq -0x8(%rbp), %rax
addq $0x4e18, %rax           # imm = 0x4E18
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
xorl %eax, %eax
cmpl $0x10, (%rcx)
movb %al, -0x31(%rbp)
jne 0x11267
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
callq 0xcf60
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x49(%rbp)
jne 0x11261
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
callq 0xcf80
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x49(%rbp)
jne 0x11261
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x14, (%rax)
sete %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
movb %al, -0x31(%rbp)
movl -0x38(%rbp), %edx
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdi
movb -0x31(%rbp), %al
movzbl %al, %ecx
andl $0x1, %ecx
callq 0x229e0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movl -0x1c(%rbp), %edx
callq 0x182e0
jmp 0x1114a
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
xorl %eax, %eax
movl %eax, %esi
callq 0x16850
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x8(%rbp), %rax
movq 0x5218(%rax), %rsi
movl -0x1c(%rbp), %edx
callq 0x182e0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
callq 0x16d30
cmpl $0x2, -0x1c(%rbp)
jne 0x1131f
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
callq 0x16f00
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
callq 0x16d30
movq -0x8(%rbp), %rax
movq 0x5288(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x11341
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
callq 0x16d30
movq -0x8(%rbp), %rax
movq 0x5240(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)