// File: ATOMIC-LICHESS[P]ATOMKRAFT/endgame.cpp
// Function: Endgame<ScaleFactor, (EndgameType)15>::apply(Position const&) const
ScaleFactor Endgame<ScaleFactor, KBPKB>::apply(const Position& pos) const {

  assert(pos.non_pawn_material(strongerSide) == BishopValueMidgame);
  assert(pos.piece_count(strongerSide, BISHOP) == 1);
  assert(pos.piece_count(strongerSide, PAWN) == 1);
  assert(pos.non_pawn_material(weakerSide) == BishopValueMidgame);
  assert(pos.piece_count(weakerSide, BISHOP) == 1);
  assert(pos.piece_count(weakerSide, PAWN) == 0);

  Square pawnSq = pos.piece_list(strongerSide, PAWN, 0);
  Square strongerBishopSq = pos.piece_list(strongerSide, BISHOP, 0);
  Square weakerBishopSq = pos.piece_list(weakerSide, BISHOP, 0);
  Square weakerKingSq = pos.king_square(weakerSide);

  // Case 1: Defending king blocks the pawn, and cannot be driven away
  if (   square_file(weakerKingSq) == square_file(pawnSq)
      && relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
      && (   opposite_color_squares(weakerKingSq, strongerBishopSq)
          || relative_rank(strongerSide, weakerKingSq) <= RANK_6))
      return SCALE_FACTOR_ZERO;

  // Case 2: Opposite colored bishops
  if (opposite_color_squares(strongerBishopSq, weakerBishopSq))
  {
      // We assume that the position is drawn in the following three situations:
      //
      //   a. The pawn is on rank 5 or further back.
      //   b. The defending king is somewhere in the pawn's path.
      //   c. The defending bishop attacks some square along the pawn's path,
      //      and is at least three squares away from the pawn.
      //
      // These rules are probably not perfect, but in practice they work
      // reasonably well.

      if (relative_rank(strongerSide, pawnSq) <= RANK_5)
          return SCALE_FACTOR_ZERO;
      else
      {
          Bitboard path = squares_in_front_of(strongerSide, pawnSq);

          if (path & pos.pieces(KING, weakerSide))
              return SCALE_FACTOR_ZERO;

          if (  (pos.attacks_from<BISHOP>(weakerBishopSq) & path)
              && square_distance(weakerBishopSq, pawnSq) >= 3)
              return SCALE_FACTOR_ZERO;
      }
  }
  return SCALE_FACTOR_NONE;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rdi
movl 0x8(%rax), %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x164f0
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x3c(%rsp)
movq 0x40(%rsp), %rdi
movl 0x8(%rax), %esi
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x164f0
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x38(%rsp)
movq 0x40(%rsp), %rdi
movl 0xc(%rax), %esi
movl $0x3, %edx
xorl %ecx, %ecx
callq 0x164f0
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x34(%rsp)
movq 0x40(%rsp), %rdi
movl 0xc(%rax), %esi
callq 0x1b250
movl %eax, 0x30(%rsp)
movl 0x30(%rsp), %edi
callq 0xce10
movl %eax, 0x24(%rsp)
movl 0x3c(%rsp), %edi
callq 0xce10
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jne 0x1889c
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x3c(%rsp), %esi
callq 0x1b2c0
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x14(%rsp)
movl 0x8(%rax), %edi
movl 0x30(%rsp), %esi
callq 0x1b2c0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x1889c
movl 0x30(%rsp), %edi
movl 0x38(%rsp), %esi
callq 0x1b3a0
testb $0x1, %al
jne 0x1888f
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x30(%rsp), %esi
callq 0x1b2c0
cmpl $0x5, %eax
jg 0x1889c
movl $0x0, 0x54(%rsp)
jmp 0x1896b
movl 0x38(%rsp), %edi
movl 0x34(%rsp), %esi
callq 0x1b3a0
testb $0x1, %al
jne 0x188b2
jmp 0x18963
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x3c(%rsp), %esi
callq 0x1b2c0
cmpl $0x4, %eax
jg 0x188d5
movl $0x0, 0x54(%rsp)
jmp 0x1896b
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x3c(%rsp), %esi
callq 0x1b520
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x28(%rsp)
movq 0x28(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq 0x40(%rsp), %rdi
movl 0xc(%rax), %edx
movl $0x6, %esi
callq 0x16530
movq %rax, %rcx
movq 0x8(%rsp), %rax
andq %rcx, %rax
cmpq $0x0, %rax
je 0x1892a
movl $0x0, 0x54(%rsp)
jmp 0x1896b
movq 0x40(%rsp), %rdi
movl 0x34(%rsp), %esi
callq 0x1b550
andq 0x28(%rsp), %rax
cmpq $0x0, %rax
je 0x1895f
movl 0x34(%rsp), %edi
movl 0x3c(%rsp), %esi
callq 0xcd00
cmpl $0x3, %eax
jl 0x1895f
movl $0x0, 0x54(%rsp)
jmp 0x1896b
jmp 0x18961
jmp 0x18963
movl $0xff, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)