// File: /workspace/github_c_makefile_star2_O2/janet-lang[P]janet/src/core/struct.c
// Function: janet_struct_put_ext
void janet_struct_put_ext(JanetKV *st, Janet key, Janet value, int replace) {
    int32_t cap = janet_struct_capacity(st);
    int32_t hash = janet_hash(key);
    int32_t index = janet_maphash(cap, hash);
    int32_t i, j, dist;
    int32_t bounds[4] = {index, cap, 0, index};
    if (janet_checktype(key, JANET_NIL) || janet_checktype(value, JANET_NIL)) return;
    if (janet_checktype(key, JANET_NUMBER) && isnan(janet_unwrap_number(key))) return;
    /* Avoid extra items */
    if (janet_struct_hash(st) == janet_struct_length(st)) return;
    for (dist = 0, j = 0; j < 4; j += 2)
        for (i = bounds[j]; i < bounds[j + 1]; i++, dist++) {
            int status;
            int32_t otherhash;
            int32_t otherindex, otherdist;
            JanetKV *kv = st + i;
            /* We found an empty slot, so just add key and value */
            if (janet_checktype(kv->key, JANET_NIL)) {
                kv->key = key;
                kv->value = value;
                /* Update the temporary count */
                janet_struct_hash(st)++;
                return;
            }
            /* Robinhood hashing - check if colliding kv pair
             * is closer to their source than current. We use robinhood
             * hashing to ensure that equivalent structs that are constructed
             * with different order have the same internal layout, and therefor
             * will compare properly - i.e., {1 2 3 4} should equal {3 4 1 2}.
             * Collisions are resolved via an insertion sort insertion. */
            otherhash = janet_hash(kv->key);
            otherindex = janet_maphash(cap, otherhash);
            otherdist = (i + cap - otherindex) & (cap - 1);
            if (dist < otherdist)
                status = -1;
            else if (otherdist < dist)
                status = 1;
            else if (hash < otherhash)
                status = -1;
            else if (otherhash < hash)
                status = 1;
            else
                status = janet_compare(key, kv->key);
            /* If other is closer to their ideal slot */
            if (status == 1) {
                /* Swap current kv pair with pair in slot */
                JanetKV temp = *kv;
                kv->key = key;
                kv->value = value;
                key = temp.key;
                value = temp.value;
                /* Save dist and hash of new kv pair */
                dist = otherdist;
                hash = otherhash;
            } else if (status == 0) {
                if (replace) {
                    /* A key was added to the struct more than once - replace old value */
                    kv->value = value;
                }
                return;
            }
        }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movl -0x10(%rdi), %r12d
movq %rsi, %rdi
callq 0x46ba4
leal -0x1(%r12), %ecx
movl %eax, -0x3c(%rbp)
movl %ecx, -0x44(%rbp)
andl %ecx, %eax
movl %eax, -0x60(%rbp)
movl %r12d, -0x5c(%rbp)
andl $0x0, -0x58(%rbp)
movl %eax, -0x54(%rbp)
movq %r13, -0x38(%rbp)
shrq $0x2f, %r13
movl $0x1fff1, %ecx          # imm = 0x1FFF1
cmpl %ecx, %r13d
sete %al
movq %rbx, %rdx
shrq $0x2f, %rdx
cmpl %ecx, %edx
sete %cl
orb %al, %cl
jne 0x42ff0
movq -0x38(%rbp), %rdx
movq %rdx, %xmm0
ucomisd %xmm0, %xmm0
setp %al
movabsq $0x7800000000000, %rcx  # imm = 0x7800000000000
testq %rcx, %rdx
sete %cl
testb %cl, %al
jne 0x42ff0
movl -0x14(%r15), %eax
cmpl -0x18(%r15), %eax
je 0x42ff0
xorl %edx, %edx
xorl %esi, %esi
movl %r14d, -0x40(%rbp)
movq %r15, -0x68(%rbp)
movq %r12, -0x70(%rbp)
cmpq $0x3, %rdx
ja 0x42ff0
movq %rbx, -0x30(%rbp)
movslq -0x60(%rbp,%rdx,4), %r9
movslq -0x5c(%rbp,%rdx,4), %rax
movq %rax, -0x80(%rbp)
leal (%r12,%r9), %r8d
movq %r9, %r14
shlq $0x4, %r14
addq %r15, %r14
movq %rdx, -0x78(%rbp)
cmpq -0x80(%rbp), %r9
jge 0x42fc5
movq (%r14), %rdi
movq %rdi, %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rcx, %rax
movabsq $-0x7800000000000, %rcx # imm = 0xFFF8800000000000
cmpq %rcx, %rax
je 0x42fda
movq %r9, -0x88(%rbp)
movl %esi, %r12d
movl %r8d, %ebx
callq 0x46ba4
movl %ebx, %r8d
movl %r12d, %edx
movl %eax, %r13d
movl -0x44(%rbp), %ecx
andl %ecx, %eax
movl %ebx, %r15d
subl %eax, %r15d
andl %ecx, %r15d
cmpl %r15d, %r12d
jge 0x42f5c
movl %edx, %r15d
jmp 0x42f80
jle 0x42fa0
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq -0x38(%rbp), %rdx
movq %rdx, (%r14)
movq -0x30(%rbp), %rdx
movq %rdx, 0x8(%r14)
movl %r13d, -0x3c(%rbp)
movq %rcx, -0x30(%rbp)
movq %rax, -0x38(%rbp)
movq -0x88(%rbp), %r9
incq %r9
incl %r15d
incl %r8d
addq $0x10, %r14
movl %r15d, %esi
movq -0x78(%rbp), %rdx
jmp 0x42ef9
cmpl %r13d, -0x3c(%rbp)
jl 0x42f57
jg 0x42f5e
movq (%r14), %rsi
movq -0x38(%rbp), %rdi
callq 0x46cba
movl %ebx, %r8d
cmpl $0x1, %eax
je 0x42f5e
testl %eax, %eax
je 0x42fff
movl %r12d, %r15d
jmp 0x42f80
addq $0x2, %rdx
movq -0x30(%rbp), %rbx
movq -0x68(%rbp), %r15
movq -0x70(%rbp), %r12
jmp 0x42ecb
movq -0x38(%rbp), %rax
movq %rax, (%r14)
movq -0x30(%rbp), %rax
movq %rax, 0x8(%r14)
movq -0x68(%rbp), %rax
incl -0x14(%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, -0x40(%rbp)
movq -0x30(%rbp), %rax
je 0x42ff0
movq %rax, 0x8(%r14)
jmp 0x42ff0