// File: /workspace/github_cpp_makefile_star2_O1/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 0x8af0
cmpl $0x0, 0x230(%r14)
je 0x8af0
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x8624
movq %rax, %r15
movq %r14, %rdi
movq %rax, %rsi
callq 0x8b02
xorl %r13d, %r13d
cmpl 0x230(%r14), %eax
jge 0x8a95
movl %eax, %r12d
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %ebp, %ebp
movq %r14, %rdi
movl %r12d, %esi
callq 0x8b6e
cmpq %r15, %rax
jne 0x8a95
movq %rdx, %rcx
cmpl $0x10000, %ecx          # imm = 0x10000
jb 0x8a85
movl %ecx, %esi
shrl $0x10, %esi
cmpb $0x0, 0x4(%rsp)
je 0x8a1d
cmpl %ebp, %esi
cmoval %esi, %ebp
movzwl %cx, %eax
cmoval %eax, %r13d
jmp 0x8a85
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 0x89e0
movl %r13d, %eax
shrl $0xc, %eax
andl $0x7, %eax
je 0x8abb
shll $0xc, %eax
andl $0xfff, %r13d           # imm = 0xFFF
addl %eax, %r13d
addl $0xfffff000, %r13d      # imm = 0xFFFFF000
orl $0x4000, %r13d          # imm = 0x4000
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x1a0ce
cmpq %rax, %r14
je 0x8aee
movq %rax, %rcx
movl (%r14), %eax
movl %eax, %edx
andl $0xffff3fff, %edx       # imm = 0xFFFF3FFF
cmpl %r13d, %edx
je 0x8af0
addq $0x8, %r14
cmpq %rcx, %r14
jne 0x8ad5
xorl %eax, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq