// File: bhhbazinga[P]LockFreeHashTable/./HazardPointer/reclaimer.h
// Function: Reclaimer::TryAcquireHazardPointer()
void Reclaimer::TryAcquireHazardPointer() {
  std::atomic<InternalHazardPointer*>& head = global_hp_list_.head;
  InternalHazardPointer* p = head.load(std::memory_order_acquire);
  InternalHazardPointer* hp = nullptr;
  do {
    // Try to get the idle hazard pointer.
    if (!p->flag.test_and_set()) {
      hp = p;
      break;
    }
    p = p->next.load(std::memory_order_acquire);
  } while (p != nullptr);

  if (nullptr == hp) {
    // No idle hazard pointer, allocate new one.
    InternalHazardPointer* new_head = new InternalHazardPointer();
    new_head->flag.test_and_set();
    hp = new_head;
    global_hp_list_.size.fetch_add(1, std::memory_order_release);
    InternalHazardPointer* old_head = head.load(std::memory_order_acquire);
    do {
      new_head->next = old_head;
    } while (!head.compare_exchange_weak(old_head, new_head,
                                         std::memory_order_release,
                                         std::memory_order_relaxed));
  }
  hp_list_.push_back(hp);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x60(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movl $0x2, %esi
callq 0x4300
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x5, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x24(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x88(%rbp)
subl $0x4, %eax
ja 0x292d
movq -0x88(%rbp), %rax
leaq 0xe6e0(%rip), %rcx      # 0x11004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x90(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0x91(%rbp)
jmp 0x298a
movq -0x90(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0x91(%rbp)
jmp 0x298a
movq -0x90(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0x91(%rbp)
jmp 0x298a
movq -0x90(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0x91(%rbp)
jmp 0x298a
movq -0x90(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0x91(%rbp)
movb -0x91(%rbp), %al
cmpb $0x0, %al
jne 0x299e
movq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x29bf
movq -0x50(%rbp), %rdi
addq $0x10, %rdi
movl $0x2, %esi
callq 0x4300
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x28e9
xorl %eax, %eax
cmpq -0x58(%rbp), %rax
jne 0x2bbc
movl $0x18, %edi
callq 0x2140
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xa0(%rbp)
callq 0x44d0
jmp 0x29e9
movq -0xa0(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x5, -0x34(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl -0x34(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0xa8(%rbp)
subl $0x4, %eax
ja 0x2a38
movq -0xa8(%rbp), %rax
leaq 0xe5e9(%rip), %rcx      # 0x11018
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x2a95
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x2a95
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x2a95
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
jmp 0x2a95
movq -0xb0(%rbp), %rcx
movb $0x1, %al
xchgb %al, (%rcx)
movb %al, -0xb1(%rbp)
movq -0x80(%rbp), %rax
movb -0xb1(%rbp), %cl
movq -0x60(%rbp), %rcx
movq %rcx, -0x58(%rbp)
movq 0x60(%rax), %rax
addq $0x8, %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0xc(%rbp)
movl $0x3, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl -0x10(%rbp), %eax
movl -0xc(%rbp), %ecx
movl %ecx, -0x14(%rbp)
decl %eax
movl %eax, %ecx
movq %rcx, -0xc0(%rbp)
subl $0x4, %eax
ja 0x2afc
movq -0xc0(%rbp), %rax
leaq 0xe539(%rip), %rcx      # 0x1102c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xc8(%rbp), %rcx
movl -0x14(%rbp), %eax
lock
xaddl %eax, (%rcx)
movl %eax, -0x18(%rbp)
jmp 0x2b59
movq -0xc8(%rbp), %rcx
movl -0x14(%rbp), %eax
lock
xaddl %eax, (%rcx)
movl %eax, -0x18(%rbp)
jmp 0x2b59
movq -0xc8(%rbp), %rcx
movl -0x14(%rbp), %eax
lock
xaddl %eax, (%rcx)
movl %eax, -0x18(%rbp)
jmp 0x2b59
movq -0xc8(%rbp), %rcx
movl -0x14(%rbp), %eax
lock
xaddl %eax, (%rcx)
movl %eax, -0x18(%rbp)
jmp 0x2b59
movq -0xc8(%rbp), %rcx
movl -0x14(%rbp), %eax
lock
xaddl %eax, (%rcx)
movl %eax, -0x18(%rbp)
movq -0x48(%rbp), %rdi
movl $0x2, %esi
callq 0x4300
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rsi
movq -0x60(%rbp), %rdi
addq $0x10, %rdi
callq 0x4510
movq -0x48(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq -0x78(%rbp), %rsi
movl $0x3, %ecx
xorl %r8d, %r8d
callq 0x4540
xorb $-0x1, %al
testb $0x1, %al
jne 0x2b6b
jmp 0x2bbc
movq -0xa0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
movl $0x18, %esi
callq 0x2150
jmp 0x2bd6
movq -0x80(%rbp), %rdi
addq $0x8, %rdi
leaq -0x58(%rbp), %rsi
callq 0x4ac0
addq $0xd0, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x2230
nop