// File: /workspace/github_c_makefile_star2_O2/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, -0x58(%rbp)
movq %rdx, -0x60(%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 0x3fd42
leal -0x9(%rax), %ecx
cmpl $0x2, %ecx
jae 0x3feb2
movabsq $-0x800000000000, %r13  # imm = 0xFFFF800000000000
leaq -0x98(%rbp), %rax
andq $0x0, (%rax)
leaq -0xb0(%rbp), %rcx
andl $0x0, (%rcx)
leaq -0x48(%rbp), %rdx
andl $0x0, (%rdx)
movq %rsi, %rdi
movq %rax, %rsi
callq 0x45d86
xorl %r15d, %r15d
movabsq $-0x7800000000000, %r12 # imm = 0xFFF8800000000000
xorl %r14d, %r14d
movslq -0xb0(%rbp), %rax
cmpq %rax, %r14
jge 0x3ff4d
movq -0x98(%rbp), %rax
movq (%rax,%r15), %rax
andq %r13, %rax
cmpq %r12, %rax
je 0x3fd36
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x1a259
leaq -0xd8(%rbp), %rdi
movq %rbx, %rsi
callq 0x19f04
movq -0x98(%rbp), %rax
movq (%rax,%r15), %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 0x1abbf
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
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x2010f
movq -0x98(%rbp), %rax
movq 0x8(%rax,%r15), %rsi
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
callq 0x3fb99
testl %eax, %eax
je 0x3fd36
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1a177
incq %r14
addq $0x10, %r15
jmp 0x3fc29
movabsq $-0x800000000000, %r13  # imm = 0xFFFF800000000000
leaq -0x4c(%rbp), %rdx
andl $0x0, (%rdx)
leaq -0x48(%rbp), %rax
andq $0x0, (%rax)
movq %rsi, %rdi
movq %rax, %rsi
callq 0x45c58
pushq $0x1
popq %r15
leaq -0x1(%r15), %r14
movslq -0x4c(%rbp), %rax
cmpq %rax, %r14
jge 0x3ff4d
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x1a259
movq -0x48(%rbp), %rax
movq -0x8(%rax,%r15,8), %r12
movq %r12, %rax
andq %r13, %rax
movabsq $-0x5800000000000, %rcx # imm = 0xFFFA800000000000
cmpq %rcx, %rax
jne 0x3fdc4
movq %r12, %rdi
callq 0x4b1f1
movq %rax, %rdi
leaq 0x1a1eb(%rip), %rsi     # 0x59fa2
callq 0x44ca9
testl %eax, %eax
je 0x3fefb
leal -0x1(%r15), %eax
cmpq $0xff, %r14
ja 0x3fe0a
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
pushq $0x3d
popq %rsi
pushq $0x1
popq %rcx
callq 0x20105
jmp 0x3fe65
cvtsi2sd %eax, %xmm0
callq 0x4b471
leaq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x19f5c
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
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x2010f
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq -0x60(%rbp), %rdx
movq -0x58(%rbp), %rcx
callq 0x3fb99
testl %eax, %eax
je 0x3feaa
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1a177
incq %r15
jmp 0x3fd6a
cmpl $0x5, %eax
jne 0x3fee0
movq %rsi, %rdi
callq 0x4b1f1
movq %rbx, %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
leaq 0x24a39(%rip), %rdi     # 0x64920
xorl %eax, %eax
callq 0x3c0ed
movq %rbx, %rdi
movq %rax, %rsi
callq 0x19f92
jmp 0x3ff4d
movl -0x4c(%rbp), %r12d
cmpl %r12d, %r15d
jge 0x3ff3e
leal 0x1(%r15), %eax
cmpl %r12d, %eax
jge 0x3ff62
subq %r15, %r12
movl %r12d, %edi
callq 0x44648
orb $0x1, -0x1e(%rax)
movslq %r12d, %rcx
xorl %edx, %edx
cmpq %rcx, %rdx
jge 0x4021f
movq -0x48(%rbp), %rsi
movq (%rsi,%r15,8), %rsi
movq %rsi, (%rax,%rdx,8)
incq %rdx
incq %r15
jmp 0x3ff21
leaq 0x24a04(%rip), %rsi     # 0x64949
movq %rbx, %rdi
callq 0x19fa9
pushq $0x1
popq %rax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x48(%rbp), %rax
movq (%rax,%r15,8), %rsi
andq %rsi, %r13
movabsq $-0x5800000000000, %rax # imm = 0xFFFA800000000000
cmpq %rax, %r13
jne 0x40248
leaq -0x78(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1a259
leaq -0x98(%rbp), %rdi
movq %rbx, %rsi
callq 0x1a259
leaq -0xb0(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1a259
leal -0x1(%r15), %eax
movq 0x10(%r14), %rcx
movq %rcx, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
movswl %ax, %edx
pushq $0x2b
popq %rsi
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x1fe75
leaq 0x10(%rbp), %r14
movq 0x10(%r14), %rax
movq %rax, 0x28(%rsp)
movups (%r14), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
pushq $0x3f
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1fe89
movq 0x10(%r13), %rax
movq %rax, 0x40(%rsp)
movups (%r13), %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), %r14
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
pushq $0x23
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2010f
movl %eax, %r13d
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
pushq $0x1e
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1fe75
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(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
pushq $0x3b
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x2010f
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
pushq $0x31
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1fbb3
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)
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x1ffee
movq 0x8(%rbx), %rax
movl $0x0, %r14d
testq %rax, %rax
je 0x40127
movl -0x4(%rax), %r14d
pushq $0x1c
popq %rsi
movq %rbx, %rdi
callq 0x1f7c4
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x4013f
movl -0x4(%rax), %r12d
movl -0x7c(%rbp), %ecx
subl %ecx, %r12d
shll $0x10, %r12d
movslq %ecx, %rcx
orl %r12d, (%rax,%rcx,4)
subl %r14d, %r13d
shll $0x8, %r13d
movslq %r14d, %rcx
orl %r13d, (%rax,%rcx,4)
movq -0x68(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x78(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1a177
movq -0x88(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x98(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1a177
movq -0xa0(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xb0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1a177
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsp)
pushq $0x45
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x1fbb3
movq -0x48(%rbp), %rax
movq (%rax,%r15,8), %rdi
callq 0x4b1f1
movq -0x30(%rbp), %rcx
movq %rcx, 0x10(%rsp)
movups -0x40(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movq %rax, %rsi
movq -0x58(%rbp), %rdx
callq *-0x60(%rbp)
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1a177
jmp 0x3ff4d
movq %rax, %rdi
callq 0x44674
movabsq $-0x4000000000000, %rsi # imm = 0xFFFC000000000000
movq %rax, %rdi
callq 0x4b366
leaq 0x2473f(%rip), %rdi     # 0x6497f
movq %rax, %rsi
jmp 0x3fee7
leaq 0x24776(%rip), %rdi     # 0x649c5
jmp 0x3fee7