// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl 0x1090(%rdi), %ecx
leaq 0x190(%rdi), %rax
movq %rax, 0x40(%rsp)
movq %rcx, 0x20(%rsp)
movq %rcx, %rax
shlq $0x9, %rax
movq %rax, 0x30(%rsp)
movslq 0x310(%rdi,%rax), %rcx
movq 0x1938(%rdi), %rax
movq 0x40(%rax), %rax
leaq 0x28(%rsp), %r12
movq %rax, 0x48(%rsp)
movq %rax, (%r12)
pushq $0x1
popq %rdx
movq %rcx, 0x18(%rsp)
shlq %cl, %rdx
movq %rdx, 0x10(%rsp)
pushq $0x1
popq %rbp
xorl %r15d, %r15d
movq %rdi, 0x8(%rsp)
movq %r12, %rdi
callq 0x7b62
movslq %eax, %r13
movl (%r14,%r13,4), %ecx
andl $0x7, %ecx
cmpl $0x5, %ecx
je 0x14c89
cmpl $0x4, %ecx
je 0x14c7c
cmpl $0x3, %ecx
jne 0x14ce0
leaq 0x1076d6(%rip), %rax    # 0x11c350
jmp 0x14c83
leaq 0x1078cd(%rip), %rax    # 0x11c550
orq (%rax,%r13,8), %r15
jmp 0x14ce0
leaq 0x1078c0(%rip), %rcx    # 0x11c550
movq (%rcx,%r13,8), %r14
testq %r14, 0x10(%rsp)
je 0x14cb5
movq 0x8(%rsp), %r12
movq %r12, %rdi
movl %eax, %esi
callq 0xcf20
orq %rax, %r15
orq %r14, %r15
movq %r12, %r14
jmp 0x14cdb
leaq 0x107694(%rip), %rcx    # 0x11c350
movq (%rcx,%r13,8), %r14
movq 0x8(%rsp), %r12
movq %r12, %rdi
movl %eax, %esi
callq 0x12330
orq %r15, %rax
orq %r14, %rax
movq %r12, %r14
movq %rax, %r15
leaq 0x28(%rsp), %r12
decl %ebp
cmpq $0x0, 0x28(%rsp)
jne 0x14c52
movq 0x20(%rsp), %rax
xorl $0x1, %eax
movq %rax, 0x10(%rsp)
shlq $0x9, %rax
movq 0x40(%rsp), %rcx
addq %rcx, %rax
addq $0x180, %rax            # imm = 0x180
movq %rax, 0x50(%rsp)
movslq (%rax), %rax
leaq 0xfca34(%rip), %rcx     # 0x111750
orq 0x100(%r14), %r15
notq %r15
orq 0xc00(%rcx,%rax,8), %r15
movq 0x18(%rsp), %rax
andq 0xc00(%rcx,%rax,8), %r15
leaq 0x28(%rsp), %r12
movq %r15, (%r12)
movl %eax, %r14d
shll $0x6, %r14d
testq %r15, %r15
je 0x14d68
movq %r12, %rdi
callq 0x7b62
orl %r14d, %eax
movl %eax, (%rbx)
addq $0x8, %rbx
movq 0x28(%rsp), %r15
jmp 0x14d4b
testl %ebp, %ebp
je 0x14d7b
xorl %r12d, %r12d
movq 0x8(%rsp), %r14
movq 0x48(%rsp), %rcx
jmp 0x14d9d
shlq $0x9, %r13
addq 0x20222(%rip), %r13     # 0x34fa8
movq 0x18(%rsp), %rax
movq (%r13,%rax,8), %r12
movq 0x48(%rsp), %rcx
orq %rcx, %r12
movq 0x8(%rsp), %r14
movq 0x50(%rsp), %rax
movslq (%rax), %rax
leaq 0x14864(%rip), %r13     # 0x29610
movq (%r13,%rax,8), %rax
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq 0x140(%r14,%rax,8), %rbp
leaq 0x28(%rsp), %r15
movq %rcx, (%r15)
movq %rbp, 0x8(%rsp)
movq %r15, %rdi
callq 0x7b62
cltq
movl (%r14,%rax,4), %ecx
andl $0x7, %ecx
cmpl $0x1, %ecx
je 0x14df5
andq (%r13,%rax,8), %rbp
cmpq $0x0, 0x28(%rsp)
jne 0x14dd0
jmp 0x14df7
xorl %ebp, %ebp
movabsq $0xffffffffffff00, %r13 # imm = 0xFFFFFFFFFFFF00
movq 0x8(%rsp), %rax
andq 0x18(%rsp), %rax
orq %r12, %rax
orq %rbp, %rax
cmpq $0x0, 0x20(%rsp)
movq %rax, 0x8(%rsp)
je 0x1504c
movq 0x100(%r14), %r12
movq 0x148(%r14), %rbp
andq 0x108(%r14), %rbp
notq %r12
movq %rbp, %rsi
shrq $0x8, %rsi
movq %rax, %rdx
andq %r12, %rdx
movq 0x140(%r14), %rcx
andq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq %rbp, %rcx
andq $0xff00, %rcx           # imm = 0xFF00
movq %rdx, 0x20(%rsp)
je 0x14ec8
movq %rsi, 0x18(%rsp)
shrl $0x8, %ecx
movl %edx, %eax
andl %ecx, %eax
movq %rsp, %r15
movq %rax, (%r15)
testq %rax, %rax
je 0x14ec3
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0x200, %ecx            # imm = 0x200
orl %eax, %ecx
movl %ecx, %eax
orl $0x7000, %eax           # imm = 0x7000
movl %eax, (%rbx)
movl %ecx, %eax
orl $0x6000, %eax           # imm = 0x6000
movl %eax, 0x8(%rbx)
movl %ecx, %eax
orl $0x5000, %eax           # imm = 0x5000
movl %eax, 0x10(%rbx)
orl $0x4000, %ecx           # imm = 0x4000
movl %ecx, 0x18(%rbx)
addq $0x20, %rbx
movq (%rsp), %rax
jmp 0x14e79
movq 0x18(%rsp), %rsi
andq %rsi, %r13
andq %rsi, %r12
movq %rbp, %rcx
shrq $0x7, %rcx
andq 0x10(%rsp), %rcx
movabsq $0xfefefefefefefe, %rax # imm = 0xFEFEFEFEFEFEFE
andq %rcx, %rax
movq %rsp, %r15
movq %rax, (%r15)
testq %rax, %rax
je 0x14f13
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0x1c0, %ecx            # imm = 0x1C0
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x14eed
movq %rbp, %rax
shrq $0x9, %rax
movq 0x10(%rsp), %rcx
andq %rax, %rcx
movabsq $0x7f7f7f7f7f7f7f, %rax # imm = 0x7F7F7F7F7F7F7F
andq %rcx, %rax
movq %rsp, %r15
movq %rax, (%r15)
testq %rax, %rax
je 0x14f5b
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0x240, %ecx            # imm = 0x240
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x14f35
movq 0x20(%rsp), %rax
andq %rax, %r13
movq %rsp, %r15
movq %r13, (%r15)
shrq $0x8, %r12
andq %rax, %r12
movq %r12, %rax
movabsq $0xff00000000, %r12     # imm = 0xFF00000000
andq %rax, %r12
movq %r12, 0x38(%rsp)
testq %r13, %r13
je 0x14fab
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0x200, %ecx            # imm = 0x200
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq (%rsp), %r13
jmp 0x14f85
leaq 0x38(%rsp), %r15
movq 0x40(%rsp), %r13
testq %r12, %r12
je 0x14fdc
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0x400, %ecx            # imm = 0x400
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq 0x38(%rsp), %r12
jmp 0x14fb5
movq 0x1938(%r14), %rax
movslq 0x20(%rax), %rax
cmpq $0x40, %rax
je 0x1526d
leaq 0xfc1a8(%rip), %rcx     # 0x1111a0
movq 0x8(%rsp), %rdx
testq %rdx, 0x40(%rcx,%rax,8)
je 0x1526d
leaq 0xfc741(%rip), %rcx     # 0x111750
andq 0x200(%rcx,%rax,8), %rbp
movq %rsp, %r15
movq %rbp, (%r15)
testq %rbp, %rbp
je 0x1526d
movq %r15, %rdi
callq 0x7b62
movq 0x1938(%r14), %rcx
shll $0x6, %eax
orl 0x20(%rcx), %eax
orl $0x8000, %eax           # imm = 0x8000
movl %eax, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rbp
jmp 0x1501d
movq 0x100(%r14), %rcx
movq 0x140(%r14), %rbp
andq 0x108(%r14), %rbp
notq %rcx
movq %rbp, %rsi
shlq $0x8, %rsi
movq %rax, %r12
andq %rcx, %r12
movq 0x148(%r14), %rdx
andq %rax, %rdx
movq %rdx, 0x20(%rsp)
movabsq $0xff000000000000, %rax # imm = 0xFF000000000000
andq %rbp, %rax
je 0x150fa
movq %rsi, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
shlq $0x8, %rax
andq %r12, %rax
movq %rsp, %r15
movq %rax, (%r15)
testq %rax, %rax
je 0x150f0
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0xfffffe00, %ecx       # imm = 0xFFFFFE00
orl %eax, %ecx
movl %ecx, %eax
orl $0x7000, %eax           # imm = 0x7000
movl %eax, (%rbx)
movl %ecx, %eax
orl $0x6000, %eax           # imm = 0x6000
movl %eax, 0x8(%rbx)
movl %ecx, %eax
orl $0x5000, %eax           # imm = 0x5000
movl %eax, 0x10(%rbx)
orl $0x4000, %ecx           # imm = 0x4000
movl %ecx, 0x18(%rbx)
addq $0x20, %rbx
movq (%rsp), %rax
jmp 0x150a6
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
andq %rsi, %r13
andq %rsi, %rcx
movq %rcx, 0x10(%rsp)
movq %rbp, %rcx
shlq $0x9, %rcx
andq 0x20(%rsp), %rcx
movabsq $-0x101010101010200, %rax # imm = 0xFEFEFEFEFEFEFE00
andq %rcx, %rax
movq %rsp, %r15
movq %rax, (%r15)
testq %rax, %rax
je 0x1514a
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0xfffffdc0, %ecx       # imm = 0xFFFFFDC0
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x15124
movq %rbp, %rax
shlq $0x7, %rax
movq 0x20(%rsp), %rcx
andq %rax, %rcx
movabsq $0x7f7f7f7f7f7f7f00, %rax # imm = 0x7F7F7F7F7F7F7F00
andq %rcx, %rax
movq %rsp, %r15
movq %rax, (%r15)
testq %rax, %rax
je 0x15192
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0xfffffe40, %ecx       # imm = 0xFFFFFE40
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x1516c
andq %r12, %r13
movq %rsp, %r15
movq %r13, (%r15)
movq 0x10(%rsp), %rax
shll $0x8, %eax
andl %eax, %r12d
andl $0xff000000, %r12d      # imm = 0xFF000000
movq %r12, 0x38(%rsp)
testq %r13, %r13
je 0x151d8
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0xfffffe00, %ecx       # imm = 0xFFFFFE00
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq (%rsp), %r13
jmp 0x151b2
leaq 0x38(%rsp), %r15
movq 0x40(%rsp), %r13
testq %r12, %r12
je 0x15209
movq %r15, %rdi
callq 0x7b62
movl %eax, %ecx
shll $0x6, %ecx
addl $0xfffffc00, %ecx       # imm = 0xFFFFFC00
orl %eax, %ecx
movl %ecx, (%rbx)
addq $0x8, %rbx
movq 0x38(%rsp), %r12
jmp 0x151e2
movq 0x1938(%r14), %rax
movslq 0x20(%rax), %rax
cmpq $0x40, %rax
je 0x1526d
leaq 0xfbf7f(%rip), %rcx     # 0x1111a0
movq 0x8(%rsp), %rdx
testq %rdx, -0x40(%rcx,%rax,8)
je 0x1526d
leaq 0xfc51c(%rip), %rcx     # 0x111750
andq 0x1200(%rcx,%rax,8), %rbp
movq %rsp, %r15
movq %rbp, (%r15)
testq %rbp, %rbp
je 0x1526d
movq %r15, %rdi
callq 0x7b62
movq 0x1938(%r14), %rcx
shll $0x6, %eax
orl 0x20(%rcx), %eax
orl $0x8000, %eax           # imm = 0x8000
movl %eax, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rbp
jmp 0x15242
movq 0x30(%rsp), %rcx
leaq 0xfc4d7(%rip), %rbp     # 0x111750
movl 0x80(%r13,%rcx), %eax
cmpl $0x40, %eax
je 0x152d6
leaq (%rcx,%r13), %r12
addq $0x80, %r12
movq %rsp, %r15
movslq %eax, %r13
movq 0x400(%rbp,%r13,8), %rax
andq 0x8(%rsp), %rax
movq %rax, (%rsp)
shll $0x6, %r13d
testq %rax, %rax
je 0x152c8
movq %r15, %rdi
callq 0x7b62
orl %r13d, %eax
movl %eax, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x152ac
movl 0x4(%r12), %eax
addq $0x4, %r12
cmpl $0x40, %eax
jne 0x15294
movq 0x40(%rsp), %r13
movq 0x30(%rsp), %rax
movl 0xc0(%r13,%rax), %ebp
cmpl $0x40, %ebp
je 0x1533a
leaq (%rax,%r13), %r12
addq $0xc0, %r12
movq %rsp, %r15
movq %r14, %rdi
movl %ebp, %esi
callq 0xcf20
andq 0x8(%rsp), %rax
movq %rax, (%rsp)
shll $0x6, %ebp
testq %rax, %rax
je 0x1532c
movq %r15, %rdi
callq 0x7b62
orl %ebp, %eax
movl %eax, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x15311
movl 0x4(%r12), %ebp
addq $0x4, %r12
cmpl $0x40, %ebp
jne 0x152fb
movq 0x30(%rsp), %rax
movl 0x100(%r13,%rax), %ebp
cmpl $0x40, %ebp
je 0x15399
leaq (%rax,%r13), %r12
addq $0x100, %r12            # imm = 0x100
movq %rsp, %r15
movq %r14, %rdi
movl %ebp, %esi
callq 0x12330
andq 0x8(%rsp), %rax
movq %rax, (%rsp)
shll $0x6, %ebp
testq %rax, %rax
je 0x1538b
movq %r15, %rdi
callq 0x7b62
orl %ebp, %eax
movl %eax, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x15370
movl 0x4(%r12), %ebp
addq $0x4, %r12
cmpl $0x40, %ebp
jne 0x1535a
movq 0x30(%rsp), %rax
movl 0x140(%r13,%rax), %ebp
cmpl $0x40, %ebp
je 0x153fb
leaq (%rax,%r13), %r12
addq $0x140, %r12            # imm = 0x140
movq %rsp, %r15
movq 0x8(%rsp), %r13
movq %r14, %rdi
movl %ebp, %esi
callq 0x1230a
andq %r13, %rax
movq %rax, (%rsp)
shll $0x6, %ebp
testq %rax, %rax
je 0x153ed
movq %r15, %rdi
callq 0x7b62
orl %ebp, %eax
movl %eax, (%rbx)
addq $0x8, %rbx
movq (%rsp), %rax
jmp 0x153d2
movl 0x4(%r12), %ebp
addq $0x4, %r12
cmpl $0x40, %ebp
jne 0x153be
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq