// File: /workspace/github_cpp_makefile_star2_O3/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 $0x818, %rsp            # imm = 0x818
movl %edx, 0x4(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
addq $0x80, %rdi
callq 0x50e0
movl %eax, %ecx
xorl %eax, %eax
testb %cl, %cl
je 0x9050
cmpl $0x0, 0x230(%r14)
je 0x9050
movq %rbx, %rdi
callq 0x8b78
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x9062
movl $0x0, %r13d
cmpl 0x230(%r14), %eax
jge 0x901d
movl %eax, %r12d
movq %rbx, 0x8(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r14, %rdi
movl %r12d, %esi
callq 0x90ce
cmpq %r15, %rax
jne 0x8ff2
movq %rdx, %rcx
cmpl $0x10000, %ecx          # imm = 0x10000
jb 0x8fe2
movl %ecx, %esi
shrl $0x10, %esi
cmpb $0x0, 0x4(%rsp)
je 0x8f7a
cmpl %ebp, %esi
cmoval %esi, %ebp
movzwl %cx, %eax
cmoval %eax, %r13d
jmp 0x8fe2
addl %esi, %ebx
movq 0x238(%r14), %rdx
movq 0x240(%r14), %rdi
movq 0x248(%r14), %r8
movq %r8, %rax
rolq $0xd, %rax
xorq %rdi, %rax
rolq $0x7, %rdi
subq %rdi, %rdx
movq %rax, 0x238(%r14)
movq 0x250(%r14), %rdi
movq %rdi, %r9
rolq $0x25, %r9
addq %r8, %r9
movq %r9, 0x240(%r14)
addq %rdx, %rdi
movq %rdi, 0x248(%r14)
addq %rdx, %rax
movq %rax, 0x250(%r14)
xorl %edx, %edx
divl %ebx
cmpl %esi, %edx
movzwl %cx, %eax
cmovbl %eax, %r13d
incl %r12d
cmpl 0x230(%r14), %r12d
jl 0x8f3d
movl %r13d, %eax
shrl $0xc, %eax
andl $0x7, %eax
je 0x9018
shll $0xc, %eax
andl $0xfff, %r13d           # imm = 0xFFF
addl %eax, %r13d
addl $0xfffff000, %r13d      # imm = 0xFFFFF000
orl $0x4000, %r13d          # imm = 0x4000
movq 0x8(%rsp), %rbx
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1a5c0
cmpq %rax, %r14
je 0x904e
movq %rax, %rcx
movl (%r14), %eax
movl %eax, %edx
andl $0xffff3fff, %edx       # imm = 0xFFFF3FFF
cmpl %r13d, %edx
je 0x9050
addq $0x8, %r14
cmpq %rcx, %r14
jne 0x9035
xorl %eax, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq