// File: ATOMIC-LICHESS[P]ATOMKRAFT/endgame.cpp
// Function: Endgame<ScaleFactor, (EndgameType)10>::apply(Position const&) const
ScaleFactor Endgame<ScaleFactor, KBPsK>::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);

  // No assertions about the material of weakerSide, because we want draws to
  // be detected even when the weaker side has some pawns.

  Bitboard pawns = pos.pieces(PAWN, strongerSide);
  File pawnFile = square_file(pos.piece_list(strongerSide, PAWN, 0));

  // All pawns are on a single rook file ?
  if (   (pawnFile == FILE_A || pawnFile == FILE_H)
      && (pawns & ~file_bb(pawnFile)) == EmptyBoardBB)
  {
      Square bishopSq = pos.piece_list(strongerSide, BISHOP, 0);
      Square queeningSq = relative_square(strongerSide, make_square(pawnFile, RANK_8));
      Square kingSq = pos.king_square(weakerSide);

      if (   opposite_color_squares(queeningSq, bishopSq)
          && abs(square_file(kingSq) - pawnFile) <= 1)
      {
          // The bishop has the wrong color, and the defending king is on the
          // file of the pawn(s) or the neighboring file. Find the rank of the
          // frontmost pawn.
          Rank rank;
          if (strongerSide == WHITE)
          {
              for (rank = RANK_7; (rank_bb(rank) & pawns) == EmptyBoardBB; rank--) {}
              assert(rank >= RANK_2 && rank <= RANK_7);
          }
          else
          {
              for (rank = RANK_2; (rank_bb(rank) & pawns) == EmptyBoardBB; rank++) {}
              rank = Rank(rank ^ 7);  // HACK to get the relative rank
              assert(rank >= RANK_2 && rank <= RANK_7);
          }
          // If the defending king has distance 1 to the promotion square or
          // is placed somewhere in front of the pawn, it's a draw.
          if (   square_distance(kingSq, queeningSq) <= 1
              || relative_rank(strongerSide, kingSq) >= rank)
              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), %edx
movl $0x1, %esi
callq 0x16530
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x38(%rsp)
movq 0x40(%rsp), %rdi
movl 0x8(%rax), %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x164f0
movl %eax, %edi
callq 0xce10
movl %eax, 0x34(%rsp)
cmpl $0x0, 0x34(%rsp)
je 0x17ad8
cmpl $0x7, 0x34(%rsp)
jne 0x17c45
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x34(%rsp), %edi
callq 0x1b360
movq %rax, %rcx
movq 0x10(%rsp), %rax
xorq $-0x1, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
jne 0x17c45
movq 0x18(%rsp), %rax
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, 0x30(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0x34(%rsp), %edi
movl $0x7, %esi
callq 0xd740
movl 0xc(%rsp), %edi
movl %eax, %esi
callq 0x1b380
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x2c(%rsp)
movq 0x40(%rsp), %rdi
movl 0xc(%rax), %esi
callq 0x1b250
movl %eax, 0x28(%rsp)
movl 0x2c(%rsp), %edi
movl 0x30(%rsp), %esi
callq 0x1b3a0
testb $0x1, %al
jne 0x17b7a
jmp 0x17c43
movl 0x28(%rsp), %edi
callq 0xce10
movl %eax, %edi
movl 0x34(%rsp), %esi
callq 0xcdf0
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cmpl $0x1, %eax
jg 0x17c43
movq 0x18(%rsp), %rax
cmpl $0x0, 0x8(%rax)
jne 0x17bd7
movl $0x6, 0x24(%rsp)
movl 0x24(%rsp), %edi
callq 0x1b3d0
andq 0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x17bd5
jmp 0x17bc7
leaq 0x24(%rsp), %rdi
xorl %esi, %esi
callq 0xe590
jmp 0x17bb1
jmp 0x17c0e
movl $0x1, 0x24(%rsp)
movl 0x24(%rsp), %edi
callq 0x1b3d0
andq 0x38(%rsp), %rax
cmpq $0x0, %rax
jne 0x17c03
jmp 0x17bf5
leaq 0x24(%rsp), %rdi
xorl %esi, %esi
callq 0x1b3f0
jmp 0x17bdf
movl 0x24(%rsp), %eax
xorl $0x7, %eax
movl %eax, 0x24(%rsp)
movl 0x28(%rsp), %edi
movl 0x2c(%rsp), %esi
callq 0xcd00
cmpl $0x1, %eax
jle 0x17c37
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x28(%rsp), %esi
callq 0x1b2c0
cmpl 0x24(%rsp), %eax
jl 0x17c41
movl $0x0, 0x54(%rsp)
jmp 0x17c4d
jmp 0x17c43
jmp 0x17c45
movl $0xff, 0x54(%rsp)
movl 0x54(%rsp), %eax
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)