// File: /workspace/github_cpp_makefile_star2_O1/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 0xbf96
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 0xbfc9
cmpl %ebx, %r10d
jne 0xbfc9
leaq 0x304b2(%rip), %r8      # 0x3c464
sarl $0x3, %eax
sarl $0x3, %edi
subl %edi, %eax
movl %eax, %esi
negl %esi
cmovnsl %eax, %esi
addl (%r8), %esi
jmp 0xc124
movb %r9b, %r8b
movl %esi, %r15d
andl $0x7, %r15d
movl %r15d, %r11d
subl %ebx, %r11d
movl %r11d, %r12d
negl %r12d
cmovsl %r11d, %r12d
sarl $0x3, %esi
movl %edi, %r9d
sarl $0x3, %r9d
movl %esi, %r13d
subl %r9d, %r13d
movl %r13d, %r14d
negl %r14d
cmovsl %r13d, %r14d
cmpl %r14d, %r12d
cmoval %r12d, %r14d
movl %r8d, %r12d
xorl $0x1, %r12d
movl %r14d, %r13d
subl %r12d, %r13d
cmpl $0x3, %r13d
jl 0xc07d
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 0xc07d
leaq 0x30414(%rip), %rsi     # 0x3c464
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 0xc124
cmpl $0x2, %esi
jg 0xc0cf
cmpl $0x1, %r14d
jne 0xc0cf
movl %eax, %ebp
sarl $0x3, %ebp
cmpl $0x3, %ebp
jl 0xc0cf
movl %r10d, %r14d
subl %ebx, %r14d
movl %r14d, %r15d
negl %r15d
cmovsl %r14d, %r15d
subl %r9d, %ebp
movl %ebp, %r14d
negl %r14d
cmovsl %ebp, %r14d
cmpl %r14d, %r15d
cmoval %r15d, %r14d
movl %r14d, %ebp
subl %r8d, %ebp
cmpl $0x3, %ebp
jl 0xc0cf
shll $0x3, %r14d
movl $0x50, %esi
subl %r14d, %esi
jmp 0xc124
addl $-0x8, %edi
subl %ebx, %r10d
movl %r10d, %r8d
negl %r8d
cmovsl %r10d, %r8d
sarl $0x3, %eax
sarl $0x3, %edi
subl %edi, %eax
movl %eax, %r10d
negl %r10d
cmovsl %eax, %r10d
cmpl %r10d, %r8d
cmoval %r8d, %r10d
movl %r11d, %eax
negl %eax
cmovsl %r11d, %eax
subl %edi, %esi
movl %esi, %edi
negl %edi
cmovsl %esi, %edi
cmpl %edi, %eax
cmoval %eax, %edi
movl %r9d, %eax
negl %eax
cmovsl %r9d, %eax
addl %edi, %eax
subl %r10d, %eax
leal 0xc8(,%rax,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