// File: /workspace/github_c_makefile_star2_O1/janet-lang[P]janet/src/core/specials.c
// Function: destructure
static int destructure(JanetCompiler *c,
                       Janet left,
                       JanetSlot right,
                       int (*leaf)(JanetCompiler *c,
                                   const uint8_t *sym,
                                   JanetSlot s,
                                   JanetTable *attr),
                       JanetTable *attr) {
    switch (janet_type(left)) {
        default:
            janetc_error(c, janet_formatc("unexpected type in destructuring, got %v", left));
            return 1;
        case JANET_SYMBOL:
            /* Leaf, assign right to left */
            return leaf(c, janet_unwrap_symbol(left), right, attr);
        case JANET_TUPLE:
        case JANET_ARRAY: {
            int32_t len = 0;
            const Janet *values = NULL;
            janet_indexed_view(left, &values, &len);
            for (int32_t i = 0; i < len; i++) {
                JanetSlot nextright = janetc_farslot(c);
                Janet subval = values[i];

                if (janet_checktype(subval, JANET_SYMBOL) && !janet_cstrcmp(janet_unwrap_symbol(subval), "&")) {
                    if (i + 1 >= len) {
                        janetc_cerror(c, "expected symbol following '& in destructuring pattern");
                        return 1;
                    }

                    if (i + 2 < len) {
                        int32_t num_extra = len - i - 1;
                        Janet *extra = janet_tuple_begin(num_extra);
                        janet_tuple_flag(extra) |= JANET_TUPLE_FLAG_BRACKETCTOR;

                        for (int32_t j = 0; j < num_extra; ++j) {
                            extra[j] = values[j + i + 1];
                        }

                        janetc_error(c, janet_formatc("expected a single symbol follow '& in destructuring pattern, found %q", janet_wrap_tuple(janet_tuple_end(extra))));
                        return 1;
                    }

                    if (!janet_checktype(values[i + 1], JANET_SYMBOL)) {
                        janetc_error(c, janet_formatc("expected symbol following '& in destructuring pattern, found %q", values[i + 1]));
                        return 1;
                    }

                    JanetSlot argi = janetc_farslot(c);
                    JanetSlot arg  = janetc_farslot(c);
                    JanetSlot len  = janetc_farslot(c);

                    janetc_emit_si(c, JOP_LOAD_INTEGER, argi, i, 0);
                    janetc_emit_ss(c, JOP_LENGTH, len, right, 0);

                    /* loop condition - reuse arg slot for the condition result */
                    int32_t label_loop_start = janetc_emit_sss(c, JOP_LESS_THAN, arg, argi, len, 0);
                    int32_t label_loop_cond_jump = janetc_emit_si(c, JOP_JUMP_IF_NOT, arg, 0, 0);

                    /* loop body */
                    janetc_emit_sss(c, JOP_GET, arg, right, argi, 0);
                    janetc_emit_s(c, JOP_PUSH, arg, 0);
                    janetc_emit_ssi(c, JOP_ADD_IMMEDIATE, argi, argi, 1, 0);

                    /* loop - jump back to the start of the loop */
                    int32_t label_loop_loop = janet_v_count(c->buffer);
                    janetc_emit(c, JOP_JUMP);
                    int32_t label_loop_exit = janet_v_count(c->buffer);

                    /* avoid shifting negative numbers */
                    c->buffer[label_loop_cond_jump] |= (uint32_t)(label_loop_exit - label_loop_cond_jump) << 16;
                    c->buffer[label_loop_loop] |= (uint32_t)(label_loop_start - label_loop_loop) << 8;

                    janetc_freeslot(c, argi);
                    janetc_freeslot(c, arg);
                    janetc_freeslot(c, len);

                    janetc_emit_s(c, JOP_MAKE_TUPLE, nextright, 1);

                    leaf(c, janet_unwrap_symbol(values[i + 1]), nextright, attr);
                    janetc_freeslot(c, nextright);
                    break;
                }

                if (i < 0x100) {
                    janetc_emit_ssu(c, JOP_GET_INDEX, nextright, right, (uint8_t) i, 1);
                } else {
                    JanetSlot k = janetc_cslot(janet_wrap_integer(i));
                    janetc_emit_sss(c, JOP_IN, nextright, right, k, 1);
                }
                if (destructure(c, subval, nextright, leaf, attr))
                    janetc_freeslot(c, nextright);
            }
        }
        return 1;
        case JANET_TABLE:
        case JANET_STRUCT: {
            const JanetKV *kvs = NULL;
            int32_t cap = 0, len = 0;
            janet_dictionary_view(left, &kvs, &len, &cap);
            for (int32_t i = 0; i < cap; i++) {
                if (janet_checktype(kvs[i].key, JANET_NIL)) continue;
                JanetSlot nextright = janetc_farslot(c);
                JanetSlot k = janetc_value(janetc_fopts_default(c), kvs[i].key);
                janetc_emit_sss(c, JOP_IN, nextright, right, k, 1);
                if (destructure(c, kvs[i].value, nextright, leaf, attr))
                    janetc_freeslot(c, nextright);
            }
        }
        return 1;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rcx, %r13
movq %rdx, -0x58(%rbp)
movq %rdi, %rbx
movq %rsi, %xmm0
movq %rsi, %rcx
shrq $0x2f, %rcx
andl $0xf, %ecx
xorl %eax, %eax
ucomisd %xmm0, %xmm0
cmovpl %ecx, %eax
leal -0x7(%rax), %ecx
cmpl $0x2, %ecx
jb 0x43c40
leal -0x9(%rax), %ecx
cmpl $0x2, %ecx
jae 0x43dd4
movq %r13, -0x60(%rbp)
leaq -0x98(%rbp), %rax
movq $0x0, (%rax)
xorl %r14d, %r14d
leaq -0xb0(%rbp), %r15
movl %r14d, (%r15)
leaq -0x48(%rbp), %rdx
movl %r14d, (%rdx)
movq %rsi, %rdi
movq %rax, %rsi
movq %r15, %rcx
callq 0x4a377
cmpl %r14d, (%r15)
jle 0x43e1a
movabsq $-0x800000000000, %r14  # imm = 0xFFFF800000000000
xorl %r12d, %r12d
movabsq $-0x7800000000000, %r15 # imm = 0xFFF8800000000000
xorl %r13d, %r13d
movq -0x98(%rbp), %rax
movq (%rax,%r12), %rax
andq %r14, %rax
cmpq %r15, %rax
je 0x43c24
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x1bf5e
leaq -0xd8(%rbp), %rdi
movq %rbx, %rsi
callq 0x1bbf0
movq -0x98(%rbp), %rax
movq (%rax,%r12), %rsi
movq -0xb8(%rbp), %rax
movq %rax, 0x20(%rsp)
movups -0xd8(%rbp), %xmm0
movups -0xc8(%rbp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0x78(%rbp), %rdi
callq 0x1ca05
movq -0x68(%rbp), %rax
movq %rax, 0x40(%rsp)
movups -0x78(%rbp), %xmm0
movups %xmm0, 0x30(%rsp)
leaq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x28(%rsp)
movups (%rcx), %xmm0
movups %xmm0, 0x18(%rsp)
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x3a, %esi
movl $0x1, %edx
callq 0x223bf
movq -0x98(%rbp), %rax
movq 0x8(%rax,%r12), %rsi
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x43a7e
testl %eax, %eax
je 0x43c24
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1be71
incq %r13
movslq -0xb0(%rbp), %rax
addq $0x10, %r12
cmpq %rax, %r13
jl 0x43b23
jmp 0x43e1a
leaq -0x4c(%rbp), %r15
movl $0x0, (%r15)
leaq -0x48(%rbp), %rax
movq $0x0, (%rax)
movq %rsi, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x4a23e
cmpl $0x0, (%r15)
jle 0x43e1a
movabsq $-0x800000000000, %r14  # imm = 0xFFFF800000000000
movl $0x1, %r15d
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x1bf5e
movq -0x48(%rbp), %rax
movq -0x8(%rax,%r15,8), %r12
movq %r12, %rax
andq %r14, %rax
movabsq $-0x5800000000000, %rcx # imm = 0xFFFA800000000000
cmpq %rcx, %rax
jne 0x43cc7
movq %r12, %rdi
callq 0x4f838
movq %rax, %rdi
leaq 0x1b2e8(%rip), %rsi     # 0x5efa2
callq 0x4920e
testl %eax, %eax
je 0x43e31
leaq -0x1(%r15), %rax
cmpq $0xff, %rax
ja 0x43d14
leal -0x1(%r15), %eax
leaq 0x10(%rbp), %rdx
movq 0x10(%rdx), %rcx
movq %rcx, 0x28(%rsp)
movups (%rdx), %xmm0
movups %xmm0, 0x18(%rsp)
movq -0x30(%rbp), %rcx
movq %rcx, 0x10(%rsp)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsp)
movzbl %al, %edx
movq %rbx, %rdi
movl $0x3d, %esi
movl $0x1, %ecx
callq 0x223b5
jmp 0x43d7a
leal -0x1(%r15), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x4fab8
leaq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x1bc4e
movq -0x68(%rbp), %rax
movq %rax, 0x40(%rsp)
movups -0x78(%rbp), %xmm0
movups %xmm0, 0x30(%rsp)
leaq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x28(%rsp)
movups (%rcx), %xmm0
movups %xmm0, 0x18(%rsp)
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x3a, %esi
movl $0x1, %edx
callq 0x223bf
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq -0x58(%rbp), %rdx
movq %r13, %rcx
callq 0x43a7e
testl %eax, %eax
je 0x43dbe
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1be71
movslq -0x4c(%rbp), %rax
leaq 0x1(%r15), %rcx
cmpq %rax, %r15
movq %rcx, %r15
jl 0x43c7e
jmp 0x43e1a
cmpl $0x5, %eax
jne 0x43e01
movq %rsi, %rdi
callq 0x4f838
movq %rbx, %rdi
movq %rax, %rsi
movq %r13, %rdx
movq -0x58(%rbp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
leaq 0x25b18(%rip), %rdi     # 0x69920
xorl %eax, %eax
callq 0x3fd86
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1bc87
movl $0x1, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x4c(%rbp), %r12d
cmpl %r12d, %r15d
jge 0x43e9f
leal 0x1(%r15), %eax
cmpl %r12d, %eax
jge 0x43eb3
movl %r12d, %r14d
subl %r15d, %r14d
movl %r14d, %edi
callq 0x48a50
orb $0x1, -0x1e(%rax)
testl %r14d, %r14d
jle 0x43e76
subl %r15d, %r12d
xorl %ecx, %ecx
movq -0x48(%rbp), %rdx
movq (%rdx,%r15,8), %rdx
movq %rdx, (%rax,%rcx,8)
incq %rcx
incq %r15
cmpq %rcx, %r12
jne 0x43e5f
movq %rax, %rdi
callq 0x48a81
movabsq $-0x4000000000000, %rsi # imm = 0xFFFC000000000000
movq %rax, %rdi
callq 0x4f9ad
leaq 0x25ae8(%rip), %rdi     # 0x6997f
movq %rax, %rsi
jmp 0x43e08
leaq 0x25aa3(%rip), %rsi     # 0x69949
movq %rbx, %rdi
callq 0x1bc9e
jmp 0x43e1a
movq -0x48(%rbp), %rax
movq (%rax,%r15,8), %rsi
andq %rsi, %r14
movabsq $-0x5800000000000, %rax # imm = 0xFFFA800000000000
cmpq %rax, %r14
jne 0x44191
movq %r13, -0x60(%rbp)
leaq -0x78(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1bf5e
leaq -0x98(%rbp), %rdi
movq %rbx, %rsi
callq 0x1bf5e
leaq -0xb0(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1bf5e
leal -0x1(%r15), %eax
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
movswl %ax, %edx
xorl %r14d, %r14d
movq %rbx, %rdi
movl $0x2b, %esi
xorl %ecx, %ecx
callq 0x220f0
leaq 0x10(%rbp), %r13
movq 0x10(%r13), %rax
movq %rax, 0x28(%rsp)
movups (%r13), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x3f, %esi
xorl %edx, %edx
callq 0x22104
movq 0x10(%r12), %rax
movq %rax, 0x40(%rsp)
movups (%r12), %xmm0
movups %xmm0, 0x30(%rsp)
leaq -0x78(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x28(%rsp)
movups (%rcx), %xmm0
movups %xmm0, 0x18(%rsp)
leaq -0x98(%rbp), %r13
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x23, %esi
xorl %edx, %edx
callq 0x223bf
movl %eax, %r12d
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x1e, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x220f0
movl %eax, -0x7c(%rbp)
leaq -0x78(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x40(%rsp)
movups (%rcx), %xmm0
movups %xmm0, 0x30(%rsp)
leaq 0x10(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x28(%rsp)
movups (%rcx), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x3b, %esi
xorl %edx, %edx
callq 0x223bf
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x31, %esi
xorl %edx, %edx
callq 0x21e42
leaq -0x78(%rbp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x28(%rsp)
movups (%rcx), %xmm0
movups %xmm0, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x5, %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x22283
movq 0x8(%rbx), %rax
movl $0x0, %r13d
testq %rax, %rax
je 0x44093
movl -0x4(%rax), %r13d
movq %rbx, %rdi
movl $0x1c, %esi
callq 0x21a88
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x440ad
movl -0x4(%rax), %r14d
movl -0x7c(%rbp), %ecx
subl %ecx, %r14d
shll $0x10, %r14d
movslq %ecx, %rcx
orl %r14d, (%rax,%rcx,4)
subl %r13d, %r12d
shll $0x8, %r12d
movslq %r13d, %rcx
orl %r12d, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x78(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1be71
movq -0x88(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x98(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1be71
movq -0xa0(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xb0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1be71
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x45, %esi
movl $0x1, %edx
callq 0x21e42
movq -0x48(%rbp), %rax
movq (%rax,%r15,8), %rdi
callq 0x4f838
movq -0x30(%rbp), %rcx
movq %rcx, 0x10(%rsp)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq %rax, %rsi
movq -0x60(%rbp), %rdx
callq *-0x58(%rbp)
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1be71
jmp 0x43e1a
leaq 0x2582d(%rip), %rdi     # 0x699c5
jmp 0x43e08