// File: /workspace/github_c_makefile_star2_O2/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), %ecx
cmpl $0x7, %ecx
jne 0x5696
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), %rax
movl 0x4(%rax), %eax
movl %eax, 0x4(%rsp)
movl %edx, (%rsp)
movl %edx, %eax
cmpl $0x9, %ecx
jne 0x56f5
xorl %r12d, %r12d
movl (%rsp), %eax
movslq %eax, %rcx
cmpq %rcx, %r12
jge 0x56f5
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rax
movl (%rax), %eax
orl $0x8, %eax
cmpl $0xb, %eax
jne 0x56ec
callq 0xde89
movq 0x8(%r14), %rcx
movq (%rcx,%r12,8), %rsi
movq %rax, %rdi
callq 0xdf95
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r12,8)
incq %r12
movl 0x4(%r14), %eax
jmp 0x56b5
leaq 0xafed(%rip), %r12      # 0x106e9
testl %eax, %eax
je 0x5825
movq 0x20(%rbx), %rdi
cmpl $0x0, 0x4(%rdi)
je 0x576e
xorl %esi, %esi
callq 0xe2bf
movq %rax, %r13
movq 0x18(%rax), %rdi
movq %r12, %rsi
callq 0xf0d6
testb %al, %al
jne 0x57a3
movq %r14, %rdi
xorl %esi, %esi
callq 0xe2bf
movq %r15, %rdi
movq %rax, %rsi
callq 0x52a8
movq %rax, %rbp
cmpl $0x2, (%rax)
je 0x57d7
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0xeeb8
movq %r13, %rdi
callq 0xded1
movq %rbp, %rdi
callq 0xded1
movl 0x4(%r14), %eax
jmp 0x56fc
movq %r14, %rdi
callq 0xded1
movl (%rbx), %edi
callq 0xd97c
leaq 0xaf33(%rip), %rdi      # 0x106b7
movq %rax, %rsi
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdb45
movq 0x20(%rbx), %rdi
cmpl $0x1, 0x4(%rdi)
jne 0x57f9
xorl %esi, %esi
callq 0xe2bf
movq %rax, %r12
movq %r15, %rdi
movq %r14, %rsi
callq 0x3617
movq %rax, %rbp
cmpl $0x2, (%rax)
jne 0x5806
movq %r13, %rdi
callq 0xded1
movq %r12, %rdi
jmp 0x57e2
movq %r13, %rdi
callq 0xded1
movq %r14, %rdi
callq 0xded1
movq %rbp, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0xded1
jmp 0x58e9
movq 0x18(%rbx), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xeeb8
movq %r13, %rdi
callq 0xded1
movq %r12, %rdi
callq 0xded1
movq 0x20(%rbx), %rdi
cmpl $0x0, 0x4(%rdi)
jle 0x58a0
movq 0x8(%rdi), %rax
movq (%rax), %rax
movq 0x18(%rax), %rdi
leaq 0xaea8(%rip), %rsi      # 0x106e9
callq 0xf0d6
movq 0x20(%rbx), %rdi
testb %al, %al
je 0x58a0
cmpl $0x2, 0x4(%rdi)
jne 0x58e9
xorl %esi, %esi
callq 0xe2bf
movq %rax, %rdi
callq 0xded1
movq 0x20(%rbx), %rdi
xorl %esi, %esi
callq 0xe2bf
movq %rax, %r15
callq 0xde89
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xeeb8
movq %r15, %rdi
callq 0xded1
movq %r12, %rdi
callq 0xded1
movq 0x20(%rbx), %rdi
cmpl $0x0, 0x4(%rdi)
jne 0x58aa
movb $0x1, 0x30(%rbx)
movq %r14, %rdi
callq 0xded1
cmpl $0x9, (%rbx)
jne 0x58d3
cmpb $0x1, 0x30(%rbx)
jne 0x58d3
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5905
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe16d
leaq 0xadfb(%rip), %rdi      # 0x106eb
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xdb45