// File: /workspace/github_c_makefile_star2_O0/vtereshkov[P]umka-lang/src/umka_expr.c
// Function: parseBuiltinSortCall
static void parseBuiltinSortCall(Compiler *comp, const Type **type, Const *constant)
{
    if (constant)
        comp->error.handler(comp->error.context, "Function is not allowed in constant expressions");

    // Dynamic array
    *type = NULL;
    parseExpr(comp, type, NULL);
    typeAssertCompatibleBuiltin(&comp->types, *type, BUILTIN_SORT, (*type)->kind == TYPE_DYNARRAY);

    lexEat(&comp->lex, TOK_COMMA);

    // Compare closure or ascending/descending order flag
    Type *fnType = typeAdd(&comp->types, &comp->blocks, TYPE_FN);
    const Type *paramType = typeAddPtrTo(&comp->types, &comp->blocks, (*type)->base);

    typeAddParam(&comp->types, &fnType->sig, comp->anyType, "#upvalues", (Const){0});
    typeAddParam(&comp->types, &fnType->sig, paramType, "a", (Const){0});
    typeAddParam(&comp->types, &fnType->sig, paramType, "b", (Const){0});

    fnType->sig.resultType = comp->intType;

    Type *expectedCompareType = typeAdd(&comp->types, &comp->blocks, TYPE_CLOSURE);
    typeAddField(&comp->types, expectedCompareType, fnType, "#fn");
    typeAddField(&comp->types, expectedCompareType, comp->anyType, "#upvalues");

    const Type *compareOrFlagType = expectedCompareType;
    parseExpr(comp, &compareOrFlagType, NULL);

    if (typeEquivalent(compareOrFlagType, comp->boolType))
    {
        // "Fast" form

        // Dynamic array item type must be either a simple comparable type, or a structure whose field having the given name is of a simple comparable type
        if (typeValidOperator((*type)->base, TOK_LESS))
        {
            genPushIntConst(&comp->gen, 0);
            genCallBuiltin(&comp->gen, (*type)->base->kind, BUILTIN_SORTFAST);
        }
        else
        {
            typeAssertCompatibleBuiltin(&comp->types, *type, BUILTIN_SORT, (*type)->base->kind == TYPE_STRUCT);

            // Field name
            lexEat(&comp->lex, TOK_COMMA);
            lexCheck(&comp->lex, TOK_IDENT);

            const Field *field = typeAssertFindField(&comp->types, (*type)->base, comp->lex.tok.name, NULL);
            typeAssertValidOperator(&comp->types, field->type, TOK_LESS);

            lexNext(&comp->lex);

            genPushIntConst(&comp->gen, field->offset);
            genCallBuiltin(&comp->gen, field->type->kind, BUILTIN_SORTFAST);
        }
    }
    else
    {
        // "General" form

        // Compare closure type (hidden parameter)
        doAssertImplicitTypeConv(comp, expectedCompareType, &compareOrFlagType, NULL);
        genPushGlobalPtr(&comp->gen, (Type *)compareOrFlagType);

        genCallBuiltin(&comp->gen, TYPE_DYNARRAY, BUILTIN_SORT);
    }

    *type = comp->voidType;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x1221c
movq -0x8(%rbp), %rax
movq 0x50f8(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x5118(%rax), %rdi
leaq 0x1c6ac3(%rip), %rsi    # 0x1d8cdb
movb $0x0, %al
callq *%rcx
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xe8c0
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rax
cmpl $0x13, (%rax)
sete %al
movzbl %al, %ecx
andl $0x1, %ecx
movl $0x20, %edx
callq 0x229e0
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x3f, %esi
callq 0x1b150
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rsi
addq $0x4220, %rsi           # imm = 0x4220
movl $0x1a, %edx
callq 0x21950
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rsi
addq $0x4220, %rsi           # imm = 0x4220
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdx
callq 0x21fa0
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x20(%rbp), %rsi
addq $0x28, %rsi
movq -0x8(%rbp), %rax
movq 0x52a8(%rax), %rdx
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %r8
leaq 0x1c20ed(%rip), %rcx    # 0x1d43e8
callq 0x23790
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x20(%rbp), %rsi
addq $0x28, %rsi
movq -0x28(%rbp), %rdx
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %r8
leaq 0x1c20d0(%rip), %rcx    # 0x1d43fa
callq 0x23790
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x20(%rbp), %rsi
addq $0x28, %rsi
movq -0x28(%rbp), %rdx
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %r8
leaq 0x1c72f0(%rip), %rcx    # 0x1d9649
callq 0x23790
movq -0x8(%rbp), %rax
movq 0x5240(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x8(%rbp), %rsi
addq $0x4220, %rsi           # imm = 0x4220
movl $0x18, %edx
callq 0x21950
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x48(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x1c2040(%rip), %rcx    # 0x1d43f2
callq 0x23070
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x48(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x52a8(%rax), %rdx
leaq 0x1c2010(%rip), %rcx    # 0x1d43e8
callq 0x23070
movq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xe8c0
movq -0x50(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x5268(%rax), %rsi
callq 0x22250
testb $0x1, %al
jne 0x12413
jmp 0x1256b
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rdi
movl $0x2f, %esi
callq 0x22a80
testb $0x1, %al
jne 0x1242e
jmp 0x12469
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 -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movl (%rax), %esi
movl $0x21, %edx
callq 0x18040
jmp 0x12569
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rax
movq (%rax), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x16, (%rax)
sete %al
movzbl %al, %ecx
andl $0x1, %ecx
movl $0x20, %edx
callq 0x229e0
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x3f, %esi
callq 0x1b150
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
movl $0x45, %esi
callq 0x1b080
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq -0x8(%rbp), %rdx
addq $0x4ba8, %rdx           # imm = 0x4BA8
addq $0x28, %rdx
addq $0x8, %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0x23000
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
addq $0x4e18, %rdi           # imm = 0x4E18
movq -0x58(%rbp), %rax
movq 0x108(%rax), %rsi
movl $0x2f, %edx
callq 0x22db0
movq -0x8(%rbp), %rdi
addq $0x4ba8, %rdi           # imm = 0x4BA8
callq 0x1ad90
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x58(%rbp), %rax
movslq 0x110(%rax), %rsi
callq 0x16850
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x58(%rbp), %rax
movq 0x108(%rax), %rax
movl (%rax), %esi
movl $0x21, %edx
callq 0x18040
jmp 0x125ae
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
leaq -0x50(%rbp), %rdx
xorl %eax, %eax
movl %eax, %ecx
callq 0xd6e0
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movq -0x50(%rbp), %rsi
callq 0x16950
movq -0x8(%rbp), %rdi
addq $0x4e70, %rdi           # imm = 0x4E70
movl $0x13, %esi
movl $0x20, %edx
callq 0x18040
movq -0x8(%rbp), %rax
movq 0x5218(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)