// File: lyqscmy[P]array_lock_free_queue/./array_lock_free_queue.h
// Function: ArrayLockFreeQueue<int, 3ul>::push(int const&)
bool push(const T &item) {
    size_t currentReadIndex = 0;
    size_t currentWriteIndex = 0;
    size_t nextWriteInex = 0;

    // reserve space to commit
    do {
      currentWriteIndex = writeIndex.load();
      currentReadIndex = readIndex.load();
      nextWriteInex = (currentWriteIndex + 1) & sizeMask;
      if (nextWriteInex == currentReadIndex) {
        // queue full
        return false;
      }

      // cas fail means other producer take this place, this thread need try
      // again.
    } while (!std::atomic_compare_exchange_weak(&writeIndex, &currentWriteIndex,
                                                nextWriteInex));

    // commit data to the reserve place
    data[currentWriteIndex] = item;

    while (!std::atomic_compare_exchange_weak(
        &maximumReadIndex, &currentWriteIndex, nextWriteInex)) {
      // the commit step should be order between producers, producer 1 ->
      // producer 2 -> producer 3. so when producers more than the cores, yield
      // is needed to avoid spinlock.
       std::this_thread::yield();
    }

    return true;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x40(%rbp)
movq %rsi, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x68(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x8(%rbp)
movl $0x5, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x78(%rbp)
movl -0xc(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x2fa0
movl %eax, -0x6c(%rbp)
jmp 0x2a00
movl -0x6c(%rbp), %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x7c(%rbp)
decl %eax
subl $0x2, %eax
jb 0x2a2c
jmp 0x2a15
movl -0x7c(%rbp), %eax
subl $0x5, %eax
je 0x2a39
jmp 0x2a1f
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x2a4e
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x2a4e
movq -0x78(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x2a4e
movq %rax, %rdi
callq 0x2fc0
movq -0x68(%rbp), %rax
movq -0x18(%rbp), %rcx
movq %rcx, -0x58(%rbp)
addq $0x20, %rax
movq %rax, -0x20(%rbp)
movl $0x5, -0x24(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x24(%rbp), %edi
movl $0xffff, %esi           # imm = 0xFFFF
callq 0x2fa0
movl %eax, -0x80(%rbp)
jmp 0x2a86
movl -0x80(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x8c(%rbp)
decl %eax
subl $0x2, %eax
jb 0x2abb
jmp 0x2a9e
movl -0x8c(%rbp), %eax
subl $0x5, %eax
je 0x2acb
jmp 0x2aab
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x2ae3
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x2ae3
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x2ae3
movq %rax, %rdi
callq 0x2fc0
movq -0x68(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x58(%rbp), %rax
addq $0x1, %rax
andq 0x10(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x50(%rbp), %rax
jne 0x2b0f
movb $0x0, -0x31(%rbp)
jmp 0x2b69
jmp 0x2b11
movq -0x68(%rbp), %rdi
addq $0x18, %rdi
movq -0x60(%rbp), %rdx
leaq -0x58(%rbp), %rsi
callq 0x2f40
xorb $-0x1, %al
testb $0x1, %al
jne 0x29d3
movq -0x68(%rbp), %rax
movq -0x48(%rbp), %rcx
movl (%rcx), %edx
movq -0x58(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x68(%rbp), %rdi
addq $0x28, %rdi
movq -0x60(%rbp), %rdx
leaq -0x58(%rbp), %rsi
callq 0x2f40
xorb $-0x1, %al
testb $0x1, %al
jne 0x2b5e
jmp 0x2b65
callq 0x2f80
jmp 0x2b41
movb $0x1, -0x31(%rbp)
movb -0x31(%rbp), %al
andb $0x1, %al
addq $0x90, %rsp
popq %rbp
retq
nopw (%rax,%rax)