// File: /workspace/github_c_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl 0x4(%rsi), %ecx
cmpl $0x2, %ecx
jne 0x4d85
movq %rdi, %r15
movq 0x8(%rbx), %rax
movq (%rax), %r14
movl (%r14), %edi
cmpl $0xb, %edi
jne 0x4da1
movslq 0x4(%r14), %rax
testq %rax, %rax
jle 0x4cf4
movq 0x8(%r14), %rcx
xorl %r12d, %r12d
movq (%rcx,%r12,8), %rdx
cmpl $0xb, (%rdx)
jne 0x4de5
cmpl $0x2, 0x4(%rdx)
jne 0x4dd2
movq 0x8(%rdx), %rdx
movq (%rdx), %rdx
cmpl $0x3, (%rdx)
jne 0x4dd2
incq %r12
cmpq %r12, %rax
jne 0x4c92
testl %eax, %eax
jle 0x4cf4
xorl %r12d, %r12d
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0x113ff
cmpl $-0x1, %eax
jne 0x4e4c
incq %r12
movslq 0x4(%r14), %rax
cmpq %rax, %r12
jl 0x4cc8
callq 0x1016f
movq %rax, %r12
movq %r15, (%rax)
incl 0x14(%r15)
cmpl $0x0, 0x4(%r14)
jle 0x4d5a
xorl %r15d, %r15d
movq 0x8(%r14), %rax
movq (%rax,%r15,8), %rsi
movq %r12, %rdi
movl $0x1, %edx
callq 0x5f11
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r15,8)
movq 0x8(%r14), %rax
movq (%rax,%r15,8), %rax
cmpl $0x2, (%rax)
je 0x4e24
movq 0x8(%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r12, %rdi
callq 0x1145a
incq %r15
movslq 0x4(%r14), %rax
cmpq %rax, %r15
jl 0x4d0d
movq %rbx, %rdi
movl $0x1, %esi
callq 0x107b7
movq %r12, %rdi
movq %rax, %rsi
callq 0x5be8
movq %rax, %rbx
movq %r12, %rdi
callq 0x1032e
movq %rbx, %rax
jmp 0x4e18
leaq 0xd36e(%rip), %rdi      # 0x120fa
leaq 0xd647(%rip), %rsi      # 0x123da
movl $0x2, %edx
xorl %eax, %eax
callq 0xfe6b
jmp 0x4e0a
callq 0xfc74
movq %rax, %r14
movl $0xb, %edi
callq 0xfc74
leaq 0xd387(%rip), %rdi      # 0x12141
leaq 0xd619(%rip), %rsi      # 0x123da
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xfe6b
jmp 0x4e0a
movl $0x3, %edi
callq 0xfc74
leaq 0xd634(%rip), %rdi      # 0x12417
jmp 0x4df6
movl $0xb, %edi
callq 0xfc74
leaq 0xd5e8(%rip), %rdi      # 0x123de
leaq 0xd5dd(%rip), %rsi      # 0x123da
movq %rax, %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xfe6b
movq %rax, %r14
movq %rbx, %rdi
callq 0x1029f
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %r14, %rdi
movl %r15d, %esi
callq 0x1075b
movq %rax, %r14
movq %rbx, %rdi
callq 0x1029f
movq $0x0, (%r12)
movq %r12, %rdi
callq 0x1032e
jmp 0x4e15
movq 0x8(%r14), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x18(%rax), %rsi
leaq 0xd4d4(%rip), %rdi      # 0x1233a
xorl %eax, %eax
callq 0xfe6b
jmp 0x4e0a