// File: /workspace/github_c_makefile_star2_O3/xiaowenhuyuehu[P]remote_access/src/utility/hash_table/hash_table.c
// Function: ht_add_lockless
int ht_add_lockless(void *ht, const void *key, unsigned int len_key, const void *val, unsigned int size_val)
{
    ht_item_t *p_item = NULL;
    ht_item_t *p_tmp = NULL;
    ht_item_t *new_tb = NULL;

    if (!ht || !key || !val || len_key <= 0 || size_val <= 0) {
        return -1;
    }

    p_item = _ht_find_lockless(ht, key, len_key);

    if (!p_item->key) {
        p_item->key = malloc(len_key);
        if (!p_item->key) {
            return -1;
        }
        memcpy(p_item->key, key, len_key);
        p_item->val = malloc(size_val);
        if (!p_item->val) {
            free(p_item->key);
            p_item->key = NULL;
            return -1;
        }
        memcpy(p_item->val, val, size_val);
        p_item->size_val = size_val;
        return 0;
    }

    //conflict: add to it's next item in the list.
    p_tmp = p_item;
    while (p_tmp) {
        if (NULL != p_tmp->key && !memcmp(p_tmp->key, key, len_key)) {
            free(p_tmp->val);
            p_tmp->val = NULL;
            p_tmp->val = malloc(size_val);
            if (!p_item->val) {
                free(p_tmp);
                return -1;
            }
            p_item->size_val = size_val;
            memcpy(p_item->val, val, size_val);
            return 0;//repeated key , just update it
        }
        p_item = p_tmp;
        p_tmp = p_tmp->next;
    }

    new_tb = (ht_item_t *)malloc(sizeof(ht_item_t));
    if (!new_tb) {
        return -1;
    }
    memset(new_tb, 0, sizeof(ht_item_t));
    new_tb->key = malloc(len_key);
    if (!new_tb->key) {
        free(new_tb);
        return -1;
    }
    memcpy(new_tb->key, key, len_key);
    new_tb->val = malloc(size_val);
    if (!new_tb->val) {
        free(new_tb->key);
        free(new_tb);
        return -1;
    }
    new_tb->size_val = size_val;
    memcpy(new_tb->val, val, size_val);
    p_item->next = new_tb;

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %r8d, %r8d
je 0x3f72
movl %edx, %r14d
testl %edx, %edx
je 0x3f72
testq %rdi, %rdi
je 0x3f72
movq %rsi, %r15
testq %rsi, %rsi
je 0x3f72
testq %rcx, %rcx
je 0x3f72
movl %r8d, %ebp
movq %rcx, 0x8(%rsp)
movq %r15, %rsi
movl %r14d, %edx
callq 0x3cc3
movq %rax, %r12
movq (%rax), %rdi
movl %r14d, %r13d
testq %rdi, %rdi
je 0x3eb8
movq %r12, %rbx
movq %r12, %r14
testq %rdi, %rdi
je 0x3e27
movq %r15, %rsi
movq %r13, %rdx
callq 0x2350
testl %eax, %eax
je 0x3f0a
movq 0x18(%r14), %r12
testq %r12, %r12
je 0x3e39
movq (%r12), %rdi
movq %r14, %rbx
jmp 0x3e0c
movl $0x1, %edi
movl $0x20, %esi
callq 0x24a0
testq %rax, %rax
je 0x3f47
movq %rax, %r12
movq %r13, %rdi
callq 0x2150
movq %rax, (%r12)
testq %rax, %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
je 0x3f6a
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2580
movl %ebp, %r15d
movq %r15, %rdi
callq 0x2150
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x3f60
movl %ebp, 0x10(%r12)
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0x2580
movq %r12, 0x18(%r14)
jmp 0x3f3b
movq %r13, %rdi
callq 0x2150
movq %rax, (%r12)
testq %rax, %rax
je 0x3f72
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2580
movl %ebp, %r15d
movq %r15, %rdi
callq 0x2150
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x3f4e
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0x2580
movl %ebp, 0x10(%r12)
jmp 0x3f3b
movq 0x8(%r14), %rdi
callq 0x2250
movl %ebp, %r15d
movq %r15, %rdi
callq 0x2150
movq %rax, 0x8(%r14)
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x3f3f
movl %ebp, 0x10(%rbx)
movq 0x8(%rsp), %rsi
movq %r15, %rdx
callq 0x2580
xorl %ebx, %ebx
jmp 0x3f72
movq %r14, %rdi
callq 0x2250
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x3f72
movq %r14, %rdi
callq 0x2250
movq $0x0, (%r12)
jmp 0x3f72
movq 0x10(%rsp), %rdi
callq 0x2250
movq %r12, %rdi
callq 0x2250
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq