// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
movq %rsi, %rbx
leaq 0xd87f(%rip), %rax      # 0x1232c
leaq 0xd87f(%rip), %r12      # 0x12333
testl %edx, %edx
cmovneq %rax, %r12
movl 0x4(%rsi), %ecx
cmpl $0x1, %ecx
jle 0x4b4f
movl %edx, %ebp
movq %rdi, %r13
movq 0x8(%rbx), %rax
movq (%rax), %r15
movl (%r15), %edi
cmpl $0x3, %edi
je 0x4b67
cmpl $0xb, %edi
jne 0x4b97
movl %ebp, 0xc(%rsp)
movslq 0x4(%r15), %rax
testq %rax, %rax
setg %bpl
jle 0x4c2e
movq 0x8(%r15), %rcx
movq (%rcx), %rsi
xorl %edx, %edx
cmpl $0x3, (%rsi)
jne 0x4b2d
leaq -0x1(%rax), %rsi
xorl %edx, %edx
cmpq %rdx, %rsi
je 0x4c24
movq 0x8(%rcx,%rdx,8), %rdi
incq %rdx
cmpl $0x3, (%rdi)
je 0x4b10
cmpq %rax, %rdx
setb %bpl
leaq 0xd81b(%rip), %rdi      # 0x1234f
movq %r12, %rsi
xorl %eax, %eax
callq 0x1032f
movq %rax, (%rsp)
movq %rbx, %rdi
callq 0x10767
jmp 0x4c2e
leaq 0xd571(%rip), %rdi      # 0x120c7
movq %r12, %rsi
movl $0x2, %edx
xorl %eax, %eax
callq 0x1032f
jmp 0x4bc2
cmpl $0x2, %ecx
jne 0x4bdf
movq %r13, %rdi
movq %r15, %rsi
callq 0x118f8
cmpl $-0x1, %eax
je 0x4beb
movq 0x8(%rbx), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xd7ac(%rip), %rdi      # 0x1233a
xorl %eax, %eax
callq 0x1032f
jmp 0x4bc2
callq 0x10138
movq %rax, %r14
movl $0xb, %edi
callq 0x10138
leaq 0xd591(%rip), %rdi      # 0x12141
movq %r12, %rsi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x1032f
movq %rax, %r14
movq %rbx, %rdi
callq 0x10767
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd514(%rip), %rdi      # 0x120fa
jmp 0x4b56
movq %r13, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x61cd
cmpl $0x2, (%rax)
je 0x4bd0
movq %rax, %rbx
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r13, %rdi
testb %bpl, %bpl
je 0x4cfe
callq 0x11965
jmp 0x4d03
incq %rdx
cmpq %rax, %rdx
setb %bpl
testb %bpl, %bpl
jne 0x4cad
movq %rbx, 0x10(%rsp)
cmpl $0x0, 0x4(%r15)
setg %bpl
jle 0x4ca7
xorl %r14d, %r14d
movq 0x8(%r15), %rax
movq (%rax,%r14,8), %rsi
movq %r13, %rbx
movq %r13, %rdi
callq 0x118f8
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x4c89
movq 0x8(%r15), %rax
movq (%rax,%r14,8), %rax
movq 0x18(%rax), %rsi
leaq 0xd6c6(%rip), %rdi      # 0x1233a
xorl %eax, %eax
callq 0x1032f
movq %rax, (%rsp)
movq 0x10(%rsp), %rdi
callq 0x10767
cmpl $-0x1, %r13d
jne 0x4ca4
incq %r14
movslq 0x4(%r15), %rax
cmpq %rax, %r14
setl %bpl
movq %rbx, %r13
jl 0x4c46
jmp 0x4ca7
movq %rbx, %r13
testb $0x1, %bpl
je 0x4cb6
movq (%rsp), %rax
jmp 0x4bd0
movl 0x4(%r15), %edx
movq 0x10(%rsp), %rbx
movl 0x4(%rbx), %ecx
decl %ecx
cmpl %ecx, %edx
jne 0x4cef
cmpl $0x2, 0x4(%rbx)
setge %bpl
jl 0x4d3b
movq %r13, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x61cd
movq %rax, %rbx
cmpl $0x2, (%rax)
jne 0x4d10
movq %rbx, %rax
jmp 0x4d3f
leaq 0xd68f(%rip), %rdi      # 0x12385
movq %r12, %rsi
jmp 0x4b5e
callq 0x11958
movq %rbx, %rdi
movl $0x1, %esi
jmp 0x4da3
movl $0x2, %r12d
cmpl 0x4(%rbx), %r12d
setl %bpl
jge 0x4d3b
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %edx
callq 0x61cd
movq %rax, %rbx
incl %r12d
cmpl $0x2, (%rax)
jne 0x4d16
jmp 0x4d3f
movq (%rsp), %rax
testb %bpl, %bpl
jne 0x4bd0
cmpl $0x0, 0x4(%r15)
movl 0xc(%rsp), %ebp
jle 0x4d9b
xorl %r14d, %r14d
testb %bpl, %bpl
je 0x4d76
movq 0x8(%r15), %rax
movq (%rax,%r14,8), %rsi
movq 0x8(%rbx), %rax
movq 0x8(%rax,%r14,8), %rdx
movq %r13, %rdi
callq 0x11965
jmp 0x4d8f
movq 0x8(%r15), %rax
movq (%rax,%r14,8), %rsi
movq 0x8(%rbx), %rax
movq 0x8(%rax,%r14,8), %rdx
movq %r13, %rdi
callq 0x11958
incq %r14
movslq 0x4(%r15), %rax
cmpq %rax, %r14
jl 0x4d56
movl 0x4(%rbx), %esi
decl %esi
movq %rbx, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10c7f