// File: ATOMIC-LICHESS[P]ATOMKRAFT/movegen.cpp
// Function: MoveStack* generate<(MoveType)4>(Position const&, MoveStack*)
MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
  NEW assert(pos.piece_count(WHITE, KING) && pos.piece_count(BLACK, KING));
		
  assert(pos.is_ok());
  assert(pos.in_check());

  Bitboard b, target;
  Square from, checksq;
  int checkersCnt = 0;
  Color us = pos.side_to_move();
  NEW assert(pos.piece_count(us, KING));
  Square ksq = pos.king_square(us);
  Bitboard checkers = pos.checkers();
  Bitboard sliderAttacks = EmptyBoardBB;

  assert(pos.piece_on(ksq) == make_piece(us, KING));
  assert(checkers);

  // Find squares attacked by slider checkers, we will remove
  // them from the king evasions set so to early skip known
  // illegal moves and avoid an useless legality check later.
  b = checkers;
  do
  {
      checkersCnt++;
      checksq = pop_1st_bit(&b);
      
      STARTNEW
//      if (pos.color_of_piece_on(checksq) != opposite_color(us)) {
//    	  cout << "ERROR" << endl;
//    	  print_pos(pos);
//    	  cout << checksq <<  endl;
//    	  cout << PIECE_NAME[pos.piece_on(checksq)] << endl;
//    	  cout << pos.color_of_piece_on(checksq) << endl;
//    	  cout << opposite_color(us) << endl;
//    	  
//  	  }
      ENDNEW
      
      assert(pos.color_of_piece_on(checksq) == opposite_color(us));

      switch (pos.type_of_piece_on(checksq))
      {
      case BISHOP: sliderAttacks |= BishopPseudoAttacks[checksq]; break;
      case ROOK:   sliderAttacks |= RookPseudoAttacks[checksq];   break;
      case QUEEN:
          // In case of a queen remove also squares attacked in the other direction to
          // avoid possible illegal moves when queen and king are on adjacent squares.
          if (RookPseudoAttacks[checksq] & (1ULL << ksq))
              sliderAttacks |= RookPseudoAttacks[checksq] | pos.attacks_from<BISHOP>(checksq);
          else
              sliderAttacks |= BishopPseudoAttacks[checksq] | pos.attacks_from<ROOK>(checksq);
          break;
      default:
          break;
      }
  } while (b);

  // Generate evasions for king, capture and non capture moves
  OLD b = pos.attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~sliderAttacks;
  
  
  STARTNEW
  // while capture moves by the king are never allowed
  // moving our king next to the opponents king is also check evasion evasion
  assert(pos.piece_count(opposite_color(us), KING));
  Square opp_ksq = pos.king_square(opposite_color(us));
  //b = pos.attacks_from<KING>(ksq) & ~pos.occupied_squares() & ~sliderAttacks;
  //b |= (pos.attacks_from<KING>(ksq) & pos.attacks_from<KING>(opp_ksq));
  // A & ~B & ~C | (A & D) == A & ((~B & ~C) | D)
  b = pos.attacks_from<KING>(ksq) & ((~pos.occupied_squares() & ~sliderAttacks) | pos.attacks_from<KING>(opp_ksq));
  ENDNEW
  
  from = ksq;
  SERIALIZE_MOVES(b);
  
  NEW // unfortunatly this is not true for atomic
  // Generate evasions for other pieces only if not double check
  OLD if (checkersCnt > 1)
  OLD    return mlist;

  // Find squares where a blocking evasion or a capture of the
  // checker piece is possible.
  OLD target = squares_between(checksq, ksq) | checkers;
  
  STARTNEW
  
  if (checkersCnt <= 1) {
	  // one checker => moving a piece between is a possible evasion
	  target = squares_between(checksq, ksq) | checkers;
  } else {
	  // more checkers and we have to move the king or capture all of them
	  target = EmptyBoardBB;
  }

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movl $0x0, 0x8c(%rsp)
movq 0xb0(%rsp), %rdi
callq 0xf5d0
movl %eax, 0x88(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x88(%rsp), %esi
callq 0x1b250
movl %eax, 0x84(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x339b0
movq %rax, 0x78(%rsp)
movq $0x0, 0x70(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl 0x8c(%rsp), %eax
incl %eax
movl %eax, 0x8c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xd940
movl %eax, 0x90(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x90(%rsp), %esi
callq 0x11f10
movl %eax, %ecx
movl %ecx, 0x5c(%rsp)
subl $0x3, %eax
je 0x2fb17
jmp 0x2fafe
movl 0x5c(%rsp), %eax
subl $0x4, %eax
je 0x2fb39
jmp 0x2fb09
movl 0x5c(%rsp), %eax
subl $0x5, %eax
je 0x2fb5b
jmp 0x2fc0f
movslq 0x90(%rsp), %rcx
leaq 0x12393a(%rip), %rax    # 0x153460
movq (%rax,%rcx,8), %rax
orq 0x70(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x2fc11
movslq 0x90(%rsp), %rcx
leaq 0x123b18(%rip), %rax    # 0x153660
movq (%rax,%rcx,8), %rax
orq 0x70(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x2fc11
movslq 0x90(%rsp), %rcx
leaq 0x123af6(%rip), %rax    # 0x153660
movq (%rax,%rcx,8), %rax
movl 0x84(%rsp), %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2fbcc
movslq 0x90(%rsp), %rcx
leaq 0x123ac8(%rip), %rax    # 0x153660
movq (%rax,%rcx,8), %rax
movq %rax, 0x50(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x90(%rsp), %esi
callq 0x1b550
movq %rax, %rcx
movq 0x50(%rsp), %rax
orq %rcx, %rax
orq 0x70(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x2fc0d
movslq 0x90(%rsp), %rcx
leaq 0x123885(%rip), %rax    # 0x153460
movq (%rax,%rcx,8), %rax
movq %rax, 0x48(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x90(%rsp), %esi
callq 0x29d40
movq %rax, %rcx
movq 0x48(%rsp), %rax
orq %rcx, %rax
orq 0x70(%rsp), %rax
movq %rax, 0x70(%rsp)
jmp 0x2fc11
jmp 0x2fc11
jmp 0x2fc13
cmpq $0x0, 0xa0(%rsp)
jne 0x2fab9
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0x88(%rsp), %edi
callq 0x122b0
movq 0x30(%rsp), %rdi
movl %eax, %esi
callq 0x1b250
movl %eax, 0x6c(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x84(%rsp), %esi
callq 0x1b440
movq %rax, 0x40(%rsp)
movq 0xb0(%rsp), %rdi
callq 0xf510
xorq $-0x1, %rax
movq 0x70(%rsp), %rcx
xorq $-0x1, %rcx
andq %rcx, %rax
movq %rax, 0x38(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x6c(%rsp), %esi
callq 0x1b440
movq 0x38(%rsp), %rcx
movq %rax, %rdx
movq 0x40(%rsp), %rax
orq %rdx, %rcx
andq %rcx, %rax
movq %rax, 0xa0(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x94(%rsp)
cmpq $0x0, 0xa0(%rsp)
je 0x2fd0b
movl 0x94(%rsp), %eax
movl %eax, 0x2c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xd940
movl 0x2c(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0xa8(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0xa8(%rsp)
movl %ecx, (%rax)
jmp 0x2fcc0
cmpl $0x1, 0x8c(%rsp)
jg 0x2fd37
movl 0x90(%rsp), %edi
movl 0x84(%rsp), %esi
callq 0x339d0
orq 0x78(%rsp), %rax
movq %rax, 0x98(%rsp)
jmp 0x2fd43
movq $0x0, 0x98(%rsp)
movl 0x88(%rsp), %edi
callq 0x122b0
movl %eax, 0x68(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x68(%rsp), %esi
callq 0x1b250
movslq %eax, %rcx
leaq 0x2ab32(%rip), %rax     # 0x5a8a0
movq (%rax,%rcx,8), %rax
movq %rax, 0x20(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x68(%rsp), %esi
callq 0x298c0
movq %rax, %rcx
movq 0x20(%rsp), %rax
andq %rcx, %rax
orq 0x98(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x68(%rsp), %esi
callq 0x298c0
movq %rax, 0x60(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xd940
movl %eax, 0x90(%rsp)
movq 0xb0(%rsp), %rdi
movl 0x90(%rsp), %esi
callq 0x11f10
cmpl $0x1, %eax
jne 0x2fdfe
movq $0x0, 0x60(%rsp)
jmp 0x2fe28
movslq 0x90(%rsp), %rcx
leaq 0x2aa93(%rip), %rax     # 0x5a8a0
movq (%rax,%rcx,8), %rax
andq 0x60(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x2fe1d
cmpq $0x0, 0xa0(%rsp)
jne 0x2fdc6
movq 0x60(%rsp), %rax
orq 0x98(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movl 0x88(%rsp), %ecx
movq 0x98(%rsp), %rax
movq %rsi, 0x1b0(%rsp)
movq %rdx, 0x1a8(%rsp)
movl %ecx, 0x1a4(%rsp)
movq %rax, 0x198(%rsp)
cmpl $0x0, 0x1a4(%rsp)
jne 0x2feae
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
movq 0x198(%rsp), %rdx
movl $0x40, %ecx
callq 0x31a50
movq %rax, 0x18(%rsp)
jmp 0x2fed5
movq 0x1b0(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
movq 0x198(%rsp), %rdx
movl $0x40, %ecx
callq 0x31dd0
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movl 0x88(%rsp), %ecx
movq 0x98(%rsp), %rax
movq %rsi, 0xe8(%rsp)
movq %rdx, 0xe0(%rsp)
movl %ecx, 0xdc(%rsp)
movq %rax, 0xd0(%rsp)
movq 0xe8(%rsp), %rdi
movl 0xdc(%rsp), %esi
movl $0x2, %edx
callq 0x299d0
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
cmpl $0x40, (%rax)
je 0x2fff7
jmp 0x2ff54
movq 0xb8(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xc4(%rsp)
movq 0xe8(%rsp), %rdi
movl 0xc4(%rsp), %esi
callq 0x29aa0
andq 0xd0(%rsp), %rax
movq %rax, 0xc8(%rsp)
cmpq $0x0, 0xc8(%rsp)
je 0x2ffd4
movl 0xc4(%rsp), %eax
movl %eax, 0x14(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0xd940
movl 0x14(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0xe0(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0xe0(%rsp)
movl %ecx, (%rax)
jmp 0x2ff89
movq 0xb8(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0xb8(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x2ff54
jmp 0x2fff7
movq 0xe0(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movl 0x88(%rsp), %ecx
movq 0x98(%rsp), %rax
movq %rsi, 0x120(%rsp)
movq %rdx, 0x118(%rsp)
movl %ecx, 0x114(%rsp)
movq %rax, 0x108(%rsp)
movq 0x120(%rsp), %rdi
movl 0x114(%rsp), %esi
movl $0x3, %edx
callq 0x299d0
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
cmpl $0x40, (%rax)
je 0x3011c
jmp 0x30079
movq 0xf0(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xfc(%rsp)
movq 0x120(%rsp), %rdi
movl 0xfc(%rsp), %esi
callq 0x1b550
andq 0x108(%rsp), %rax
movq %rax, 0x100(%rsp)
cmpq $0x0, 0x100(%rsp)
je 0x300f9
movl 0xfc(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0x100(%rsp), %rdi
callq 0xd940
movl 0x10(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0x118(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x118(%rsp)
movl %ecx, (%rax)
jmp 0x300ae
movq 0xf0(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0xf0(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x30079
jmp 0x3011c
movq 0x118(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movl 0x88(%rsp), %ecx
movq 0x98(%rsp), %rax
movq %rsi, 0x158(%rsp)
movq %rdx, 0x150(%rsp)
movl %ecx, 0x14c(%rsp)
movq %rax, 0x140(%rsp)
movq 0x158(%rsp), %rdi
movl 0x14c(%rsp), %esi
movl $0x4, %edx
callq 0x299d0
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rax
cmpl $0x40, (%rax)
je 0x30241
jmp 0x3019e
movq 0x128(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x134(%rsp)
movq 0x158(%rsp), %rdi
movl 0x134(%rsp), %esi
callq 0x29d40
andq 0x140(%rsp), %rax
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
je 0x3021e
movl 0x134(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x138(%rsp), %rdi
callq 0xd940
movl 0xc(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0x150(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x150(%rsp)
movl %ecx, (%rax)
jmp 0x301d3
movq 0x128(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0x128(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x3019e
jmp 0x30241
movq 0x150(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movl 0x88(%rsp), %ecx
movq 0x98(%rsp), %rax
movq %rsi, 0x190(%rsp)
movq %rdx, 0x188(%rsp)
movl %ecx, 0x184(%rsp)
movq %rax, 0x178(%rsp)
movq 0x190(%rsp), %rdi
movl 0x184(%rsp), %esi
movl $0x5, %edx
callq 0x299d0
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rax
cmpl $0x40, (%rax)
je 0x30366
jmp 0x302c3
movq 0x160(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x16c(%rsp)
movq 0x190(%rsp), %rdi
movl 0x16c(%rsp), %esi
callq 0x29cf0
andq 0x178(%rsp), %rax
movq %rax, 0x170(%rsp)
cmpq $0x0, 0x170(%rsp)
je 0x30343
movl 0x16c(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x170(%rsp), %rdi
callq 0xd940
movl 0x8(%rsp), %edi
movl %eax, %esi
callq 0xf810
movl %eax, %ecx
movq 0x188(%rsp), %rax
movq %rax, %rdx
addq $0x8, %rdx
movq %rdx, 0x188(%rsp)
movl %ecx, (%rax)
jmp 0x302f8
movq 0x160(%rsp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, 0x160(%rsp)
cmpl $0x40, 0x4(%rax)
jne 0x302c3
jmp 0x30366
movq 0x188(%rsp), %rax
addq $0x1b8, %rsp            # imm = 0x1B8
retq
nopw %cs:(%rax,%rax)