// File: /workspace/github_c_makefile_star2_O3/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 0x43324
leal -0x9(%rax), %ecx
cmpl $0x2, %ecx
jae 0x434b8
movq %r13, -0x60(%rbp)
leaq -0x98(%rbp), %rax
movq $0x0, (%rax)
xorl %ecx, %ecx
leaq -0xb0(%rbp), %r15
movl %ecx, (%r15)
leaq -0x48(%rbp), %rdx
movl %ecx, (%rdx)
movq %rsi, %rdi
movq %rax, %rsi
movq %r15, %rcx
callq 0x4985f
movl (%r15), %eax
testl %eax, %eax
jle 0x434fe
movabsq $-0x800000000000, %r14  # imm = 0xFFFF800000000000
xorl %r12d, %r12d
movabsq $-0x7800000000000, %r15 # imm = 0xFFF8800000000000
xorl %r13d, %r13d
movq -0x98(%rbp), %rcx
movq (%rcx,%r12), %rcx
andq %r14, %rcx
cmpq %r15, %rcx
je 0x4330c
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x1be05
leaq -0xd8(%rbp), %rdi
movq %rbx, %rsi
callq 0x1ba98
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 0x1c7be
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 0x22063
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 0x43160
testl %eax, %eax
je 0x43306
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1bd19
movl -0xb0(%rbp), %eax
incq %r13
movslq %eax, %rcx
addq $0x10, %r12
cmpq %rcx, %r13
jl 0x43205
jmp 0x434fe
leaq -0x4c(%rbp), %r15
movl $0x0, (%r15)
leaq -0x48(%rbp), %rax
movq $0x0, (%rax)
movq %rsi, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x49726
cmpl $0x0, (%r15)
jle 0x434fe
movabsq $-0x800000000000, %r14  # imm = 0xFFFF800000000000
movl $0x1, %r15d
leaq -0x40(%rbp), %rdi
movq %rbx, %rsi
callq 0x1be05
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 0x433ab
movq %r12, %rdi
callq 0x4ebc4
movq %rax, %rdi
leaq 0x1ac04(%rip), %rsi     # 0x5dfa2
callq 0x4872e
testl %eax, %eax
je 0x43515
leaq -0x1(%r15), %rax
cmpq $0xff, %rax
ja 0x433f8
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 0x22059
jmp 0x4345e
leal -0x1(%r15), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
callq 0x4ee44
leaq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x1baf6
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 0x22063
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 0x43160
testl %eax, %eax
je 0x434a2
movq -0x30(%rbp), %rax
movq %rax, 0x10(%rsp)
movupd -0x40(%rbp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1bd19
movslq -0x4c(%rbp), %rax
leaq 0x1(%r15), %rcx
cmpq %rax, %r15
movq %rcx, %r15
jl 0x43362
jmp 0x434fe
cmpl $0x5, %eax
jne 0x434e5
movq %rsi, %rdi
callq 0x4ebc4
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 0x25414(%rip), %rdi     # 0x68900
xorl %eax, %eax
callq 0x3f652
movq %rbx, %rdi
movq %rax, %rsi
callq 0x1bb2f
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 0x43583
leal 0x1(%r15), %eax
cmpl %r12d, %eax
jge 0x43597
movl %r12d, %r14d
subl %r15d, %r14d
movl %r14d, %edi
callq 0x47fa0
orb $0x1, -0x1e(%rax)
testl %r14d, %r14d
jle 0x4355a
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 0x43543
movq %rax, %rdi
callq 0x47fd1
movabsq $-0x4000000000000, %rsi # imm = 0xFFFC000000000000
movq %rax, %rdi
callq 0x4ed39
leaq 0x253e4(%rip), %rdi     # 0x6895f
movq %rax, %rsi
jmp 0x434ec
leaq 0x2539f(%rip), %rsi     # 0x68929
movq %rbx, %rdi
callq 0x1bb46
jmp 0x434fe
movq -0x48(%rbp), %rax
movq (%rax,%r15,8), %rsi
andq %rsi, %r14
movabsq $-0x5800000000000, %rax # imm = 0xFFFA800000000000
cmpq %rax, %r14
jne 0x43875
movq %r13, -0x60(%rbp)
leaq -0x78(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1be05
leaq -0x98(%rbp), %rdi
movq %rbx, %rsi
callq 0x1be05
leaq -0xb0(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1be05
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 0x21d94
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 0x21da8
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 0x22063
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 0x21d94
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 0x22063
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
movl $0x31, %esi
xorl %edx, %edx
callq 0x21ae6
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 0x21f27
movq 0x8(%rbx), %rax
movl $0x0, %r13d
testq %rax, %rax
je 0x43777
movl -0x4(%rax), %r13d
movq %rbx, %rdi
movl $0x1c, %esi
callq 0x21728
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x43791
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 0x1bd19
movq -0x88(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0x98(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1bd19
movq -0xa0(%rbp), %rax
movq %rax, 0x10(%rsp)
movups -0xb0(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x1bd19
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 0x21ae6
movq -0x48(%rbp), %rax
movq (%rax,%r15,8), %rdi
callq 0x4ebc4
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 0x1bd19
jmp 0x434fe
leaq 0x25129(%rip), %rdi     # 0x689a5
jmp 0x434ec