// File: /workspace/github_c_makefile_star2_O0/TimeToogo[P]ff-proxy/src/hash_table.c
// Function: ff_hash_table_remove_item
void ff_hash_table_remove_item(struct ff_hash_table *hash_table, uint64_t item_id)
{
    pthread_mutex_lock(&ff_hash_table_data_mutex);

    union ff_hash_table_bucket **bucket_list = calloc(1, sizeof(union ff_hash_table_bucket *) * hash_table->bucket_levels);
    union ff_hash_table_bucket *buckets = ff_hash_table_get_or_create_bucket(hash_table, item_id, true, bucket_list);

    if (buckets == NULL || buckets->nodes == NULL)
    {
        goto cleanup;
    }

    // Find last node or matching in chain
    struct ff_hash_table_node *prev_node = NULL;
    struct ff_hash_table_node *node = buckets->nodes;

    while (node != NULL)
    {
        if (node->item_id == item_id)
        {
            // Remove from hash node linked list
            if (prev_node != NULL)
            {
                prev_node->next = node->next;
            }
            else
            {
                buckets->nodes = node->next;
            }

            // Remove from global linked list
            if (node->prev_in_list != NULL)
            {
                node->prev_in_list->next_in_list = node->next_in_list;
            }

            if (node->next_in_list != NULL)
            {
                node->next_in_list->prev_in_list = node->prev_in_list;
            }

            if (hash_table->linked_list == node)
            {
                hash_table->linked_list = node->next_in_list;
            }

            if (hash_table->linked_list_last == node)
            {
                hash_table->linked_list_last = node->prev_in_list;
            }

            hash_table->length--;
            FREE(node);
            break;
        }

        prev_node = node;
        node = node->next;
    }

    if (buckets->nodes == NULL)
    {
        // Walk back up tree clearing unused buckets
        for (uint8_t level = hash_table->bucket_levels - 2; ; level--)
        {
            bool should_free = true;
            union ff_hash_table_bucket *bucket_level = bucket_list[level];
            uint8_t hash = FF_HASH_GET_FOR_LEVEL(item_id, level);

            for (int i = 0; i < FF_BUCKET_POOL_LENGTH; i++)
            {
                if (bucket_level[i].buckets != NULL)
                {
                    should_free = false;
                    break;
                }
            }

            if (!should_free)
            {
                break;
            }

            FREE(bucket_level);
            if (level == 0)
            {
                hash_table->buckets[hash].buckets = NULL;
                break;
            }
            else
            {
                bucket_list[level - 1][hash].buckets = NULL;
            }
        }
    }

cleanup:
    pthread_mutex_unlock(&ff_hash_table_data_mutex);
    FREE(bucket_list);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x7a11(%rip), %rdi      # 0xb318
callq 0x2420
movq -0x8(%rbp), %rax
movzbl 0x1(%rax), %eax
movl %eax, %esi
shlq $0x3, %rsi
movl $0x1, %edi
callq 0x2200
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rcx
movl $0x1, %edx
callq 0x3590
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x3953
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x3958
jmp 0x3b65
movq $0x0, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3a62
movq -0x30(%rbp), %rax
movq (%rax), %rax
cmpq -0x10(%rbp), %rax
jne 0x3a49
cmpq $0x0, -0x28(%rbp)
je 0x39a0
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x39af
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x39ce
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x39ed
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
cmpq -0x30(%rbp), %rax
jne 0x3a0b
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x30(%rbp), %rax
jne 0x3a29
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x4(%rax)
movq -0x30(%rbp), %rdi
callq 0x2350
movq $0x0, -0x30(%rbp)
jmp 0x3a62
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x396b
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x3b63
movq -0x8(%rbp), %rax
movzbl 0x1(%rax), %eax
subl $0x2, %eax
movb %al, -0x31(%rbp)
movb $0x1, -0x32(%rbp)
movq -0x18(%rbp), %rax
movzbl -0x31(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movzbl -0x31(%rbp), %edx
addl $0x1, %edx
shll $0x3, %edx
movl $0x40, %ecx
subl %edx, %ecx
movl %ecx, %ecx
movq $-0x1, %rdx
shrq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
movzbl -0x31(%rbp), %ecx
shll $0x3, %ecx
movl %ecx, %ecx
shrq %cl, %rax
movb %al, -0x41(%rbp)
movl $0x0, -0x48(%rbp)
cmpl $0x100, -0x48(%rbp)     # imm = 0x100
jge 0x3afa
movq -0x40(%rbp), %rax
movslq -0x48(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x3aed
movb $0x0, -0x32(%rbp)
jmp 0x3afa
jmp 0x3aef
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x3acf
testb $0x1, -0x32(%rbp)
jne 0x3b02
jmp 0x3b61
jmp 0x3b04
movq -0x40(%rbp), %rdi
callq 0x2350
movq $0x0, -0x40(%rbp)
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
jne 0x3b34
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movzbl -0x41(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
jmp 0x3b61
movq -0x18(%rbp), %rax
movzbl -0x31(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movzbl -0x41(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
jmp 0x3b54
movb -0x31(%rbp), %al
addb $-0x1, %al
movb %al, -0x31(%rbp)
jmp 0x3a7e
jmp 0x3b63
jmp 0x3b65
leaq 0x77ac(%rip), %rdi      # 0xb318
callq 0x2240
movq -0x18(%rbp), %rdi
callq 0x2350
movq $0x0, -0x18(%rbp)
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)