// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/book.cpp
// Function: Book::get_move(Position const&, bool)
Move Book::get_move(const Position& pos, bool findBestMove) {

  if (!bookFile.is_open() || bookSize == 0)
      return MOVE_NONE;
  

  BookEntry entry;
  int bookMove = MOVE_NONE;
  unsigned score, scoresSum = 0, bestScore = 0;
  uint64_t key = book_key(pos);
  
  // Choose a book move among the possible moves for the given position
  for (int idx = find_entry(key); idx < bookSize; idx++)
  {
      entry = read_entry(idx);
      

      if (entry.key != key)
          break;

      score = entry.count;
      
      NEW // skip moves with non positive score
      NEW if (score <= 0) continue;
      
      
      if (!findBestMove)
      {
          // Choose book move according to its score. If a move has a very
          // high score it has more probability to be choosen then a one with
          // lower score. Note that first entry is always chosen.
          scoresSum += score;
          if (RKiss.rand<unsigned>() % scoresSum < score)
              bookMove = entry.move;
      }
      else if (score > bestScore)
      {
          bestScore = score;
          bookMove = entry.move;
      }
  }

  // A PolyGlot book move is encoded as follows:
  //
  // bit  0- 5: destination square (from 0 to 63)
  // bit  6-11: origin square (from 0 to 63)
  // bit 12-13-14: promotion piece (from KNIGHT == 1 to QUEEN == 4)
  //
  // Castling moves follow "king captures rook" representation. So in case
  // book move is a promotion we have to convert to our representation, in
  // all other cases we can directly compare with a Move after having
  // masked out special Move's flags that are not supported by PolyGlot.
  int p = (bookMove >> 12) & 7;

  if (p)
      bookMove = int(make_promotion_move(move_from(Move(bookMove)),
                 move_to(Move(bookMove)), PieceType(p + 1)));

  // Verify the book move (if any) is legal
  MoveStack mlist[MAX_MOVES];
  MoveStack* last = generate<MV_LEGAL>(pos, mlist);
  for (MoveStack* cur = mlist; cur != last; cur++)
      if ((int(cur->move) & ~(3 << 14)) == bookMove) // Mask out special flags
          return cur->move;

  return MOVE_NONE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x838, %rsp            # imm = 0x838
movl %edx, 0x14(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
addq $0x80, %rdi
callq 0x50e0
movl %eax, %ecx
xorl %eax, %eax
testb %cl, %cl
je 0x86a7
cmpl $0x0, 0x230(%r14)
je 0x86a7
movq %rbx, 0x18(%rsp)
movq %rbx, %rdi
callq 0x82c8
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x86ba
movl %eax, %r12d
leaq 0x238(%r14), %rax
movq %rax, 0x28(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
movq %r15, 0x20(%rsp)
cmpl 0x230(%r14), %r12d
jge 0x8650
movq %r14, %rdi
movl %r12d, %esi
callq 0x871e
cmpq %r15, %rax
jne 0x8650
movq %rdx, %rbx
cmpl $0x10000, %ebx          # imm = 0x10000
jb 0x864b
movl %ebx, %ebp
shrl $0x10, %ebp
cmpb $0x0, 0x14(%rsp)
je 0x8621
movl 0x10(%rsp), %eax
cmpl %eax, %ebp
cmoval %ebp, %eax
movl %eax, 0x10(%rsp)
movzwl %bx, %eax
cmoval %eax, %r13d
jmp 0x864b
movl 0xc(%rsp), %r15d
addl %ebp, %r15d
movq 0x28(%rsp), %rdi
callq 0x8bc8
xorl %edx, %edx
movl %r15d, 0xc(%rsp)
divl %r15d
movq 0x20(%rsp), %r15
cmpl %ebp, %edx
movzwl %bx, %eax
cmovbl %eax, %r13d
incl %r12d
jmp 0x85db
movl %r13d, %eax
shrl $0xc, %eax
andl $0x7, %eax
je 0x8676
shll $0xc, %eax
andl $0xfff, %r13d           # imm = 0xFFF
addl %eax, %r13d
addl $0xfffff000, %r13d      # imm = 0xFFFFF000
orl $0x4000, %r13d          # imm = 0x4000
leaq 0x30(%rsp), %rbx
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x1543a
movq %rax, %rcx
cmpq %rcx, %rbx
je 0x86a5
movl (%rbx), %eax
movl %eax, %edx
andl $0xffff3fff, %edx       # imm = 0xFFFF3FFF
addq $0x8, %rbx
cmpl %r13d, %edx
jne 0x868b
jmp 0x86a7
xorl %eax, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop