// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rsi, %r14
movq %rdi, %r15
movl $0x7, %edi
callq 0x36e8
testl %eax, %eax
je 0x2c74
movslq 0x18(%r14), %rdx
cmpq $-0x1, %rdx
je 0x2c5a
cmpq %rdx, 0x18(%rbx)
jne 0x2d78
movq 0x20(%r14), %rax
movq %r15, %rdi
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movl $0x8, %edi
movq %r14, %rsi
callq 0x36e8
testl %eax, %eax
je 0x2d89
movq 0x10(%r14), %rcx
movq 0x18(%r14), %rax
movq 0x18(%rcx), %r13
testq %rax, %rax
jne 0x2ca9
cmpq %r13, 0x18(%rbx)
jne 0x2da4
testq %rax, %rax
je 0x2cb3
cmpq %r13, 0x18(%rbx)
jb 0x2d93
movq 0x28(%r14), %rdi
callq 0x2298
movq %rax, %r12
xorl %ebp, %ebp
testq %r13, %r13
je 0x2d01
movq 0x10(%r14), %rax
cmpq 0x18(%rax), %rbp
jae 0x2cda
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rsi
jmp 0x2cdc
xorl %esi, %esi
cmpq 0x18(%rbx), %rbp
jae 0x2cec
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rdx
jmp 0x2cee
xorl %edx, %edx
movq %r12, %rdi
callq 0x22fe
incq %rbp
cmpq %rbp, %r13
jne 0x2cc6
movq %r13, %rbp
cmpq $0x0, 0x18(%r14)
je 0x2d41
movq 0x18(%rbx), %rdi
subq %r13, %rdi
callq 0x396e
movq %rax, %r13
cmpq 0x18(%rbx), %rbp
jae 0x2d32
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rsi
movq %r13, %rdi
callq 0x3ac7
incq %rbp
jmp 0x2d17
movq 0x18(%r14), %rsi
movq %r12, %rdi
movq %r13, %rdx
callq 0x22fe
movq 0x20(%r14), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2db7
movq %rax, %rbx
movq %rax, %rdi
callq 0x3c73
movq %rax, %r14
movq %rbx, %rdi
callq 0x3920
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x18, %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1461
jmp 0x2db3
movq %r14, %rdi
callq 0x155c
jmp 0x2db3
addq $0x18, %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x150e
jmp 0x2db3
addq $0x18, %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x14c0
xorl %eax, %eax
jmp 0x2d69