// File: /workspace/github_c_makefile_star2_O3/samsonjs[P]lake/src/eval.c
// Function: apply
LakeVal *apply(LakeCtx *ctx, LakeVal *fnVal, LakeList *args)
{
    LakeVal *result = NULL;
    if (lake_is_type(TYPE_PRIM, fnVal))
    {
        LakePrimitive *prim = PRIM(fnVal);
        int arity = prim->arity;
        if (arity == ARITY_VARARGS || LIST_N(args) == arity)
        {
            result = prim->fn(ctx, args);
        }
        else
        {
            ERR("%s expects %d params but got %zu", prim->name, arity,
                LIST_N(args));
            result = NULL;
        }
    }
    else if (lake_is_type(TYPE_FN, fnVal))
    {
        LakeFn *fn = FN(fnVal);

        /* Check # of params */
        size_t nparams = LIST_N(fn->params);
        if (!fn->varargs && LIST_N(args) != nparams)
        {
            ERR("expected %zu params but got %zu", nparams, LIST_N(args));
            return NULL;
        }
        else if (fn->varargs && LIST_N(args) < nparams)
        {
            ERR("expected at least %zu params but got %zu", nparams,
                LIST_N(args));
            return NULL;
        }

        Env *env = env_make(fn->closure);

        /* bind each (param,arg) pair in env */
        size_t i;
        for (i = 0; i < nparams; ++i)
        {
            env_define(env, SYM(LIST_VAL(fn->params, i)), LIST_VAL(args, i));
        }

        /* bind varargs */
        if (fn->varargs)
        {
            LakeList *remainingArgs =
                list_make_with_capacity(LIST_N(args) - nparams);
            for (; i < LIST_N(args); ++i)
            {
                list_append(remainingArgs, LIST_VAL(args, i));
            }
            env_define(env, fn->varargs, VAL(remainingArgs));
        }

        /* evaluate body */
        result = eval_exprs1(ctx, env, fn->body);
    }
    else
    {
        ERR("not a function: %s", lake_repr(fnVal));
    }
    return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movl $0x7, %edi
callq 0x36a4
testl %eax, %eax
je 0x2c59
movslq 0x18(%rbx), %rdx
cmpq $-0x1, %rdx
je 0x2c3f
cmpq %rdx, 0x18(%r15)
jne 0x2d62
movq 0x20(%rbx), %rax
movq %r14, %rdi
movq %r15, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movl $0x8, %edi
movq %rbx, %rsi
callq 0x36a4
testl %eax, %eax
je 0x2d73
movq 0x10(%rbx), %rax
movq 0x18(%rax), %r12
leaq 0x18(%r15), %rdi
cmpq $0x0, 0x18(%rbx)
movq 0x18(%r15), %rax
je 0x2c97
cmpq %r12, %rax
jae 0x2ca0
movq %r12, %rsi
callq 0x14c0
jmp 0x2d85
cmpq %r12, %rax
jne 0x2d7d
movq 0x28(%rbx), %rdi
callq 0x2268
movq %rax, %r13
testq %r12, %r12
je 0x2ceb
xorl %ebp, %ebp
movq 0x10(%rbx), %rax
cmpq 0x18(%rax), %rbp
jae 0x2cc7
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rsi
jmp 0x2cc9
xorl %esi, %esi
cmpq 0x18(%r15), %rbp
jae 0x2cd9
movq 0x20(%r15), %rax
movq (%rax,%rbp,8), %rdx
jmp 0x2cdb
xorl %edx, %edx
movq %r13, %rdi
callq 0x22ce
incq %rbp
cmpq %rbp, %r12
jne 0x2cb3
cmpq $0x0, 0x18(%rbx)
je 0x2d2b
movq 0x18(%r15), %rdi
subq %r12, %rdi
callq 0x3926
movq %rax, %rbp
cmpq 0x18(%r15), %r12
jae 0x2d1c
movq 0x20(%r15), %rax
movq (%rax,%r12,8), %rsi
movq %rbp, %rdi
callq 0x3a63
incq %r12
jmp 0x2d01
movq 0x18(%rbx), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x22ce
movq 0x20(%rbx), %rdx
movq %r14, %rdi
movq %r13, %rsi
callq 0x2d89
movq %rax, %rbx
movq %rax, %rdi
callq 0x3bff
movq %rax, %r14
movq %rbx, %rdi
callq 0x38d8
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x18, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1461
jmp 0x2d85
movq %rbx, %rdi
callq 0x155c
jmp 0x2d85
movq %r12, %rsi
callq 0x150e
xorl %eax, %eax
jmp 0x2d53