// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x7
popq %rdi
callq 0x2a6c
testl %eax, %eax
je 0x20a7
movslq 0x18(%r14), %r12
cmpq $-0x1, %r12
je 0x208d
cmpq %r12, 0x18(%rbx)
jne 0x21af
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
pushq $0x8
popq %rdi
movq %r14, %rsi
callq 0x2a6c
testl %eax, %eax
je 0x21e9
movq 0x10(%r14), %rax
movq 0x18(%rax), %r12
cmpq $0x0, 0x18(%r14)
movq 0x18(%rbx), %rax
je 0x2101
cmpq %r12, %rax
jae 0x210a
movq 0x7f1f(%rip), %r14      # 0x9ff8
movq (%r14), %rcx
leaq 0x3f7b(%rip), %rdi      # 0x605e
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
movq (%r14), %rdi
movq 0x18(%rbx), %rcx
leaq 0x3ffa(%rip), %rsi      # 0x60f6
jmp 0x2253
cmpq %r12, %rax
jne 0x2229
movq 0x28(%r14), %rdi
callq 0x15a0
movq %rax, %r13
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x2152
movq 0x10(%r14), %rax
cmpq 0x18(%rax), %rbp
jae 0x2131
movq 0x20(%rax), %rax
movq (%rax,%rbp,8), %rsi
jmp 0x2133
xorl %esi, %esi
cmpq 0x18(%rbx), %rbp
jae 0x2143
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rdx
jmp 0x2145
xorl %edx, %edx
movq %r13, %rdi
callq 0x1600
incq %rbp
jmp 0x2118
cmpq $0x0, 0x18(%r14)
je 0x2192
movq 0x18(%rbx), %rdi
subq %r12, %rdi
callq 0x2d21
movq %rax, %rbp
cmpq 0x18(%rbx), %r12
jae 0x2183
movq 0x20(%rbx), %rax
movq (%rax,%r12,8), %rsi
movq %rbp, %rdi
callq 0x2e14
incq %r12
jmp 0x2168
movq 0x18(%r14), %rsi
movq %r13, %rdi
movq %rbp, %rdx
callq 0x1600
movq 0x20(%r14), %rdx
movq %r15, %rdi
movq %r13, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22d0
movq 0x7e42(%rip), %r15      # 0x9ff8
movq (%r15), %rcx
leaq 0x3e9e(%rip), %rdi      # 0x605e
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
movq (%r15), %rdi
movq 0x10(%r14), %rdx
movq 0x18(%rbx), %r8
leaq 0x3ed8(%rip), %rsi      # 0x60b5
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1170
jmp 0x2224
movq 0x7e08(%rip), %r15      # 0x9ff8
movq (%r15), %rcx
leaq 0x3e64(%rip), %rdi      # 0x605e
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
movq (%r15), %rbx
movq %r14, %rdi
callq 0x2a74
leaq 0x3f08(%rip), %rsi      # 0x611f
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1170
movq (%r15), %rsi
jmp 0x2260
movq 0x7dc8(%rip), %r14      # 0x9ff8
movq (%r14), %rcx
leaq 0x3e24(%rip), %rdi      # 0x605e
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
callq 0x11f0
movq (%r14), %rdi
movq 0x18(%rbx), %rcx
leaq 0x3e83(%rip), %rsi      # 0x60d6
movq %r12, %rdx
xorl %eax, %eax
callq 0x1170
movq (%r14), %rsi
pushq $0xa
popq %rdi
callq 0x1100
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq