// File: /workspace/github_c_makefile_star2_O2/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
pushq $-0x1
popq %rbx
testl %r8d, %r8d
je 0x3d6d
movl %edx, %r14d
testl %edx, %edx
je 0x3d6d
testq %rdi, %rdi
je 0x3d6d
movq %rsi, %r12
testq %rsi, %rsi
je 0x3d6d
testq %rcx, %rcx
je 0x3d6d
movl %r8d, %ebp
movq %rcx, 0x8(%rsp)
movq %r12, %rsi
movl %r14d, %edx
callq 0x3b1e
movq %rax, %r15
cmpq $0x0, (%rax)
movl %r14d, %r13d
je 0x3cc8
movq %r15, %r14
testq %r14, %r14
je 0x3c55
movq (%r14), %rdi
testq %rdi, %rdi
je 0x3c4c
movq %r12, %rsi
movq %r13, %rdx
callq 0x2350
testl %eax, %eax
je 0x3d17
movq %r14, %r15
movq 0x18(%r14), %r14
jmp 0x3c2c
pushq $0x1
popq %rdi
pushq $0x20
popq %rsi
callq 0x24a0
testq %rax, %rax
je 0x3d6d
movq %rax, %r14
movq %r13, %rdi
callq 0x2150
movq %rax, (%r14)
testq %rax, %rax
je 0x3d65
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2580
movl %ebp, %r12d
movq %r12, %rdi
callq 0x2150
movq %rax, 0x8(%r14)
testq %rax, %rax
je 0x3d5b
movl %ebp, 0x10(%r14)
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
callq 0x2580
movq %r14, 0x18(%r15)
jmp 0x3d49
movq %r13, %rdi
callq 0x2150
movq %rax, (%r15)
testq %rax, %rax
je 0x3d6d
movq %rax, %r14
movq %rax, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x2580
movl %ebp, %r12d
movq %r12, %rdi
callq 0x2150
movq %rax, 0x8(%r15)
testq %rax, %rax
je 0x3d4d
movq %rax, %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
callq 0x2580
movl %ebp, 0x10(%r15)
jmp 0x3d49
movq 0x8(%r14), %rdi
callq 0x2250
movl %ebp, %r12d
movq %r12, %rdi
callq 0x2150
movq %rax, 0x8(%r14)
movq 0x8(%r15), %rdi
testq %rdi, %rdi
je 0x3d65
movl %ebp, 0x10(%r15)
movq 0x8(%rsp), %rsi
movq %r12, %rdx
callq 0x2580
xorl %ebx, %ebx
jmp 0x3d6d
movq %r14, %rdi
callq 0x2250
andq $0x0, (%r15)
jmp 0x3d6d
movq 0x10(%rsp), %rdi
callq 0x2250
movq %r14, %rdi
callq 0x2250
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq