// File: /workspace/github_c_makefile_star2_O3/voithos[P]awl/src/eval.c
// Function: awlval_call
awlval* awlval_call(awlenv* e, awlval* f, awlval* a) {
    /* calls a function if builtin, or evals a macro, else fills in the
     * corresponding parameters, and lets awlval_eval perform tail
     * call optimization
     */
    if (f->type == AWLVAL_BUILTIN) {
        return f->builtin(e, a);
    }

    int given = a->count;
    int total = f->formals->count;

    /* special case for macros */
    if (f->type == AWLVAL_MACRO) {
        for (int i = 0; i < a->count; i++) {
            /* wrap SExprs and Symbols in QExprs to avoid evaluation */
            if (a->cell[i]->type == AWLVAL_SEXPR) {
                awlval* q = awlval_qexpr();
                q = awlval_add(q, a->cell[i]);
                a->cell[i] = q;
            } else if (a->cell[i]->type == AWLVAL_SYM) {
                awlval* q = awlval_qexpr();
                q = awlval_add(q, a->cell[i]);
                a->cell[i] = q;
            }
        }
    }

    while (a->count) {
        if (f->formals->count == 0) {
            awlval_del(a);
            return awlval_err("%s passed too many arguments; got %i, expected %i",
                    awlval_type_name(f->type), given, total);
        }
        awlval* sym = awlval_pop(f->formals, 0);

        /* special case for variadic functions */
        if (streq(sym->sym, "&")) {
            if (f->formals->count != 1) {
                awlval_del(a);
                return awlval_err("function format invalid; symbol '&' not followed by single symbol");
            }

            awlval* nsym = awlval_pop(f->formals, 0);
            awlval* varargs = builtin_list(e, a);

            if (varargs->type == AWLVAL_ERR) {
                awlval_del(sym);
                awlval_del(nsym);
                return varargs;
            }

            awlenv_put(f->env, nsym, varargs);
            awlval_del(sym);
            awlval_del(nsym);
            break;
        }

        awlval* val = awlval_eval(e, awlval_pop(a, 0));
        if (val->type == AWLVAL_ERR) {
            awlval_del(sym);
            awlval_del(a);
            return val;
        }

        awlenv_put(f->env, sym, val);
        awlval_del(sym);
        awlval_del(val);
    }

    /* Special case for pure variadic function with no arguments */
    if (f->formals->count > 0 &&
            streq(f->formals->cell[0]->sym, "&")) {
        if (f->formals->count != 2) {
            return awlval_err("function format invalid; symbol '&' not followed by single symbol");
        }
        awlval_del(awlval_pop(f->formals, 0));
        awlval* sym = awlval_pop(f->formals, 0);
        awlval* val = awlval_qexpr();

        awlenv_put(f->env, sym, val);
        awlval_del(sym);
        awlval_del(val);
    }

    if (f->formals->count == 0) {
        f->called = true;
    }

    awlval_del(a);

    /* Handle macros -- they are called directly because their output must
     * be evaluated in the enclosing environment */
    if (f->type == AWLVAL_MACRO && f->called) {
        return awlval_eval_macro(f);
    } else {
        return awlval_copy(f);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
movl (%rsi), %eax
cmpl $0x7, %eax
jne 0x5ff6
movq 0x18(%rbx), %rax
movq %r15, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movl 0x4(%r14), %edx
movq 0x20(%rbx), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, (%rsp)
cmpl $0x9, %eax
sete %al
testl %edx, %edx
setg %cl
andb %al, %cl
movl %edx, 0x4(%rsp)
movl %edx, %eax
cmpb $0x1, %cl
jne 0x605a
xorl %r12d, %r12d
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rax
movl (%rax), %eax
orl $0x8, %eax
cmpl $0xb, %eax
jne 0x604e
callq 0x10233
movq 0x8(%r14), %rcx
movq (%rcx,%r12,8), %rsi
movq %rax, %rdi
callq 0x1036d
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r12,8)
incq %r12
movslq 0x4(%r14), %rax
cmpq %rax, %r12
jl 0x601f
testl %eax, %eax
je 0x6194
leaq 0xc650(%rip), %r12      # 0x126b9
movq 0x20(%rbx), %rdi
cmpl $0x0, 0x4(%rdi)
je 0x60dd
xorl %esi, %esi
callq 0x1075b
movq %rax, %r13
movq 0x18(%rax), %rdi
movq %r12, %rsi
callq 0x11692
testb %al, %al
jne 0x6112
movq %r14, %rdi
xorl %esi, %esi
callq 0x1075b
movq %r15, %rdi
movq %rax, %rsi
callq 0x5be8
movq %rax, %rbp
cmpl $0x2, (%rax)
je 0x6146
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1145a
movq %r13, %rdi
callq 0x1029f
movq %rbp, %rdi
callq 0x1029f
cmpl $0x0, 0x4(%r14)
jne 0x6069
jmp 0x6194
movq %r14, %rdi
callq 0x1029f
movl (%rbx), %edi
callq 0xfc74
leaq 0xc594(%rip), %rdi      # 0x12687
movq %rax, %rsi
movl 0x4(%rsp), %edx
movl (%rsp), %ecx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfe6b
movq 0x20(%rbx), %rdi
cmpl $0x1, 0x4(%rdi)
jne 0x6168
xorl %esi, %esi
callq 0x1075b
movq %rax, %r12
movq %r15, %rdi
movq %r14, %rsi
callq 0x3e03
movq %rax, %rbp
cmpl $0x2, (%rax)
jne 0x6175
movq %r13, %rdi
callq 0x1029f
movq %r12, %rdi
jmp 0x6151
movq %r13, %rdi
callq 0x1029f
movq %r14, %rdi
callq 0x1029f
movq %rbp, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1029f
jmp 0x6258
movq 0x18(%rbx), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x1145a
movq %r13, %rdi
callq 0x1029f
movq %r12, %rdi
callq 0x1029f
movq 0x20(%rbx), %rdi
cmpl $0x0, 0x4(%rdi)
jle 0x620f
movq 0x8(%rdi), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
leaq 0xc509(%rip), %rsi      # 0x126b9
callq 0x11692
movq 0x20(%rbx), %rdi
testb %al, %al
je 0x620f
cmpl $0x2, 0x4(%rdi)
jne 0x6258
xorl %esi, %esi
callq 0x1075b
movq %rax, %rdi
callq 0x1029f
movq 0x20(%rbx), %rdi
xorl %esi, %esi
callq 0x1075b
movq %rax, %r15
callq 0x10233
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x1145a
movq %r15, %rdi
callq 0x1029f
movq %r12, %rdi
callq 0x1029f
movq 0x20(%rbx), %rdi
cmpl $0x0, 0x4(%rdi)
jne 0x6219
movb $0x1, 0x30(%rbx)
movq %r14, %rdi
callq 0x1029f
cmpl $0x9, (%rbx)
jne 0x6242
cmpb $0x1, 0x30(%rbx)
jne 0x6242
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6274
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10597
leaq 0xc45c(%rip), %rdi      # 0x126bb
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xfe6b