// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movl %ecx, -0x58(%rbp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, -0x50(%rbp)
movl -0x10(%rdi), %ebx
movq %rsi, %rdi
callq 0x4b1e3
leal -0x1(%rbx), %ecx
movl %eax, -0x34(%rbp)
movl %ecx, -0x5c(%rbp)
andl %ecx, %eax
movl %eax, -0x70(%rbp)
movq %rbx, -0x78(%rbp)
movl %ebx, -0x6c(%rbp)
movl $0x0, -0x68(%rbp)
movl %eax, -0x64(%rbp)
movq %r14, -0x30(%rbp)
shrq $0x2f, %r14
movl $0x1fff1, %ecx          # imm = 0x1FFF1
cmpl %ecx, %r14d
sete %al
movq %r12, %rdx
shrq $0x2f, %rdx
cmpl %ecx, %edx
sete %cl
orb %al, %cl
jne 0x47173
movq -0x30(%rbp), %rdx
movq %rdx, %xmm0
ucomisd %xmm0, %xmm0
setp %al
movabsq $0x7800000000000, %rcx  # imm = 0x7800000000000
testq %rcx, %rdx
sete %cl
testb %cl, %al
jne 0x47173
movq -0x50(%rbp), %rcx
movl -0x14(%rcx), %eax
cmpl -0x18(%rcx), %eax
jne 0x47182
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, -0x48(%rbp)
xorl %r14d, %r14d
movq -0x48(%rbp), %rax
movslq -0x70(%rbp,%rax,4), %r15
movslq -0x6c(%rbp,%rax,4), %rbx
cmpl %ebx, %r15d
jge 0x472a0
movq -0x78(%rbp), %rax
leal (%rax,%r15), %r13d
subq %r15, %rbx
shlq $0x4, %r15
addq -0x50(%rbp), %r15
movq (%r15), %rdi
movq %rdi, %rax
movabsq $-0x800000000000, %rcx  # imm = 0xFFFF800000000000
andq %rcx, %rax
movabsq $-0x7800000000000, %rcx # imm = 0xFFF8800000000000
cmpq %rcx, %rax
jne 0x471f2
movq -0x30(%rbp), %rax
movq %rax, (%r15)
movq %r12, 0x8(%r15)
movq -0x50(%rbp), %rax
incl -0x14(%rax)
xorl %ecx, %ecx
jmp 0x47285
movq %r12, -0x40(%rbp)
callq 0x4b1e3
movl %eax, %edi
movl -0x5c(%rbp), %ecx
andl %ecx, %eax
movl %r13d, %r12d
subl %eax, %r12d
andl %ecx, %r12d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %r12d, %r14d
jl 0x4723c
jle 0x4721e
movl $0x1, %eax
jmp 0x4723c
cmpl %edi, -0x34(%rbp)
jl 0x4723c
movl $0x1, %eax
jg 0x4723c
movq (%r15), %rsi
movl %edi, -0x54(%rbp)
movq -0x30(%rbp), %rdi
callq 0x4b30c
movl -0x54(%rbp), %edi
testl %eax, %eax
je 0x4726c
movb $0x1, %cl
cmpl $0x1, %eax
jne 0x47281
movq (%r15), %rax
movq 0x8(%r15), %rdx
movq -0x30(%rbp), %rsi
movq %rsi, (%r15)
movq -0x40(%rbp), %rsi
movq %rsi, 0x8(%r15)
movl %r12d, %r14d
movl %edi, -0x34(%rbp)
movq %rdx, %r12
movq %rax, -0x30(%rbp)
jmp 0x47285
cmpl $0x0, -0x58(%rbp)
je 0x4727f
movq -0x40(%rbp), %r12
movq %r12, 0x8(%r15)
jmp 0x471eb
xorl %ecx, %ecx
movq -0x40(%rbp), %r12
testb %cl, %cl
je 0x47173
incl %r14d
incl %r13d
addq $0x10, %r15
decq %rbx
jne 0x471b7
movq -0x48(%rbp), %rcx
leaq 0x2(%rcx), %rax
testq %rcx, %rcx
movq %rax, -0x48(%rbp)
je 0x4718d
jmp 0x47173