// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r12
movl (%rsi), %eax
cmpl $0x7, %eax
jne 0x62b8
movq 0x18(%rbx), %rax
movq %r12, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movl 0x4(%r14), %ecx
movl %ecx, 0xc(%rsp)
movq 0x20(%rbx), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, 0x8(%rsp)
cmpl $0x9, %eax
jne 0x6315
cmpl $0x0, 0x4(%r14)
jle 0x6315
xorl %r15d, %r15d
movq 0x8(%r14), %rax
movq (%rax,%r15,8), %rax
movl (%rax), %eax
orl $0x8, %eax
cmpl $0xb, %eax
jne 0x6309
callq 0x106fb
movq 0x8(%r14), %rcx
movq (%rcx,%r15,8), %rsi
movq %rax, %rdi
callq 0x10835
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r15,8)
incq %r15
movslq 0x4(%r14), %rax
cmpq %rax, %r15
jl 0x62da
leaq 0xc3b1(%rip), %r15      # 0x126cd
movq %r12, 0x10(%rsp)
cmpl $0x0, 0x4(%r14)
je 0x645a
movq 0x20(%rbx), %rdi
cmpl $0x0, 0x4(%rdi)
je 0x651e
xorl %esi, %esi
callq 0x10c23
movq %rax, %rbp
movq 0x18(%rax), %rdi
movq %r15, %rsi
callq 0x11b89
testb %al, %al
je 0x639d
movq 0x20(%rbx), %rdi
cmpl $0x1, 0x4(%rdi)
jne 0x63d5
xorl %esi, %esi
callq 0x10c23
movq %rax, %r13
movq %r12, %rdi
movq %r14, %rsi
callq 0x3e53
movq %rax, %r15
cmpl $0x2, (%rax)
jne 0x6419
movq %rbp, %rdi
callq 0x10767
movq %r13, %rdi
callq 0x10767
movl $0x1, %eax
movq %r15, (%rsp)
jmp 0x643d
movq %r14, %rdi
xorl %esi, %esi
callq 0x10c23
movq %r12, %rdi
movq %rax, %rsi
callq 0x5e90
movq %rax, %r13
cmpl $0x2, (%rax)
jne 0x63f6
movq %rbp, %rdi
callq 0x10767
movq %r14, %rdi
callq 0x10767
movl $0x1, %eax
movq %r13, (%rsp)
jmp 0x6449
movq %r14, %rdi
callq 0x10767
leaq 0xc2eb(%rip), %rdi      # 0x126cf
xorl %eax, %eax
callq 0x1032f
movq %rax, (%rsp)
movl $0x1, %eax
jmp 0x6449
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x11958
movq %rbp, %rdi
callq 0x10767
movq %r13, %rdi
callq 0x10767
xorl %eax, %eax
jmp 0x6449
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x11958
movq %rbp, %rdi
callq 0x10767
movq %r13, %rdi
callq 0x10767
movl $0x6, %eax
leaq 0xc289(%rip), %r15      # 0x126cd
movq 0x10(%rsp), %r12
testl %eax, %eax
je 0x6321
cmpl $0x6, %eax
jne 0x6554
movq 0x20(%rbx), %rax
cmpl $0x0, 0x4(%rax)
jle 0x64d1
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
leaq 0xc257(%rip), %rsi      # 0x126cd
callq 0x11b89
testb %al, %al
je 0x64d1
movq 0x20(%rbx), %rdi
cmpl $0x2, 0x4(%rdi)
jne 0x6567
xorl %esi, %esi
callq 0x10c23
movq %rax, %rdi
callq 0x10767
movq 0x20(%rbx), %rdi
xorl %esi, %esi
callq 0x10c23
movq %rax, %r15
callq 0x106fb
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x11958
movq %r15, %rdi
callq 0x10767
movq %r12, %rdi
callq 0x10767
movq 0x20(%rbx), %rax
cmpl $0x0, 0x4(%rax)
jne 0x64df
movb $0x1, 0x30(%rbx)
movq %r14, %rdi
callq 0x10767
cmpl $0x9, (%rbx)
jne 0x6508
cmpb $0x1, 0x30(%rbx)
jne 0x6508
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6583
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10a5f
movq %r14, %rdi
callq 0x10767
movl (%rbx), %edi
callq 0x10138
leaq 0xc167(%rip), %rdi      # 0x1269b
movq %rax, %rsi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1032f
movq (%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc161(%rip), %rdi      # 0x126cf
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1032f