// File: /workspace/github_cpp_makefile_star2_O3/ATOMIC-LICHESS[P]ATOMKRAFT/endgame.cpp
// Function: Endgame<Value, (EndgameType)3>::apply(Position const&) const
ENDNEW



/// Mate with KX vs K. This function is used to evaluate positions with
/// King and plenty of material vs a lone king. It simply gives the
/// attacking side a bonus for driving the defending king towards the edge
/// of the board, and for keeping the distance between the two kings small.
template<>
Value Endgame<Value, KXK>::apply(const Position& pos) const {

  assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
  assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO);

  STARTOLD
  Square winnerKSq = pos.king_square(strongerSide);
  Square loserKSq = pos.king_square(weakerSide);
  
  Value result =   pos.non_pawn_material(strongerSide)
                 + pos.piece_count(strongerSide, PAWN) * PawnValueEndgame
                 + MateTable[loserKSq]
                 + DistanceBonus[square_distance(winnerKSq, loserKSq)];

  if (   pos.piece_count(strongerSide, QUEEN)
      || pos.piece_count(strongerSide, ROOK)
      || pos.piece_count(strongerSide, BISHOP) > 1)
      // TODO: check for two equal-colored bishops!
      result += VALUE_KNOWN_WIN;
  ENDOLD

  if (    pos.piece_count(strongerSide, QUEEN) 
	  && (   pos.piece_count(strongerSide, ROOK)
		  || pos.piece_count(strongerSide, BISHOP)
		  || pos.piece_count(strongerSide, KNIGHT)
	)) {
	  return strongerSide == pos.side_to_move() ? VALUE_KNOWN_WIN : -VALUE_KNOWN_WIN;
  }
  
  if (pos.piece_count(strongerSide, PAWN) > 1) {
	  return strongerSide == pos.side_to_move() ? VALUE_KNOWN_WIN : -VALUE_KNOWN_WIN;
  }

  return VALUE_NONE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl 0x1090(%rsi), %ecx
movl 0x8(%rdi), %edx
movl 0xc(%rdi), %r10d
xorl %r8d, %r8d
cmpl %edx, %ecx
sete %r9b
movq %rdx, %rax
shlq $0x9, %rax
movl 0x290(%rsi,%rax), %ebp
movl 0x310(%rsi,%rax), %eax
shlq $0x9, %r10
movl 0x1d0(%rsi,%r10), %edi
movl 0x310(%rsi,%r10), %esi
cmpl $0x1, %edx
jne 0xc4ca
xorl $0x38, %eax
xorl $0x38, %ebp
xorl $0x38, %esi
xorl $0x38, %edi
movl %edi, %ebx
andl $0x7, %ebx
movl %eax, %r10d
andl $0x7, %r10d
cmpl %edi, %eax
jge 0xc4fd
cmpl %ebx, %r10d
jne 0xc4fd
leaq 0x30f7e(%rip), %r8      # 0x3d464
sarl $0x3, %eax
sarl $0x3, %edi
subl %edi, %eax
movl %eax, %esi
negl %esi
cmovnsl %eax, %esi
addl (%r8), %esi
jmp 0xc64c
movb %r9b, %r8b
movl %esi, %r15d
andl $0x7, %r15d
movl %r15d, %r9d
subl %ebx, %r9d
movl %r9d, %r11d
negl %r11d
cmovsl %r9d, %r11d
sarl $0x3, %esi
movl %edi, %r9d
sarl $0x3, %r9d
movl %esi, %r12d
subl %r9d, %r12d
movl %r12d, %r14d
negl %r14d
cmovsl %r12d, %r14d
cmpl %r14d, %r11d
cmoval %r11d, %r14d
movl %r8d, %r12d
xorl $0x1, %r12d
movl %r14d, %r13d
subl %r12d, %r13d
cmpl $0x3, %r13d
jl 0xc5b1
movl %ebp, %r12d
andl $0x7, %r12d
subl %r12d, %r15d
movl %r15d, %r12d
negl %r12d
cmovsl %r15d, %r12d
sarl $0x3, %ebp
movl %esi, %r15d
subl %ebp, %r15d
movl %r15d, %ebp
negl %ebp
cmovsl %r15d, %ebp
cmpl %ebp, %r12d
cmoval %r12d, %ebp
cmpl $0x3, %ebp
jb 0xc5b1
leaq 0x30ee0(%rip), %rsi     # 0x3d464
subl %ebx, %r10d
movl %r10d, %edi
negl %edi
cmovsl %r10d, %edi
movl (%rsi), %esi
sarl $0x3, %eax
subl %r9d, %eax
movl %eax, %r8d
negl %r8d
cmovsl %eax, %r8d
cmpl %r8d, %edi
cmoval %edi, %r8d
subl %r8d, %esi
jmp 0xc64c
sarl $0x3, %eax
cmpl $0x2, %esi
jg 0xc601
cmpl $0x1, %r14d
jne 0xc601
cmpl $0x3, %eax
jl 0xc601
movl %r10d, %ebp
subl %ebx, %ebp
movl %ebp, %r14d
negl %r14d
cmovsl %ebp, %r14d
movl %eax, %r15d
subl %r9d, %r15d
movl %r15d, %ebp
negl %ebp
cmovsl %r15d, %ebp
cmpl %ebp, %r14d
cmoval %r14d, %ebp
movl %ebp, %r14d
subl %r8d, %r14d
cmpl $0x3, %r14d
jl 0xc601
shll $0x3, %ebp
movl $0x50, %esi
subl %ebp, %esi
jmp 0xc64c
addl $-0x8, %edi
subl %ebx, %r10d
movl %r10d, %r8d
negl %r8d
cmovsl %r10d, %r8d
sarl $0x3, %edi
subl %edi, %eax
movl %eax, %r10d
negl %r10d
cmovsl %eax, %r10d
cmpl %r10d, %r8d
cmoval %r8d, %r10d
subl %edi, %esi
movl %esi, %eax
negl %eax
cmovsl %esi, %eax
cmpl %eax, %r11d
cmoval %r11d, %eax
movl %r9d, %esi
negl %esi
cmovsl %r9d, %esi
addl %eax, %esi
subl %r10d, %esi
leal 0xc8(,%rsi,8), %esi
movl %esi, %eax
negl %eax
cmpl %edx, %ecx
cmovel %esi, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq