// File: /workspace/github_c_makefile_star2_O1/voithos[P]awl/src/builtins.c
// Function: builtin_let
awlval* builtin_let(awlenv* e, awlval* a) {
    /* TODO: maybe fix DEFINE and GLOBAL to work like this too... */
    AWLASSERT_ARGCOUNT(a, 2, "let");
    AWLASSERT_TYPE(a, 0, AWLVAL_SEXPR, "let");

    awlval* bindings = a->cell[0];
    /* verify structure of inner bindings list */
    for (int i = 0; i < bindings->count; i++) {
        AWLASSERT(a, (bindings->cell[i]->type == AWLVAL_SEXPR),
                "function '%s' requires %s in binding list at position %i",
                "let", awlval_type_name(AWLVAL_SEXPR), i);

        AWLASSERT(a, (bindings->cell[i]->count == 2 && bindings->cell[i]->cell[0]->type == AWLVAL_SYM),
                "function '%s' bindings must have two parts: %s and value, at position %i",
                "let", awlval_type_name(AWLVAL_SYM), i);
    }

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

    // TODO: Because of reference cycles, this causes garbage to build up
    // Need to add GC
    awlenv* lenv = awlenv_new();
    lenv->parent = e;
    lenv->parent->references++;

    /* Evaluate value arguments (but not the symbols) */
    for (int i = 0; i < bindings->count; i++) {
        bindings->cell[i] = awlval_eval_arg(lenv, bindings->cell[i], 1);

        if (bindings->cell[i]->type == AWLVAL_ERR) {
            awlval* err = awlval_pop(bindings, i);
            awlval_del(a);
            lenv->parent = NULL;
            awlenv_del(lenv);
            return err;
        }

        awlval* sym = bindings->cell[i]->cell[0];
        awlval* val = bindings->cell[i]->cell[1];

        awlenv_put(lenv, sym, val);
    }

    awlval* v = awlval_eval(lenv, awlval_take(a, 1));

    awlenv_del(lenv);
    return v;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl 0x4(%rsi), %ecx
cmpl $0x2, %ecx
jne 0x4e3b
movq %rdi, %r12
movq 0x8(%rbx), %rax
movq (%rax), %r14
movl (%r14), %edi
cmpl $0xb, %edi
jne 0x4e57
movslq 0x4(%r14), %rax
testq %rax, %rax
setg %bpl
jle 0x4ea3
movq 0x8(%r14), %rcx
xorl %r13d, %r13d
movq (%rcx,%r13,8), %rdx
cmpl $0xb, (%rdx)
jne 0x4eb8
cmpl $0x2, 0x4(%rdx)
jne 0x4ea5
movq 0x8(%rdx), %rdx
movq (%rdx), %rdx
cmpl $0x3, (%rdx)
jne 0x4ea5
incq %r13
cmpq %rax, %r13
setb %bpl
jne 0x4e07
jmp 0x4ee9
leaq 0xd2b8(%rip), %rdi      # 0x120fa
leaq 0xd591(%rip), %rsi      # 0x123da
movl $0x2, %edx
xorl %eax, %eax
callq 0x1032f
jmp 0x4e86
callq 0x10138
movq %rax, %r14
movl $0xb, %edi
callq 0x10138
leaq 0xd2d1(%rip), %rdi      # 0x12141
leaq 0xd563(%rip), %rsi      # 0x123da
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 $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4ee9
movl $0x3, %edi
callq 0x10138
leaq 0xd561(%rip), %rdi      # 0x12417
jmp 0x4ec9
movl $0xb, %edi
callq 0x10138
leaq 0xd515(%rip), %rdi      # 0x123de
leaq 0xd50a(%rip), %rsi      # 0x123da
movq %rax, %rdx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x1032f
movq %rax, (%rsp)
movq %rbx, %rdi
callq 0x10767
testb $0x1, %bpl
jne 0x4f63
cmpl $0x0, 0x4(%r14)
setg %r15b
jle 0x4f5d
xorl %r13d, %r13d
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0x118f8
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x4f48
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xd405(%rip), %rdi      # 0x1233a
xorl %eax, %eax
callq 0x1032f
movq %rax, (%rsp)
movq %rbx, %rdi
callq 0x10767
cmpl $-0x1, %ebp
jne 0x4f5d
incq %r13
movslq 0x4(%r14), %rax
cmpq %rax, %r13
setl %r15b
jl 0x4efd
testb $0x1, %r15b
je 0x4f6c
movq (%rsp), %rax
jmp 0x4e94
callq 0x10637
movq %rax, %r13
movq %r12, (%rax)
incl 0x14(%r12)
cmpl $0x0, 0x4(%r14)
setg %bpl
jle 0x5032
movq 0x8(%r14), %rax
movq (%rax), %rsi
movq %r13, %rdi
movl $0x1, %edx
callq 0x61cd
movq 0x8(%r14), %rcx
movq %rax, (%rcx)
movq 0x8(%r14), %rax
movq (%rax), %rax
xorl %r12d, %r12d
cmpl $0x2, (%rax)
je 0x500b
movl $0x1, %r12d
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r13, %rdi
callq 0x11958
movslq 0x4(%r14), %rax
cmpq %rax, %r12
setl %bpl
jge 0x5032
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rsi
movq %r13, %rdi
movl $0x1, %edx
callq 0x61cd
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rax
incq %r12
cmpl $0x2, (%rax)
jne 0x4fbb
decl %r12d
movq %r14, %rdi
movl %r12d, %esi
callq 0x10c23
movq %rax, (%rsp)
movq %rbx, %rdi
callq 0x10767
movq $0x0, (%r13)
movq %r13, %rdi
callq 0x107f6
testb %bpl, %bpl
movq (%rsp), %rax
jne 0x4e94
movq %rbx, %rdi
movl $0x1, %esi
callq 0x10c7f
movq %r13, %rdi
movq %rax, %rsi
callq 0x5e90
movq %rax, %rbx
movq %r13, %rdi
callq 0x107f6
movq %rbx, %rax
jmp 0x4e94