// File: /workspace/github_c_makefile_star2_O0/ammarfaizi2[P]hashtable/src/hashtable.c
// Function: insert_item_ht
void
insert_item_ht(HashTable *ht, const void *key, size_t key_len,
               const void *data, size_t data_len)
{
  char   *heap;
  size_t hash;
  size_t index;
  Item   *item;
  Bucket *tail;

  assert(ht->hash_f != NULL);

  hash  = ht->hash_f(key, key_len);
  index = hash % ht->size;
  heap  = (char *)malloc(sizeof(Item) + key_len + data_len);
  item  = (Item *)heap;

  item->key       = (void *)&(heap[sizeof(Item)]);
  item->key_len   = key_len;
  item->data      = (void *)&(heap[sizeof(Item) + key_len]);
  item->data_len  = data_len;

  memcpy(item->key, key, key_len);
  memcpy(item->data, data, data_len);

  tail = ht->tail_bucket[index];

  if (unlikely(tail != NULL)) {
    /* We have hash collision. */
    Bucket *key_col;

    key_col = find_bucket_slot(&(ht->bucket[index]), key, key_len);
    if (key_col == NULL) {
      tail->next = (Bucket *)malloc(sizeof(Bucket));
      tail       = tail->next;
    } else {
      /* Don't update tail, we only replace item. */
      free(key_col->item);
      key_col->item = item;
      return;
    }
  } else {
    tail       = &(ht->bucket[index]);
    assert(tail->next == NULL);
  }

  tail->item = item;
  tail->next = NULL;
  ht->tail_bucket[index] = tail;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %r8, 0x30(%rsp)
movq 0x50(%rsp), %rax
cmpq $0x0, 0x20(%rax)
je 0x142b
jmp 0x144a
leaq 0xbd2(%rip), %rdi       # 0x2004
leaq 0xbde(%rip), %rsi       # 0x2017
movl $0x6c, %edx
leaq 0xbe2(%rip), %rcx       # 0x2027
callq 0x1070
movq 0x50(%rsp), %rax
movq 0x20(%rax), %rax
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq *%rax
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
movq 0x50(%rsp), %rcx
xorl %edx, %edx
divq 0x10(%rcx)
movq %rdx, 0x18(%rsp)
movq 0x40(%rsp), %rdi
addq $0x20, %rdi
addq 0x30(%rsp), %rdi
callq 0x10c0
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rcx
addq $0x20, %rcx
movq 0x10(%rsp), %rax
movq %rcx, (%rax)
movq 0x40(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rcx
movq 0x40(%rsp), %rax
addq $0x20, %rax
addq %rax, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x30(%rsp), %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x10b0
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
callq 0x10b0
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
setne %al
andb $0x1, %al
movzbl %al, %eax
cltq
cmpq $0x0, %rax
je 0x15ae
movq 0x50(%rsp), %rax
movq (%rax), %rdi
movq 0x18(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdx
callq 0x1630
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
jne 0x1592
movl $0x10, %edi
callq 0x10c0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0x15ac
movq (%rsp), %rax
movq (%rax), %rdi
callq 0x1030
movq 0x10(%rsp), %rcx
movq (%rsp), %rax
movq %rcx, (%rax)
jmp 0x1627
jmp 0x15f6
movq 0x50(%rsp), %rax
movq (%rax), %rax
movq 0x18(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x15d5
jmp 0x15f4
leaq 0xa98(%rip), %rdi       # 0x2074
leaq 0xa34(%rip), %rsi       # 0x2017
movl $0x8d, %edx
leaq 0xa38(%rip), %rcx       # 0x2027
callq 0x1070
jmp 0x15f6
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
movq 0x8(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0x8(%rsp), %rdx
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
addq $0x58, %rsp
retq
nopl (%rax)