// File: ATOMIC-LICHESS[P]ATOMKRAFT/position.cpp
// Function: bool Position::is_draw<false>() const
bool Position::is_draw() const {

  // Draw by material?
  if (   !pieces(PAWN)
      && (non_pawn_material(WHITE) + non_pawn_material(BLACK) <= BishopValueMidgame))
      return true;

  // Draw by the 50 moves rule?
  if (st->rule50 > 99 && !is_mate())
      return true;

  // Draw by repetition?

#if 0
  if (false && !SkipRepetition) // not working in Atomic :(
  {
      int i = 4, e = Min(st->rule50, st->pliesFromNull);

      if (st && st->previous && i <= e)
      {
          StateInfo* stp = st->previous->previous;
	  if (stp && stp->previous)
	    {
	      do {
		stp = stp->previous->previous;

		if (stp && stp->key == st->key)
                  return true;

		i +=2;
		
	      } while (i <= e);
	    }
      }
  }
#else // here is the SF 2.1.1 code, not sure what ATOMKRAFT was doing ?
      // seems to have mixed-matched from later code, with state->previous
      // rather than history to detect draws
  for (int i = 4, e = Min(Min(st->gamePly, st->rule50), st->pliesFromNull);
       i <= e; i += 2)
    if (history[st->gamePly - i] == st->key)
      return true;
#endif
  return false;
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x28(%rsp)
movq 0x28(%rsp), %rdi
movq %rdi, 0x18(%rsp)
movl $0x1, %esi
callq 0x33a70
cmpq $0x0, %rax
jne 0x3e902
movq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x298e0
movq 0x18(%rsp), %rdi
movl %eax, 0x14(%rsp)
movl $0x1, %esi
callq 0x298e0
movl 0x14(%rsp), %edi
movl %eax, %esi
callq 0x1b320
leaq 0x2dc74(%rip), %rcx     # 0x6c568
cmpl (%rcx), %eax
jg 0x3e902
movb $0x1, 0x37(%rsp)
jmp 0x3ea43
movq 0x18(%rsp), %rax
movq 0x1938(%rax), %rax
cmpl $0x63, 0x14(%rax)
jle 0x3e92c
movq 0x18(%rsp), %rdi
callq 0x3d4c0
testb $0x1, %al
jne 0x3e92c
movb $0x1, 0x37(%rsp)
jmp 0x3ea43
movq 0x18(%rsp), %rcx
movl $0x4, 0x24(%rsp)
movq 0x1938(%rcx), %rax
movl 0x18(%rax), %eax
movq 0x1938(%rcx), %rcx
cmpl 0x14(%rcx), %eax
jge 0x3e964
movq 0x18(%rsp), %rax
movq 0x1938(%rax), %rax
movl 0x18(%rax), %eax
movl %eax, 0x10(%rsp)
jmp 0x3e977
movq 0x18(%rsp), %rax
movq 0x1938(%rax), %rax
movl 0x14(%rax), %eax
movl %eax, 0x10(%rsp)
movq 0x18(%rsp), %rcx
movl 0x10(%rsp), %eax
movq 0x1938(%rcx), %rcx
cmpl 0x1c(%rcx), %eax
jge 0x3e9d9
movq 0x18(%rsp), %rcx
movq 0x1938(%rcx), %rax
movl 0x18(%rax), %eax
movq 0x1938(%rcx), %rcx
cmpl 0x14(%rcx), %eax
jge 0x3e9bc
movq 0x18(%rsp), %rax
movq 0x1938(%rax), %rax
movl 0x18(%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x3e9cf
movq 0x18(%rsp), %rax
movq 0x1938(%rax), %rax
movl 0x14(%rax), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x3e9ec
movq 0x18(%rsp), %rax
movq 0x1938(%rax), %rax
movl 0x1c(%rax), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x20(%rsp), %eax
jg 0x3ea3e
movq 0x18(%rsp), %rcx
movq 0x1938(%rcx), %rax
movl 0x18(%rax), %eax
subl 0x24(%rsp), %eax
cltq
movq 0x1098(%rcx,%rax,8), %rax
movq 0x1938(%rcx), %rcx
cmpq 0x38(%rcx), %rax
jne 0x3ea2f
movb $0x1, 0x37(%rsp)
jmp 0x3ea43
jmp 0x3ea31
movl 0x24(%rsp), %eax
addl $0x2, %eax
movl %eax, 0x24(%rsp)
jmp 0x3e9f4
movb $0x0, 0x37(%rsp)
movb 0x37(%rsp), %al
andb $0x1, %al
addq $0x38, %rsp
retq
nop