// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x6f83(%rip), %rdi      # 0xa2e8
callq 0x2400
movzbl 0x1(%rbx), %esi
shll $0x3, %esi
movl $0x1, %edi
callq 0x21f0
movq %rax, %r15
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
movq %rax, %rcx
callq 0x3143
testq %rax, %rax
je 0x3414
movq %rax, %r12
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3414
cmpq %r14, (%rdi)
jne 0x33aa
xorl %ecx, %ecx
jmp 0x33c1
movq %rdi, %rax
movq 0x10(%rax), %rdi
testq %rdi, %rdi
je 0x3414
movq %rax, %rcx
movq %rdi, %rax
cmpq %r14, (%rdi)
jne 0x33ad
leaq 0x10(%rcx), %rax
testq %rcx, %rcx
movq 0x10(%rdi), %rcx
cmoveq %r12, %rax
movq %rcx, (%rax)
movq 0x18(%rdi), %rcx
movq 0x20(%rdi), %rax
testq %rcx, %rcx
je 0x33e4
movq %rax, 0x20(%rcx)
testq %rax, %rax
je 0x33ed
movq %rcx, 0x18(%rax)
cmpq %rdi, 0x10(%rbx)
jne 0x33f7
movq %rax, 0x10(%rbx)
cmpq %rdi, 0x18(%rbx)
jne 0x3405
movq 0x18(%rdi), %rax
movq %rax, 0x18(%rbx)
decl 0x4(%rbx)
callq 0x2340
cmpq $0x0, (%r12)
je 0x3436
leaq 0x6ecd(%rip), %rdi      # 0xa2e8
callq 0x2230
movq %r15, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2340
movb 0x1(%rbx), %al
addb $-0x2, %al
movzbl %al, %r12d
movl %r12d, %eax
shll $0x3, %eax
movq (%r15,%rax), %rdi
movb $0x38, %cl
subb %al, %cl
movq %r14, %r13
shlq %cl, %r13
shrq %cl, %r13
movl %eax, %ecx
shrq %cl, %r13
xorl %eax, %eax
cmpq $0x0, (%rdi,%rax,8)
jne 0x3414
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x345d
callq 0x2340
movl %r12d, %eax
movzbl %r13b, %ecx
subb $0x1, %al
jb 0x348e
movq -0x8(%r15,%r12,8), %rdx
movq $0x0, (%rdx,%rcx,8)
jmp 0x343b
movq 0x8(%rbx), %rax
movq $0x0, (%rax,%rcx,8)
jmp 0x3414