// File: /workspace/github_c_makefile_star2_O3/Paxa[P]fast_excel/libxlsxwriter/src/hash_table.c
// Function: lxw_insert_hash_element
lxw_hash_element *
lxw_insert_hash_element(lxw_hash_table *lxw_hash, void *key, void *value,
                        size_t key_len)
{
    size_t hash_key = _generate_hash_key(key, key_len, lxw_hash->num_buckets);
    struct lxw_hash_bucket_list *list = NULL;
    lxw_hash_element *element = NULL;

    if (!lxw_hash->buckets[hash_key]) {
        /* The key isn't in the LXW_HASH hash table. */

        /* Create a linked list in the bucket to hold the lxw_hash keys. */
        list = calloc(1, sizeof(struct lxw_hash_bucket_list));
        GOTO_LABEL_ON_MEM_ERROR(list, mem_error1);

        /* Initialize the bucket linked list. */
        SLIST_INIT(list);

        /* Create an lxw_hash element to add to the linked list. */
        element = calloc(1, sizeof(lxw_hash_element));
        GOTO_LABEL_ON_MEM_ERROR(element, mem_error1);

        /* Store the key and value. */
        element->key = key;
        element->value = value;

        /* Add the lxw_hash element to the bucket's linked list. */
        SLIST_INSERT_HEAD(list, element, lxw_hash_list_pointers);

        /* Also add it to the insertion order linked list. */
        STAILQ_INSERT_TAIL(lxw_hash->order_list, element,
                           lxw_hash_order_pointers);

        /* Store the bucket list at the hash index. */
        lxw_hash->buckets[hash_key] = list;

        lxw_hash->used_buckets++;
        lxw_hash->unique_count++;

        return element;
    }
    else {
        /* The key is already in the table or there is a hash collision. */
        list = lxw_hash->buckets[hash_key];

        /* Iterate over the keys in the bucket's linked list. */
        SLIST_FOREACH(element, list, lxw_hash_list_pointers) {
            if (memcmp(element->key, key, key_len) == 0) {
                /* The key already exists in the table. Update the value. */
                if (lxw_hash->free_value)
                    free(element->value);

                element->value = value;
                return element;
            }
        }

        /* Key doesn't exist in the list so this is a hash collision.
         * Create an lxw_hash element to add to the linked list. */
        element = calloc(1, sizeof(lxw_hash_element));
        GOTO_LABEL_ON_MEM_ERROR(element, mem_error2);

        /* Store the key and value. */
        element->key = key;
        element->value = value;

        /* Add the lxw_hash element to the bucket linked list. */
        SLIST_INSERT_HEAD(list, element, lxw_hash_list_pointers);

        /* Also add it to the insertion order linked list. */
        STAILQ_INSERT_TAIL(lxw_hash->order_list, element,
                           lxw_hash_order_pointers);

        lxw_hash->unique_count++;

        return element;
    }

mem_error1:
    free(list);

mem_error2:
    free(element);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r13
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
movl $0x811c9dc5, %eax       # imm = 0x811C9DC5
movl (%rdi), %ecx
testq %r13, %r13
je 0x1f3f1
xorl %edx, %edx
imulq $0x1000193, %rax, %rsi  # imm = 0x1000193
movzbl (%r15,%rdx), %eax
xorq %rsi, %rax
incq %rdx
cmpq %rdx, %r13
jne 0x1f3da
xorl %edx, %edx
divq %rcx
movq 0x18(%rbx), %rax
movq (%rax,%rdx,8), %r14
testq %r14, %r14
je 0x1f488
movq %r12, (%rsp)
movq (%r14), %rbp
testq %rbp, %rbp
je 0x1f437
movq %rbp, %r12
movq (%r12), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0xcaf0
testl %eax, %eax
je 0x1f4fa
movq 0x18(%r12), %r12
testq %r12, %r12
jne 0x1f416
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x1f515
movq %rax, %r12
movq %r15, (%rax)
movq (%rsp), %rax
movq %rax, 0x8(%r12)
movq %rbp, 0x18(%r12)
movq %r12, (%r14)
movq %r12, %rax
addq $0x10, %rax
movq 0x10(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq %r12, (%rcx)
movq 0x10(%rbx), %rcx
movq %rax, 0x8(%rcx)
incl 0x8(%rbx)
jmp 0x1f533
movq %rdx, %rbp
movl $0x1, %edi
movl $0x8, %esi
callq 0xc7f0
movq %rax, %r13
testq %rax, %rax
je 0x1f51c
movq %r12, %r14
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x1f523
movq %rax, %r12
movq %r15, (%rax)
movq %r14, 0x8(%rax)
movq %rax, (%r13)
addq $0x10, %rax
movq 0x10(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq %r12, (%rcx)
movq 0x10(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%rbx), %rax
movq %r13, (%rax,%rbp,8)
movq 0x4(%rbx), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x4(%rbx)
jmp 0x1f533
cmpb $0x0, 0xd(%rbx)
je 0x1f50a
movq 0x8(%r12), %rdi
callq 0xc0c0
movq (%rsp), %rax
movq %rax, 0x8(%r12)
jmp 0x1f533
callq 0xdb9e
jmp 0x1f530
movl $0x52, %edi
jmp 0x1f528
movl $0x59, %edi
movq %r13, %rsi
callq 0xdbc2
xorl %r12d, %r12d
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq