// File: /workspace/github_c_makefile_star2_O2/voithos[P]awl/src/builtins.c
// Function: builtin_var
awlval* builtin_var(awlenv* e, awlval* a, bool global) {
    char* op = global ? "global" : "define";
    AWLASSERT_MINARGCOUNT(a, 2, op);

    /* Special case when there is a single symbol to be defined */
    if (a->cell[0]->type == AWLVAL_SYM) {
        AWLASSERT_ARGCOUNT(a, 2, op);

        int index = awlenv_index(e, a->cell[0]);
        AWLASSERT(a, index == -1,
                "cannot redefine '%s'", a->cell[0]->sym);

        EVAL_SINGLE_ARG(e, a, 1);

        if (global) {
            awlenv_put_global(e, a->cell[0], a->cell[1]);
        } else {
            awlenv_put(e, a->cell[0], a->cell[1]);
        }
        return awlval_take(a, 1);
    }

    AWLASSERT_TYPE(a, 0, AWLVAL_SEXPR, op);

    awlval* syms = a->cell[0];
    for (int i = 0; i < syms->count; i++) {
        AWLASSERT(a, (syms->cell[i]->type == AWLVAL_SYM),
                "function '%s' cannot define non-symbol at position %i", op, i);
    }

    for (int i = 0; i < syms->count; i++) {
        int index = awlenv_index(e, syms->cell[i]);
        AWLASSERT(a, index == -1,
                "cannot redefine '%s'", syms->cell[i]->sym);
    }

    AWLASSERT(a, (syms->count == a->count - 1),
            "function '%s' given non-matching number of symbols and values; %i symbols, %i values",
            op, syms->count, a->count - 1);

    /* Evaluate value arguments (but not the symbols) */
    for (int i = 1; i < a->count; i++) {
        EVAL_SINGLE_ARG(e, a, i);
    }

    for (int i = 0; i < syms->count; i++) {
        if (global) {
            awlenv_put_global(e, syms->cell[i], a->cell[i + 1]);
        } else {
            awlenv_put(e, syms->cell[i], a->cell[i + 1]);
        }
    }

    return awlval_take(a, a->count - 1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
leaq 0xc153(%rip), %rax      # 0x1032c
leaq 0xc153(%rip), %r12      # 0x10333
testl %edx, %edx
cmovneq %rax, %r12
movl 0x4(%rsi), %ecx
cmpl $0x1, %ecx
jle 0x4231
movl %edx, %ebp
movq %rdi, %r14
movq 0x8(%rbx), %rax
movq (%rax), %r15
movl (%r15), %edi
cmpl $0x3, %edi
je 0x424a
cmpl $0xb, %edi
jne 0x4270
movl 0x4(%r15), %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rdx, %rax
je 0x429b
movq 0x8(%r15), %rcx
movq (%rcx,%rdx,8), %rcx
cmpl $0x3, (%rcx)
jne 0x42e8
incq %rdx
jmp 0x4212
leaq 0xbe8f(%rip), %rdi      # 0x100c7
pushq $0x2
popq %rdx
movq %r12, %rsi
xorl %eax, %eax
callq 0xdb45
jmp 0x42f9
cmpl $0x2, %ecx
jne 0x42dc
movq %r14, %rdi
movq %r15, %rsi
callq 0xee5d
cmpl $-0x1, %eax
je 0x4344
movq 0x8(%rbx), %rax
movq (%rax), %rax
jmp 0x42c8
callq 0xd97c
movq %rax, %r14
pushq $0xb
popq %rdi
callq 0xd97c
leaq 0xbeba(%rip), %rdi      # 0x10141
movq %r12, %rsi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xdb45
jmp 0x42f9
xorl %r13d, %r13d
movslq 0x4(%r15), %rdx
cmpq %rdx, %r13
jge 0x4316
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rsi
movq %r14, %rdi
callq 0xee5d
incq %r13
cmpl $-0x1, %eax
je 0x429e
movq 0x8(%r15), %rax
movq -0x8(%rax,%r13,8), %rax
movq 0x18(%rax), %rsi
leaq 0xc067(%rip), %rdi      # 0x1033a
xorl %eax, %eax
callq 0xdb45
jmp 0x42f9
leaq 0xbe17(%rip), %rdi      # 0x100fa
jmp 0x4238
leaq 0xc060(%rip), %rdi      # 0x1034f
movq %r12, %rsi
xorl %eax, %eax
callq 0xdb45
movq %rax, %r14
movq %rbx, %rdi
callq 0xded1
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rbx), %ecx
decl %ecx
cmpl %ecx, %edx
jne 0x435f
pushq $0x1
popq %r12
cmpl 0x4(%rbx), %r12d
jge 0x4388
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x55b2
movq %rax, %rbx
incl %r12d
cmpl $0x2, (%rax)
jne 0x4323
jmp 0x435a
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x55b2
movq %rax, %rbx
cmpl $0x2, (%rax)
jne 0x436e
movq %rbx, %rax
jmp 0x4307
leaq 0xc01f(%rip), %rdi      # 0x10385
movq %r12, %rsi
jmp 0x423e
movq 0x8(%rbx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
testb %bpl, %bpl
je 0x43c5
callq 0xeec5
jmp 0x43ca
xorl %r12d, %r12d
movslq 0x4(%r15), %rax
cmpq %rax, %r12
jge 0x43be
movq 0x8(%r15), %rax
movq (%rax,%r12,8), %rsi
movq 0x8(%rbx), %rax
movq 0x8(%rax,%r12,8), %rdx
movq %r14, %rdi
testb %bpl, %bpl
je 0x43b4
callq 0xeec5
jmp 0x43b9
callq 0xeeb8
incq %r12
jmp 0x438b
movl 0x4(%rbx), %esi
decl %esi
jmp 0x43cd
callq 0xeeb8
pushq $0x1
popq %rsi
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe31b