// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
movq %rsi, %rbx
movl 0x4(%rsi), %ecx
cmpl $0x2, %ecx
jne 0x4455
movq %rdi, %r15
movq 0x8(%rbx), %rax
movq (%rax), %r14
movl (%r14), %edi
cmpl $0xb, %edi
jne 0x4472
movl 0x4(%r14), %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovlel %r12d, %eax
cmpq %r12, %rax
je 0x44b5
movq 0x8(%r14), %rcx
movq (%rcx,%r12,8), %rcx
cmpl $0xb, (%rcx)
jne 0x4504
cmpl $0x2, 0x4(%rcx)
jne 0x44a4
movq 0x8(%rcx), %rcx
movq (%rcx), %rcx
cmpl $0x3, (%rcx)
jne 0x44a4
incq %r12
jmp 0x4424
leaq 0xbc9e(%rip), %rdi      # 0x100fa
leaq 0xbf77(%rip), %rsi      # 0x103da
pushq $0x2
popq %rdx
xorl %eax, %eax
callq 0xdb45
jmp 0x4527
callq 0xd97c
movq %rax, %r14
pushq $0xb
popq %rdi
callq 0xd97c
leaq 0xbcb8(%rip), %rdi      # 0x10141
leaq 0xbf4a(%rip), %rsi      # 0x103da
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xdb45
jmp 0x4527
pushq $0x3
popq %rdi
callq 0xd97c
leaq 0xbf64(%rip), %rdi      # 0x10417
jmp 0x4513
xorl %r12d, %r12d
movslq 0x4(%r14), %rax
cmpq %rax, %r12
jge 0x453e
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xee5d
incq %r12
cmpl $-0x1, %eax
je 0x44b8
movq 0x8(%r14), %rax
movq -0x8(%rax,%r12,8), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xbe3f(%rip), %rdi      # 0x1033a
xorl %eax, %eax
callq 0xdb45
jmp 0x4527
pushq $0xb
popq %rdi
callq 0xd97c
leaq 0xbecb(%rip), %rdi      # 0x103de
leaq 0xbec0(%rip), %rsi      # 0x103da
movq %rax, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xdb45
movq %rax, %r14
movq %rbx, %rdi
callq 0xded1
movq %r14, %rax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0xddef
movq %rax, %r12
movq %r15, (%rax)
incl 0x14(%r15)
xorl %r15d, %r15d
pushq $0x1
popq %rbp
movslq 0x4(%r14), %rax
cmpq %rax, %r15
jge 0x459b
movq 0x8(%r14), %rax
movq (%rax,%r15,8), %rsi
movq %r12, %rdi
movl %ebp, %edx
callq 0x55b2
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x8(%r14), %rax
movq (%rax,%r15,8), %rax
cmpl $0x2, (%rax)
je 0x45c4
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r12, %rdi
callq 0xeeb8
incq %r15
jmp 0x4553
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xe31b
movq %r12, %rdi
movq %rax, %rsi
callq 0x52a8
movq %rax, %rbx
movq %r12, %rdi
callq 0xdf56
movq %rbx, %rax
jmp 0x4535
movq %r14, %rdi
movl %r15d, %esi
callq 0xe2bf
movq %rax, %r14
movq %rbx, %rdi
callq 0xded1
andq $0x0, (%r12)
movq %r12, %rdi
callq 0xdf56
jmp 0x4532