// File: /workspace/github_c_makefile_star2_O0/gaultier[P]lox-c/vm.c
// Function: value_call
static Result value_call(Vm* vm, Value callee, uint8_t arg_count) {
    const Location* const loc = get_location(vm);
    if (!IS_OBJ(callee)) {
        VM_ERROR(vm, loc, "Can only call functions and classes, got: %s",
                 value_to_str_debug(callee));
    }
    LOG("calling fn=%s arg_count=%d\n", value_to_str_debug(callee), arg_count);

    switch (AS_OBJ(callee)->type) {
        case OBJ_FUNCTION:
            return fn_call(vm, AS_FN(callee), arg_count);
        case OBJ_NATIVE: {
            const NativeFn fn = AS_NATIVE(callee)->fn;
            Value ret = {0};
            RETURN_IF_ERR(fn(vm, vm->stack_top - arg_count, arg_count, &ret));
            vm->stack_top -= arg_count + 1;

            RETURN_IF_ERR(stack_push(vm, ret));
            return RES_OK;
        }
        case OBJ_STRING:
            VM_ERROR(vm, loc, "Can only call functions and classes, got: %s",
                     value_to_str_debug(callee));
    }
}

// Assembly:
subq $0x88, %rsp
movb %cl, %al
movl %esi, 0x70(%rsp)
movq %rdx, 0x78(%rsp)
movq %rdi, 0x68(%rsp)
movb %al, 0x67(%rsp)
movq 0x68(%rsp), %rdi
callq 0x71c0
movq %rax, 0x58(%rsp)
cmpl $0x3, 0x70(%rsp)
je 0x99b5
jmp 0x9943
movq 0x66a6(%rip), %rax      # 0xfff0
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x30(%rsp)
movl 0x70(%rsp), %edi
movq 0x78(%rsp), %rsi
callq 0x6ad0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rax, %r8
leaq 0x3c28(%rip), %rsi      # 0xd5bc
movb $0x0, %al
callq 0x1140
movq 0x68(%rsp), %rdi
callq 0x95e0
movl $0x2, 0x84(%rsp)
jmp 0x9b4a
jmp 0x99b7
jmp 0x99b9
movq 0x78(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
testl %eax, %eax
je 0x9add
jmp 0x99ce
movl 0x1c(%rsp), %eax
subl $0x1, %eax
je 0x99e7
jmp 0x99d9
movl 0x1c(%rsp), %eax
subl $0x2, %eax
je 0x9a07
jmp 0x9b4a
movq 0x68(%rsp), %rdi
movq 0x78(%rsp), %rsi
movzbl 0x67(%rsp), %edx
callq 0xa420
movl %eax, 0x84(%rsp)
jmp 0x9b4a
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10f0
movq 0x50(%rsp), %rax
movq 0x68(%rsp), %rdi
movq 0x68(%rsp), %rcx
movq 0x40000(%rcx), %rsi
movzbl 0x67(%rsp), %ecx
movslq %ecx, %rdx
xorl %ecx, %ecx
subq %rdx, %rcx
shlq $0x4, %rcx
addq %rcx, %rsi
leaq 0x40(%rsp), %rcx
movzbl 0x67(%rsp), %edx
callq *%rax
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
je 0x9a77
movl 0x3c(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x9b4a
jmp 0x9a79
movzbl 0x67(%rsp), %edx
addl $0x1, %edx
movq 0x68(%rsp), %rax
movq 0x40000(%rax), %rcx
movslq %edx, %rsi
xorl %edx, %edx
subq %rsi, %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq %rcx, 0x40000(%rax)
movq 0x68(%rsp), %rdi
movl 0x40(%rsp), %esi
movq 0x48(%rsp), %rdx
callq 0x9560
movl %eax, 0x38(%rsp)
cmpl $0x0, 0x38(%rsp)
je 0x9ace
movl 0x38(%rsp), %eax
movl %eax, 0x84(%rsp)
jmp 0x9b4a
jmp 0x9ad0
movl $0x0, 0x84(%rsp)
jmp 0x9b4a
jmp 0x9adf
movq 0x650a(%rip), %rax      # 0xfff0
movq (%rax), %rax
movq %rax, (%rsp)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x58(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x70(%rsp), %edi
movq 0x78(%rsp), %rsi
callq 0x6ad0
movq (%rsp), %rdi
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %rax, %r8
leaq 0x3a8e(%rip), %rsi      # 0xd5bc
movb $0x0, %al
callq 0x1140
movq 0x68(%rsp), %rdi
callq 0x95e0
movl $0x2, 0x84(%rsp)
movl 0x84(%rsp), %eax
addq $0x88, %rsp
retq
nopl (%rax)