// File: /workspace/github_c_makefile_star2_O3/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 0xd920(%rip), %rax      # 0x1232c
leaq 0xd920(%rip), %r12      # 0x12333
testl %edx, %edx
cmovneq %rax, %r12
movl 0x4(%rsi), %ecx
cmpl $0x1, %ecx
jle 0x4b1e
movl %edx, %ebp
movq %rdi, %r14
movq 0x8(%rbx), %rax
movq (%rax), %r15
movl (%r15), %edi
cmpl $0x3, %edi
je 0x4b39
cmpl $0xb, %edi
jne 0x4b5a
movl 0x4(%r15), %edx
testl %edx, %edx
jle 0x4a98
movq 0x8(%r15), %rsi
xorl %eax, %eax
movq (%rsi,%rax,8), %rdi
cmpl $0x3, (%rdi)
jne 0x4b90
incq %rax
cmpq %rax, %rdx
jne 0x4a54
testl %edx, %edx
jle 0x4a98
xorl %r13d, %r13d
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rsi
movq %r14, %rdi
callq 0x113ff
cmpl $-0x1, %eax
jne 0x4bd3
incq %r13
movslq 0x4(%r15), %rdx
cmpq %rdx, %r13
jl 0x4a70
movl 0x4(%rbx), %ecx
leal -0x1(%rcx), %eax
cmpl %eax, %edx
jne 0x4ba5
cmpl $0x2, %ecx
jl 0x4ad7
movl $0x1, %r12d
movq %r14, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x5f11
movq %rax, %rbx
cmpl $0x2, (%rax)
je 0x4bce
incl %r12d
movl 0x4(%rbx), %ecx
cmpl %ecx, %r12d
jl 0x4aae
movl 0x4(%r15), %edx
testl %edx, %edx
jle 0x4b12
xorl %r12d, %r12d
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 0x4afe
callq 0x11467
jmp 0x4b03
callq 0x1145a
incq %r12
movslq 0x4(%r15), %rax
cmpq %rax, %r12
jl 0x4ade
movl 0x4(%rbx), %ecx
decl %ecx
movq %rbx, %rdi
movl %ecx, %esi
jmp 0x4c31
leaq 0xd5a2(%rip), %rdi      # 0x120c7
movq %r12, %rsi
movl $0x2, %edx
xorl %eax, %eax
callq 0xfe6b
jmp 0x4bed
cmpl $0x2, %ecx
jne 0x4b87
movq %r14, %rdi
movq %r15, %rsi
callq 0x113ff
cmpl $-0x1, %eax
je 0x4bb6
movq 0x8(%rbx), %rax
movq (%rax), %rax
jmp 0x4bdb
callq 0xfc74
movq %rax, %r14
movl $0xb, %edi
callq 0xfc74
leaq 0xd5ce(%rip), %rdi      # 0x12141
movq %r12, %rsi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xfe6b
jmp 0x4bed
leaq 0xd56c(%rip), %rdi      # 0x120fa
jmp 0x4b25
leaq 0xd7b8(%rip), %rdi      # 0x1234f
movq %r12, %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0xfe6b
jmp 0x4bed
leaq 0xd7d9(%rip), %rdi      # 0x12385
movq %r12, %rsi
movl %eax, %ecx
jmp 0x4b2d
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x5f11
movq %rax, %rbx
cmpl $0x2, (%rax)
jne 0x4c0a
movq %rbx, %rax
jmp 0x4bfb
movq 0x8(%r15), %rax
movq (%rax,%r13,8), %rax
movq 0x18(%rax), %rsi
leaq 0xd754(%rip), %rdi      # 0x1233a
xorl %eax, %eax
callq 0xfe6b
movq %rax, %r14
movq %rbx, %rdi
callq 0x1029f
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
testb %bpl, %bpl
je 0x4c24
callq 0x11467
jmp 0x4c29
callq 0x1145a
movq %rbx, %rdi
movl $0x1, %esi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x107b7