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

  assert(pos.non_pawn_material(strongerSide) == RookValueMidgame);
  assert(pos.piece_count(strongerSide, PAWN) == 2);
  assert(pos.non_pawn_material(weakerSide) == RookValueMidgame);
  assert(pos.piece_count(weakerSide, PAWN) == 1);

  Square wpsq1 = pos.piece_list(strongerSide, PAWN, 0);
  Square wpsq2 = pos.piece_list(strongerSide, PAWN, 1);
  Square bksq = pos.king_square(weakerSide);

  // Does the stronger side have a passed pawn?
  if (   pos.pawn_is_passed(strongerSide, wpsq1)
      || pos.pawn_is_passed(strongerSide, wpsq2))
      return SCALE_FACTOR_NONE;

  Rank r = Max(relative_rank(strongerSide, wpsq1), relative_rank(strongerSide, wpsq2));

  if (   file_distance(bksq, wpsq1) <= 1
      && file_distance(bksq, wpsq2) <= 1
      && relative_rank(strongerSide, bksq) > r)
  {
      switch (r) {
      case RANK_2: return ScaleFactor(10);
      case RANK_3: return ScaleFactor(10);
      case RANK_4: return ScaleFactor(15);
      case RANK_5: return ScaleFactor(20);
      case RANK_6: return ScaleFactor(40);
      default: assert(false);
      }
  }
  return SCALE_FACTOR_NONE;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rdi
movl 0x8(%rax), %esi
movl $0x1, %edx
xorl %ecx, %ecx
callq 0x164f0
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x2c(%rsp)
movq 0x30(%rsp), %rdi
movl 0x8(%rax), %esi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x164f0
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x28(%rsp)
movq 0x30(%rsp), %rdi
movl 0xc(%rax), %esi
callq 0x1b250
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x24(%rsp)
movq 0x30(%rsp), %rdi
movl 0x8(%rax), %esi
movl 0x2c(%rsp), %edx
callq 0x1b4b0
testb $0x1, %al
jne 0x1851f
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rdi
movl 0x8(%rax), %esi
movl 0x28(%rsp), %edx
callq 0x1b4b0
testb $0x1, %al
jne 0x1851f
jmp 0x1852c
movl $0xff, 0x44(%rsp)
jmp 0x18636
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x2c(%rsp), %esi
callq 0x1b2c0
movl %eax, %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x14(%rsp)
movl 0x8(%rax), %edi
movl 0x28(%rsp), %esi
callq 0x1b2c0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x18575
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x28(%rsp), %esi
callq 0x1b2c0
movl %eax, 0x10(%rsp)
jmp 0x1858a
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x2c(%rsp), %esi
callq 0x1b2c0
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x24(%rsp), %edi
movl 0x2c(%rsp), %esi
callq 0xcd70
cmpl $0x1, %eax
jg 0x1862e
movl 0x24(%rsp), %edi
movl 0x28(%rsp), %esi
callq 0xcd70
cmpl $0x1, %eax
jg 0x1862e
movq 0x18(%rsp), %rax
movl 0x8(%rax), %edi
movl 0x24(%rsp), %esi
callq 0x1b2c0
cmpl 0x20(%rsp), %eax
jle 0x1862e
movl 0x20(%rsp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, 0x8(%rsp)
subl $0x4, %eax
ja 0x1862a
movq 0x8(%rsp), %rax
leaq 0x413d1(%rip), %rcx     # 0x599c0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xa, 0x44(%rsp)
jmp 0x18636
movl $0xa, 0x44(%rsp)
jmp 0x18636
movl $0xf, 0x44(%rsp)
jmp 0x18636
movl $0x14, 0x44(%rsp)
jmp 0x18636
movl $0x28, 0x44(%rsp)
jmp 0x18636
jmp 0x1862c
jmp 0x1862e
movl $0xff, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nop