// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq %rsi, %r12
movq %rdi, %r14
movl (%rdi), %edx
movq %rsi, %rdi
movq %rcx, %rsi
callq 0x1c3e0
movq %rax, %rbp
movq 0x18(%r14), %rax
movq (%rax,%rbp,8), %r15
testq %r15, %r15
je 0x1c46d
movq %rbx, %rbp
movq %r15, %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x1c4de
movq (%rbx), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xcb60
testl %eax, %eax
je 0x1c526
addq $0x18, %rbx
jmp 0x1c445
pushq $0x1
popq %rdi
pushq $0x8
popq %rsi
callq 0xc850
movq %rax, %r13
testq %rax, %rax
je 0x1c54c
movq %rbx, %r15
pushq $0x1
popq %rdi
pushq $0x20
popq %rsi
callq 0xc850
testq %rax, %rax
je 0x1c550
movq %rax, %rbx
movq %r12, (%rax)
movq %r15, 0x8(%rax)
movq %rax, (%r13)
addq $0x10, %rax
movq 0x10(%r14), %rcx
movq 0x8(%rcx), %rcx
movq %rbx, (%rcx)
movq 0x10(%r14), %rcx
movq %rax, 0x8(%rcx)
movq 0x18(%r14), %rax
movq %r13, (%rax,%rbp,8)
movq 0x4(%r14), %xmm0
pcmpeqd %xmm1, %xmm1
psubd %xmm1, %xmm0
movq %xmm0, 0x4(%r14)
jmp 0x1c53a
pushq $0x1
popq %rdi
pushq $0x20
popq %rsi
callq 0xc850
testq %rax, %rax
je 0x1c57e
movq %rax, %rbx
movq %r12, (%rax)
movq %rbp, 0x8(%rax)
movq (%r15), %rax
movq %rax, 0x18(%rbx)
movq %rbx, (%r15)
movq %rbx, %rax
addq $0x10, %rax
movq 0x10(%r14), %rcx
movq 0x8(%rcx), %rcx
movq %rbx, (%rcx)
movq 0x10(%r14), %rcx
movq %rax, 0x8(%rcx)
incl 0x8(%r14)
jmp 0x1c53a
cmpb $0x0, 0xd(%r14)
je 0x1c536
movq 0x8(%rbx), %rdi
callq 0xc0d0
movq %rbp, 0x8(%rbx)
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x52
jmp 0x1c552
pushq $0x59
popq %rcx
movq 0x36a9e(%rip), %rax     # 0x52ff8
movq (%rax), %rdi
leaq 0x20b7c(%rip), %rsi     # 0x3d0e0
leaq 0x2353f(%rip), %rdx     # 0x3faaa
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xc8d0
movq %r13, %rdi
callq 0xc0d0
jmp 0x1c53a
movq 0x36a73(%rip), %rax     # 0x52ff8
movq (%rax), %rdi
leaq 0x20b51(%rip), %rsi     # 0x3d0e0
leaq 0x23514(%rip), %rdx     # 0x3faaa
xorl %ebx, %ebx
movl $0x81, %ecx
xorl %eax, %eax
callq 0xc8d0
jmp 0x1c53a