// File: /workspace/github_c_makefile_star2_O1/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
pushq %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %r8d, %r8d
je 0x4044
movl %edx, %r14d
testl %edx, %edx
je 0x4044
testq %rdi, %rdi
je 0x4044
movq %rsi, %r15
testq %rsi, %rsi
je 0x4044
testq %rcx, %rcx
je 0x4044
movl %r8d, %ebp
movq %rcx, (%rsp)
movq %r15, %rsi
movl %r14d, %edx
callq 0x3dc5
movq %rax, %r12
cmpq $0x0, (%rax)
movl %r14d, %r13d
je 0x3f8c
movq %r12, %r14
movq %r12, %rbx
movq (%r12), %rdi
testq %rdi, %rdi
je 0x3f04
movq %r15, %rsi
movq %r13, %rdx
callq 0x2350
testl %eax, %eax
je 0x3fda
movq 0x18(%rbx), %r12
movq %rbx, %r14
testq %r12, %r12
jne 0x3ee5
movl $0x20, %edi
callq 0x2150
testq %rax, %rax
je 0x403f
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %r13, %rdi
callq 0x2150
movq %rax, (%r12)
testq %rax, %rax
je 0x4037
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2570
movl %ebp, %r15d
movq %r15, %rdi
callq 0x2150
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x402f
movl %ebp, 0x10(%r12)
movq %rax, %rdi
movq (%rsp), %rsi
movq %r15, %rdx
callq 0x2570
movq %r12, 0x18(%rbx)
jmp 0x4013
movq %r13, %rdi
callq 0x2150
movq %rax, (%r12)
testq %rax, %rax
je 0x4044
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2570
movl %ebp, %r14d
movq %r14, %rdi
callq 0x2150
movq %rax, 0x8(%r12)
testq %rax, %rax
je 0x401c
movq %rax, %rdi
movq (%rsp), %rsi
movq %r14, %rdx
callq 0x2570
movl %ebp, 0x10(%r12)
jmp 0x4013
movq 0x8(%rbx), %rdi
callq 0x2250
movq $0x0, 0x8(%rbx)
movl %ebp, %r15d
movq %r15, %rdi
callq 0x2150
movq %rax, 0x8(%rbx)
movq 0x8(%r14), %rdi
testq %rdi, %rdi
je 0x4017
movl %ebp, 0x10(%r14)
movq (%rsp), %rsi
movq %r15, %rdx
callq 0x2570
xorl %ebx, %ebx
jmp 0x4044
movq %rbx, %rdi
jmp 0x403a
movq (%r12), %rdi
callq 0x2250
movq $0x0, (%r12)
jmp 0x4044
movq %r14, %rdi
callq 0x2250
movq %r12, %rdi
callq 0x2250
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq