// File: /workspace/github_cpp_makefile_star2_O3/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 0x16e5b(%rip), %r15     # 0x329a4
movq 0x48(%rbx), %rcx
movq 0x50(%rbx), %rax
cmpq %rax, %rcx
jne 0x1bb60
movq %rbx, %rdi
callq 0x1b736
jmp 0x1bb49
movl 0x3c(%rbx), %edx
cmpq $0x8, %rdx
ja 0x1bb49
movslq (%r15,%rdx,4), %rdx
addq %r15, %rdx
jmpq *%rdx
leaq 0x8(%rcx), %rdx
movq %rdx, 0x48(%rbx)
movq (%rcx), %r14
cmpq %rax, %rdx
je 0x1bbae
movq %r14, %rcx
shrq $0x20, %rcx
cmpl 0x4(%rdx), %ecx
jge 0x1bba5
movl %r14d, %esi
movq (%rdx), %r14
shlq $0x20, %rcx
orq %rcx, %rsi
movq %rsi, (%rdx)
movq %r14, %rcx
shrq $0x20, %rcx
addq $0x8, %rdx
cmpq %rax, %rdx
jne 0x1bb89
cmpl %r14d, 0x18(%rbx)
je 0x1bb49
jmp 0x1bcd3
leaq 0x8(%rcx), %rax
movq %rax, 0x48(%rbx)
movl (%rcx), %r14d
cmpl 0x18(%rbx), %r14d
je 0x1bb49
jmp 0x1bcd3
leaq 0x8(%rcx), %rax
movq %rax, 0x48(%rbx)
movl (%rcx), %r14d
testq %r14, %r14
je 0x1bb49
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x1ed3c
testb %al, %al
je 0x1bb49
cmpl 0x18(%rbx), %r14d
je 0x1bb49
cmpl 0x20(%rbx), %r14d
je 0x1bb49
testl $0x18000, %r14d         # imm = 0x18000
je 0x1bda3
movl %r14d, %eax
notl %eax
testl $0xc000, %eax           # imm = 0xC000
jne 0x1bb49
jmp 0x1bdf9
movq 0x58(%rbx), %rdx
cmpq %rdx, %rcx
jne 0x1bca0
cmpq %rax, %rcx
sete %sil
leaq 0x8(%rdx), %rdi
cmpq %rax, %rdi
sete %r8b
orb %sil, %r8b
jne 0x1bca0
movq %rdx, %r8
movq %rdi, %rsi
movq 0x8(%r8), %rdi
movq %rdi, %r9
shrq $0x20, %r9
cmpl %r9d, 0x4(%r8)
jge 0x1bc90
movq (%r8), %r10
movq %r10, 0x8(%r8)
cmpq %rcx, %r8
je 0x1bc8a
leaq -0x8(%r8), %r10
cmpl %r9d, -0x4(%r8)
movq %r10, %r8
jl 0x1bc6b
addq $0x8, %r10
jmp 0x1bc8d
movq %rdx, %r10
movq %rdi, (%r10)
leaq 0x8(%rsi), %rdi
movq %rsi, %r8
cmpq %rax, %rdi
jne 0x1bc57
movq 0x48(%rbx), %rcx
leaq 0x8(%rcx), %rax
movq %rax, 0x48(%rbx)
movl (%rcx), %r14d
cmpl 0x18(%rbx), %r14d
je 0x1bb49
cmpl 0x20(%rbx), %r14d
je 0x1bb49
cmpl 0x28(%rbx), %r14d
je 0x1bb49
cmpl 0x30(%rbx), %r14d
je 0x1bb49
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x1e96c
jmp 0x1bd07
leaq 0x8(%rcx), %rax
movq %rax, 0x48(%rbx)
movl (%rcx), %r14d
testl %r14d, %r14d
je 0x1bb49
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x1ed3c
testb %al, %al
je 0x1bb49
jmp 0x1bdf9
leaq 0x8(%rcx), %rdx
movq %rdx, 0x48(%rbx)
movq (%rcx), %r14
cmpq %rax, %rdx
je 0x1bd50
movq %r14, %rcx
shrq $0x20, %rcx
cmpl 0x4(%rdx), %ecx
jge 0x1bd47
movl %r14d, %esi
movq (%rdx), %r14
shlq $0x20, %rcx
orq %rcx, %rsi
movq %rsi, (%rdx)
movq %r14, %rcx
shrq $0x20, %rcx
addq $0x8, %rdx
cmpq %rax, %rdx
jne 0x1bd2b
cmpl %r14d, 0x18(%rbx)
je 0x1bb49
cmpl %r14d, 0x20(%rbx)
je 0x1bb49
movq (%rbx), %rdi
movq 0x10(%rbx), %rdx
movl %r14d, %esi
callq 0x1e96c
testb %al, %al
je 0x1bb49
movq (%rbx), %rdi
movl %r14d, %esi
callq 0x20bf8
cmpl 0x38(%rbx), %eax
jge 0x1bdf9
movq 0x60(%rbx), %rcx
leaq -0x8(%rcx), %rdx
movq %rdx, 0x60(%rbx)
movl %r14d, -0x8(%rcx)
movl %eax, -0x4(%rcx)
jmp 0x1bb49
movq (%rbx), %rax
movl %r14d, %ecx
andl $0x3f, %ecx
cmpl $0x10, (%rax,%rcx,4)
jne 0x1bb49
jmp 0x1bdf9
xorl %r14d, %r14d
jmp 0x1bdf9
leaq 0x8(%rcx), %rdx
movq %rdx, 0x48(%rbx)
movq (%rcx), %r14
cmpq %rax, %rdx
je 0x1bdf9
movq %r14, %rcx
shrq $0x20, %rcx
cmpl 0x4(%rdx), %ecx
jge 0x1bdf0
movl %r14d, %esi
movq (%rdx), %r14
shlq $0x20, %rcx
orq %rcx, %rsi
movq %rsi, (%rdx)
movq %r14, %rcx
shrq $0x20, %rcx
addq $0x8, %rdx
cmpq %rax, %rdx
jne 0x1bdd4
movl %r14d, %eax
popq %rbx
popq %r14
popq %r15
retq
nop