// File: 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:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x342f5
movq 0x10(%rsp), %rcx
movq 0x48(%rcx), %rax
cmpq 0x50(%rcx), %rax
jne 0x34310
movq 0x10(%rsp), %rdi
callq 0x33d20
jmp 0x342f5
movq 0x10(%rsp), %rax
movl 0x3c(%rax), %eax
movq %rax, 0x8(%rsp)
subq $0x8, %rax
ja 0x3464a
movq 0x8(%rsp), %rax
leaq 0x267b1(%rip), %rcx     # 0x5aae4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rsp), %rcx
movq 0x48(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x48(%rcx)
movl (%rax), %eax
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x34385
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
movq 0x10(%rax), %rdx
callq 0x39740
testb $0x1, %al
jne 0x34378
jmp 0x34385
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x34651
jmp 0x3464c
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rdi
movq %rdi, %rcx
addq $0x8, %rcx
movq %rcx, 0x48(%rax)
movq 0x50(%rax), %rsi
callq 0x34850
movq 0x10(%rsp), %rcx
movq %rax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x18(%rcx), %eax
je 0x34441
movq 0x10(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x20(%rcx), %eax
je 0x34441
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
movq 0x10(%rax), %rdx
callq 0x38e80
testb $0x1, %al
jne 0x343eb
jmp 0x34441
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
callq 0x3d470
movq 0x10(%rsp), %rcx
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x38(%rcx), %eax
jl 0x3441b
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x34651
movq 0x10(%rsp), %rax
movl 0x34(%rsp), %edx
movq 0x60(%rax), %rcx
movq %rcx, %rsi
addq $-0x8, %rsi
movq %rsi, 0x60(%rax)
movl %edx, -0x8(%rcx)
movl 0x28(%rsp), %ecx
movq 0x60(%rax), %rax
movl %ecx, 0x4(%rax)
jmp 0x3464c
movq 0x10(%rsp), %rcx
movq 0x48(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x48(%rcx)
movl (%rax), %eax
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x344c0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
movq 0x10(%rax), %rdx
callq 0x39740
testb $0x1, %al
jne 0x34482
jmp 0x344c0
movq 0x10(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x18(%rcx), %eax
je 0x344c0
movq 0x10(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x20(%rcx), %eax
je 0x344c0
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
callq 0x2e590
testb $0x1, %al
jne 0x344c0
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x34651
jmp 0x3464c
movq 0x10(%rsp), %rcx
movq 0x48(%rcx), %rax
cmpq 0x58(%rcx), %rax
jne 0x344e6
movq 0x10(%rsp), %rax
movq 0x58(%rax), %rdi
movq 0x50(%rax), %rsi
callq 0x348d0
movq 0x10(%rsp), %rcx
movq 0x48(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x48(%rcx)
movl (%rax), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x18(%rcx), %eax
je 0x3455b
movq 0x10(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x20(%rcx), %eax
je 0x3455b
movq 0x10(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x28(%rcx), %eax
je 0x3455b
movq 0x10(%rsp), %rcx
movl 0x34(%rsp), %eax
cmpl 0x30(%rcx), %eax
je 0x3455b
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
movq 0x10(%rax), %rdx
callq 0x38e80
testb $0x1, %al
jne 0x3454e
jmp 0x3455b
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x34651
jmp 0x3464c
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rdi
movq %rdi, %rcx
addq $0x8, %rcx
movq %rcx, 0x48(%rax)
movq 0x50(%rax), %rsi
callq 0x34850
movq %rax, 0x20(%rsp)
movl 0x20(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x34651
movq 0x10(%rsp), %rax
movq 0x48(%rax), %rdi
movq %rdi, %rcx
addq $0x8, %rcx
movq %rcx, 0x48(%rax)
movq 0x50(%rax), %rsi
callq 0x34850
movq 0x10(%rsp), %rcx
movq %rax, 0x18(%rsp)
movl 0x18(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x18(%rcx), %eax
je 0x345f4
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
movq 0x10(%rax), %rdx
callq 0x38e80
testb $0x1, %al
jne 0x345ea
jmp 0x345f4
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x34651
jmp 0x3464c
movq 0x10(%rsp), %rcx
movq 0x48(%rcx), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x48(%rcx)
movl (%rax), %eax
movl %eax, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x18(%rcx), %eax
je 0x3463e
movq 0x10(%rsp), %rax
movq (%rax), %rdi
movl 0x34(%rsp), %esi
movq 0x10(%rax), %rdx
callq 0x38e80
testb $0x1, %al
jne 0x34634
jmp 0x3463e
movl 0x34(%rsp), %eax
movl %eax, 0x44(%rsp)
jmp 0x34651
jmp 0x3464c
movl $0x0, 0x44(%rsp)
jmp 0x34651
jmp 0x3464c
jmp 0x342f3
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopw (%rax,%rax)