// File: /workspace/github_c_makefile_star2_O1/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 0x1f2f9
xorl %edx, %edx
imulq $0x1000193, %rax, %rsi  # imm = 0x1000193
movzbl (%r15,%rdx), %eax
xorq %rsi, %rax
incq %rdx
cmpq %rdx, %r13
jne 0x1f2e2
xorl %edx, %edx
divq %rcx
movq 0x18(%rbx), %rax
movq (%rax,%rdx,8), %r14
testq %r14, %r14
je 0x1f386
movq %r12, %rbp
movq (%r14), %r12
testq %r12, %r12
je 0x1f334
movq (%r12), %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0xcaf0
testl %eax, %eax
je 0x1f407
movq 0x18(%r12), %r12
jmp 0x1f311
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x1f41e
movq %rax, %r12
movq %r15, (%rax)
movq %rbp, 0x8(%rax)
movq (%r14), %rax
movq %rax, 0x18(%r12)
movq %r12, (%r14)
movq %r12, %rax
addq $0x10, %rax
movq $0x0, 0x10(%r12)
movq 0x10(%rbx), %rcx
movq 0x8(%rcx), %rcx
movq %r12, (%rcx)
movq 0x10(%rbx), %rcx
movq %rax, 0x8(%rcx)
jmp 0x1f402
movq %rdx, %rbp
movl $0x1, %edi
movl $0x8, %esi
callq 0xc7f0
movq %rax, %r13
testq %rax, %rax
je 0x1f425
movq %r12, %r14
movq $0x0, (%r13)
movl $0x1, %edi
movl $0x20, %esi
callq 0xc7f0
testq %rax, %rax
je 0x1f42c
movq %rax, %r12
movq %r15, (%rax)
movq %r14, 0x8(%rax)
xorl %eax, %eax
movq %rax, 0x18(%r12)
movq %r12, (%r13)
movq %r12, %rcx
addq $0x10, %rcx
movq %rax, 0x10(%r12)
movq 0x10(%rbx), %rax
movq 0x8(%rax), %rax
movq %r12, (%rax)
movq 0x10(%rbx), %rax
movq %rcx, 0x8(%rax)
movq 0x18(%rbx), %rax
movq %r13, (%rax,%rbp,8)
incl 0x4(%rbx)
incl 0x8(%rbx)
jmp 0x1f43c
cmpb $0x0, 0xd(%rbx)
je 0x1f417
movq 0x8(%r12), %rdi
callq 0xc0c0
movq %rbp, 0x8(%r12)
jmp 0x1f43c
callq 0xdb8c
jmp 0x1f439
movl $0x52, %edi
jmp 0x1f431
movl $0x59, %edi
movq %r13, %rsi
callq 0xdbb0
xorl %r12d, %r12d
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq