// File: bhhbazinga[P]LockFreeHashTable/./lockfree_hashtable.h
// Function: LockFreeHashTable<int, int, std::hash<int>>::DeleteNode(LockFreeHashTable<int, int, std::hash<int>>::DummyNode*, LockFreeHashTable<int, int, std::hash<int>>::Node*)
bool LockFreeHashTable<K, V, Hash>::DeleteNode(DummyNode* head,
                                               Node* delete_node) {
  Node* prev;
  Node* cur;
  Node* next;
  HazardPointer prev_hp, cur_hp;
  do {
    do {
      if (!SearchNode(head, delete_node, &prev, &cur, prev_hp, cur_hp)) {
        return false;
      }
      next = cur->get_next();
    } while (is_marked_reference(next));
    // Logically delete cur by marking cur->next.
  } while (!cur->next.compare_exchange_weak(next, get_marked_reference(next),
                                            std::memory_order_release,
                                            std::memory_order_relaxed));

  if (prev->next.compare_exchange_strong(cur, next,
                                         std::memory_order_release)) {
    size_.fetch_sub(1, std::memory_order_relaxed);
    auto& reclaimer = TableReclaimer<K, V>::GetInstance();
    reclaimer.ReclaimLater(cur, LockFreeHashTable<K, V, Hash>::OnDeleteNode);
    reclaimer.ReclaimNoHazardPointer();
  } else {
    prev_hp.UnMark();
    cur_hp.UnMark();
    SearchNode(head, delete_node, &prev, &cur, prev_hp, cur_hp);
  }

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0xc0(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xb020
leaq -0x80(%rbp), %rdi
callq 0xb020
jmp 0xe476
jmp 0xe478
jmp 0xe47a
movq -0xc0(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rsp, %rax
leaq -0x80(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x50(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x70(%rbp), %r9
callq 0xb040
movb %al, -0xc1(%rbp)
jmp 0xe4ac
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0xe4f9
jmp 0xe4b8
movb $0x0, -0x29(%rbp)
movl $0x1, -0x90(%rbp)
jmp 0xe763
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0xe78c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0xe783
movq -0x58(%rbp), %rdi
callq 0xbab0
movq %rax, -0x60(%rbp)
movq -0xc0(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0xbb90
testb $0x1, %al
jne 0xe47a
jmp 0xe520
movq -0xc0(%rbp), %rdi
movq -0x58(%rbp), %rax
addq $0x18, %rax
movq %rax, -0xd8(%rbp)
movq -0x60(%rbp), %rsi
callq 0xe7b0
movq %rax, -0xd0(%rbp)
jmp 0xe548
movq -0xd0(%rbp), %rdx
movq -0xd8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movl $0x3, %ecx
xorl %r8d, %r8d
callq 0xb490
xorb $-0x1, %al
testb $0x1, %al
jne 0xe478
movq -0x50(%rbp), %rdi
addq $0x18, %rdi
movq -0x60(%rbp), %rdx
leaq -0x58(%rbp), %rsi
movl $0x3, %ecx
callq 0xbbb0
testb $0x1, %al
jne 0xe594
jmp 0xe711
movq -0xc0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
movq $0x1, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq %rcx, -0x20(%rbp)
decl %eax
movl %eax, %ecx
movq %rcx, -0xe0(%rbp)
subl $0x4, %eax
ja 0xe5ef
movq -0xe0(%rbp), %rax
leaq 0x2b62(%rip), %rcx      # 0x11148
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xe8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0xe66a
movq -0xe8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0xe66a
movq -0xe8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0xe66a
movq -0xe8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
jmp 0xe66a
movq -0xe8(%rbp), %rcx
movq -0x20(%rbp), %rax
negq %rax
lock
xaddq %rax, (%rcx)
movq %rax, -0x28(%rbp)
callq 0xba40
movq %rax, -0xf0(%rbp)
jmp 0xe678
movq -0xf0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0x24a6(%rip), %rsi     # 0xc200
leaq -0xb8(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0xc230
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
callq 0xc180
jmp 0xe6d5
leaq -0xb8(%rbp), %rdi
callq 0x5750
movq -0x98(%rbp), %rdi
callq 0x2be0
jmp 0xe6ef
jmp 0xe755
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x5750
jmp 0xe783
leaq -0x70(%rbp), %rdi
callq 0xbae0
jmp 0xe71c
leaq -0x80(%rbp), %rdi
callq 0xbae0
jmp 0xe727
movq -0xc0(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %rsp, %rax
leaq -0x80(%rbp), %rcx
movq %rcx, (%rax)
leaq -0x50(%rbp), %rcx
leaq -0x58(%rbp), %r8
leaq -0x70(%rbp), %r9
callq 0xb040
jmp 0xe753
jmp 0xe755
movb $0x1, -0x29(%rbp)
movl $0x1, -0x90(%rbp)
leaq -0x80(%rbp), %rdi
callq 0xba10
leaq -0x70(%rbp), %rdi
callq 0xba10
movb -0x29(%rbp), %al
andb $0x1, %al
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
leaq -0x80(%rbp), %rdi
callq 0xba10
leaq -0x70(%rbp), %rdi
callq 0xba10
movq -0x88(%rbp), %rdi
callq 0x2230
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)