// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r15
movl -0x10(%rdi), %r12d
movq %rsi, %rdi
callq 0x4a67f
leal -0x1(%r12), %ecx
movl %eax, -0x40(%rbp)
movl %ecx, -0x4c(%rbp)
andl %ecx, %eax
movl %eax, -0x60(%rbp)
movl %r12d, -0x5c(%rbp)
movl $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 0x46896
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 0x46896
movl -0x14(%r15), %eax
cmpl -0x18(%r15), %eax
je 0x46896
xorl %esi, %esi
movl $0x1, %eax
leaq -0x60(%rbp), %rcx
movb $0x1, %dl
movl %edx, -0x3c(%rbp)
movq %r15, -0x68(%rbp)
movq %r12, -0x70(%rbp)
movl %r14d, -0x48(%rbp)
movq %rbx, -0x30(%rbp)
movslq (%rcx), %rcx
movl -0x60(%rbp,%rax,4), %r14d
cmpl %ecx, %r14d
jle 0x46852
addl %ecx, %r12d
movq %rcx, %rbx
shlq $0x4, %rbx
addq %r15, %rbx
subl %ecx, %r14d
movq (%rbx), %rdi
movq %rdi, %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rcx, %rax
movabsq $-0x7800000000000, %rcx # imm = 0xFFF8800000000000
cmpq %rcx, %rax
je 0x46880
movl %esi, -0x44(%rbp)
callq 0x4a67f
movl -0x44(%rbp), %edx
movl %eax, %r13d
movl -0x4c(%rbp), %ecx
andl %ecx, %eax
movl %r12d, %r15d
subl %eax, %r15d
andl %ecx, %r15d
cmpl %r15d, %edx
jge 0x467f1
movl %edx, %r15d
jmp 0x46815
jle 0x4682d
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movq -0x38(%rbp), %rdx
movq %rdx, (%rbx)
movq -0x30(%rbp), %rdx
movq %rdx, 0x8(%rbx)
movl %r13d, -0x40(%rbp)
movq %rcx, -0x30(%rbp)
movq %rax, -0x38(%rbp)
incl %r15d
incl %r12d
addq $0x10, %rbx
movl %r15d, %esi
decl %r14d
jne 0x467a5
jmp 0x46855
cmpl %r13d, -0x40(%rbp)
jl 0x467ec
jg 0x467f3
movq (%rbx), %rsi
movq -0x38(%rbp), %rdi
callq 0x4a795
movl -0x44(%rbp), %ecx
cmpl $0x1, %eax
je 0x467f3
testl %eax, %eax
je 0x468a5
movl %ecx, %r15d
jmp 0x46815
movl %esi, %r15d
movl $0x3, %eax
leaq -0x58(%rbp), %rcx
movl %r15d, %esi
testb $0x1, -0x3c(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x30(%rbp), %rbx
movq -0x68(%rbp), %r15
movq -0x70(%rbp), %r12
jne 0x46780
jmp 0x46896
movq -0x38(%rbp), %rax
movq %rax, (%rbx)
movq -0x30(%rbp), %rax
movq %rax, 0x8(%rbx)
movq -0x68(%rbp), %rax
incl -0x14(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, -0x48(%rbp)
movq -0x30(%rbp), %rax
je 0x46896
movq %rax, 0x8(%rbx)
jmp 0x46896