// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/movepick.cpp
// Function: MovePicker::get_next_move()
Move MovePicker::get_next_move() {

  Move move;

  while (true)
  {
      while (curMove == lastMove)
          go_next_phase();

      switch (phase) {

      case PH_TT_MOVES:
          move = (curMove++)->move;
          
          if (   move != MOVE_NONE
              && pos.move_is_legal(move, pinned))
              return move;
          break;

      case PH_GOOD_CAPTURES:
          move = pick_best(curMove++, lastMove).move;
          if (   move != ttMoves[0].move
              && move != ttMoves[1].move
              && pos.pl_move_is_legal(move, pinned))
          {
              // Check for a non negative SEE now
              int seeValue = pos.see_sign(move);
              if (seeValue >= badCaptureThreshold)
                  return move;

              // Losing capture, move it to the tail of the array, note
              // that move has now been already checked for legality.
              (--badCaptures)->move = move;
              badCaptures->score = seeValue;
          }
          break;

      case PH_KILLERS:
          move = (curMove++)->move;
          if (   move != MOVE_NONE
              && pos.move_is_legal(move, pinned)
              && move != ttMoves[0].move
              && move != ttMoves[1].move
              && !pos.move_is_capture(move))
              return move;
          break;

      case PH_NONCAPTURES:
          // Sort negative scored moves only when we get there
          if (curMove == lastGoodNonCapture)
              insertion_sort<MoveStack>(lastGoodNonCapture, lastMove);

          move = (curMove++)->move;
          if (   move != ttMoves[0].move
              && move != ttMoves[1].move
              && move != killers[0].move
              && move != killers[1].move
              && pos.pl_move_is_legal(move, pinned))
              return move;
          break;

      case PH_BAD_CAPTURES:
          move = pick_best(curMove++, lastMove).move;
          return move;

      case PH_EVASIONS:
      case PH_QCAPTURES:
          move = pick_best(curMove++, lastMove).move;
          if (move != ttMoves[0].move && pos.pl_move_is_legal(move, pinned)) {
              return move;
          }
          break;

      case PH_QCHECKS:
          move = (curMove++)->move;
          if (   move != ttMoves[0].move
              && pos.pl_move_is_legal(move, pinned))
              return move;
          break;

      case PH_STOP:
          return MOVE_NONE;

      default:
          assert(false);
          break;
      }
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
leaq 0x13339(%rip), %r15     # 0x29864
movq 0x48(%rbx), %rdi
movq 0x50(%rbx), %rsi
cmpq %rsi, %rdi
je 0x16565
movl 0x3c(%rbx), %eax
cmpq $0x8, %rax
ja 0x1652b
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
leaq 0x8(%rdi), %rax
movq %rax, 0x48(%rbx)
callq 0x16758
movq %rax, %r14
cmpl %r14d, 0x18(%rbx)
je 0x1652b
jmp 0x16625
movq %rbx, %rdi
callq 0x16200
jmp 0x1652b
leaq 0x8(%rdi), %rax
movq %rax, 0x48(%rbx)
movl (%rdi), %r14d
cmpl 0x18(%rbx), %r14d
je 0x1652b
jmp 0x16625
leaq 0x8(%rdi), %rax
movq %rax, 0x48(%rbx)
movl (%rdi), %r14d
testq %r14, %r14
je 0x1652b
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x18b96
testb %al, %al
je 0x1652b
cmpl 0x18(%rbx), %r14d
je 0x1652b
cmpl 0x20(%rbx), %r14d
je 0x1652b
testl $0x18000, %r14d         # imm = 0x18000
je 0x166c9
movl %r14d, %eax
notl %eax
testw $0xc000, %ax            # imm = 0xC000
jne 0x1652b
jmp 0x166f3
movq 0x58(%rbx), %rax
cmpq %rax, %rdi
jne 0x165f2
movq %rax, %rdi
callq 0x16782
movq 0x48(%rbx), %rdi
leaq 0x8(%rdi), %rax
movq %rax, 0x48(%rbx)
movl (%rdi), %r14d
cmpl 0x18(%rbx), %r14d
je 0x1652b
cmpl 0x20(%rbx), %r14d
je 0x1652b
cmpl 0x28(%rbx), %r14d
je 0x1652b
cmpl 0x30(%rbx), %r14d
je 0x1652b
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x187c6
jmp 0x16659
leaq 0x8(%rdi), %rax
movq %rax, 0x48(%rbx)
movl (%rdi), %r14d
testl %r14d, %r14d
je 0x1652b
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x18b96
testb %al, %al
je 0x1652b
jmp 0x166f3
leaq 0x8(%rdi), %rax
movq %rax, 0x48(%rbx)
callq 0x16758
movq %rax, %r14
cmpl %r14d, 0x18(%rbx)
je 0x1652b
cmpl %r14d, 0x20(%rbx)
je 0x1652b
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x187c6
testb %al, %al
je 0x1652b
movq (%rbx), %rdi
movl %r14d, %esi
callq 0x1a668
cmpl 0x38(%rbx), %eax
jge 0x166f3
movq 0x60(%rbx), %rcx
leaq -0x8(%rcx), %rdx
movq %rdx, 0x60(%rbx)
movl %r14d, -0x8(%rcx)
movl %eax, -0x4(%rcx)
jmp 0x1652b
movq (%rbx), %rax
movl %r14d, %ecx
andl $0x3f, %ecx
cmpl $0x10, (%rax,%rcx,4)
jne 0x1652b
jmp 0x166f3
xorl %r14d, %r14d
jmp 0x166f3
leaq 0x8(%rdi), %rax
movq %rax, 0x48(%rbx)
callq 0x16758
movq %rax, %r14
movl %r14d, %eax
popq %rbx
popq %r14
popq %r15
retq