// File: /workspace/github_c_makefile_star2_O2/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 0x609c(%rip), %rdi      # 0x92e8
callq 0x2400
movzbl 0x1(%rbx), %esi
shll $0x3, %esi
pushq $0x1
popq %rdi
callq 0x21f0
movq %rax, %r15
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rcx
callq 0x305b
testq %rax, %rax
je 0x32ec
movq %rax, %r12
movq (%rax), %rdi
testq %rdi, %rdi
je 0x32ec
xorl %eax, %eax
testq %rdi, %rdi
je 0x32ec
cmpq %r14, (%rdi)
je 0x3299
movq %rdi, %rax
movq 0x10(%rdi), %rdi
jmp 0x3286
leaq 0x10(%rax), %rcx
testq %rax, %rax
movq 0x10(%rdi), %rax
cmoveq %r12, %rcx
movq %rax, (%rcx)
movq 0x18(%rdi), %rcx
movq 0x20(%rdi), %rax
testq %rcx, %rcx
je 0x32bc
movq %rax, 0x20(%rcx)
testq %rax, %rax
je 0x32c5
movq %rcx, 0x18(%rax)
cmpq %rdi, 0x10(%rbx)
jne 0x32cf
movq %rax, 0x10(%rbx)
cmpq %rdi, 0x18(%rbx)
jne 0x32dd
movq 0x18(%rdi), %rax
movq %rax, 0x18(%rbx)
decl 0x4(%rbx)
callq 0x2340
cmpq $0x0, (%r12)
je 0x330e
leaq 0x5ff5(%rip), %rdi      # 0x92e8
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
movq (%r15,%r12,8), %rdi
leal (,%r12,8), %eax
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 $0x100, %rax            # imm = 0x100
je 0x334c
cmpq $0x0, (%rdi,%rax,8)
leaq 0x1(%rax), %rax
je 0x3337
jmp 0x32ec
callq 0x2340
movzbl %r13b, %eax
subq $0x1, %r12
jb 0x3366
movq (%r15,%r12,8), %rcx
andq $0x0, (%rcx,%rax,8)
jmp 0x3317
movq 0x8(%rbx), %rcx
andq $0x0, (%rcx,%rax,8)
jmp 0x32ec