// File: /workspace/github_cpp_makefile_star2_O1/ATOMIC-LICHESS[P]ATOMKRAFT/position.cpp
// Function: Position::move_gives_check(Move, CheckInfo const&) const
bool Position::move_gives_check(Move m, const CheckInfo& ci) const {

  assert(is_ok());
  assert(move_is_ok(m));
  assert(ci.dcCandidates == discovered_check_candidates(side_to_move()));
  assert(color_of_piece_on(move_from(m)) == side_to_move());
  assert(piece_on(ci.ksq) == make_piece(opposite_color(side_to_move()), KING));

  Square from = move_from(m);
  Square to = move_to(m);
  PieceType pt = type_of_piece_on(from);
  
  NEW int is_capture = type_of_piece_on(to) || move_is_ep(m);
  
  NEW // direct check is not possible if the move is a capture since the piece explodes!
  NEW // there are of course discovery checks where the move is a capture
  NEW // but this case is handled in the new code
  NEW if (!is_capture) {
	  
	  STARTNEW
	  // there are some extra rules for kings
	  if (pt == KING) {
		  
		  Color us = side_to_move();
		  Color them = opposite_color(us);
		  assert(piece_count(them, KING));
		  Square opp_ksq = king_square(them);
		  
		  // moving the king such that it faces the opponent king directly never results in check
		  if (squares_touch[to][opp_ksq]) {
			  return false;
		  }
		  
		  // if kings touch before the move but not afterwards there may be check
		  if (squares_touch[from][opp_ksq]) {
			  if (squares_touch[to][opp_ksq] == 0) {
				  // this is attackers_to(opp_ksq);
				  // it is ok that we compute that here since we 
				  // saved time not initializing checkSq[...] as kings touch
				  
				  // first we need to update the king in occupied squares
				  // since it may be possible (e.g. kings adjacent) for a king to move along 
				  // an x-ray of an opponent sliding piece and we couldn't detect it
				  Bitboard occ = occupied_squares();
				  do_move_bb(&occ, make_move_bb(from, to));
				  
			      Bitboard att_to = 
			    		    (attacks_from<PAWN>(opp_ksq, BLACK) & pieces(PAWN, WHITE))
			              | (attacks_from<PAWN>(opp_ksq, WHITE) & pieces(PAWN, BLACK))
			              | (attacks_from<KNIGHT>(opp_ksq)      & pieces(KNIGHT))
			              | (rook_attacks_bb(opp_ksq, occ)      & pieces(ROOK, QUEEN))
			              | (bishop_attacks_bb(opp_ksq, occ)    & pieces(BISHOP, QUEEN));
			      
				  att_to &= pieces_of_color(us);
				  
				  if (att_to) {
					  return true;
				  }
			  } 		  
		  }
	  }
	  ENDNEW
	  
	  // Direct check ?
	  if (bit_is_set(ci.checkSq[pt], to)) {
		  return true;
	  }
	
	  // Discovery check ?
	  if (ci.dcCandidates && bit_is_set(ci.dcCandidates, from))
	  {
		  // For pawn and king moves we need to verify also direction
		  if (  (pt != PAWN && pt != KING)
			  || !squares_aligned(from, to, ci.ksq)) {
			  return true;
		  }
	  }
	  
  NEW }
  
  STARTNEW
  // Explosion with check?
  // almost the same code is used in Position::pl_move_is_legal
  Color us = side_to_move();
  Bitboard b = occupied_squares();
  
  if (is_capture) {	// is capture move?
	  // get pawns which don't explode
	  Bitboard notexpl_pawns_bb = pieces(PAWN);
	  clear_bit(&notexpl_pawns_bb, from);	// the from-pawn explodes always
	  
	  if (move_is_ep(m)) {
		  // the captured en passant piece explodes
		  clear_bit(&notexpl_pawns_bb, make_square(square_file(to), square_rank(from)));
	  } else {
		  clear_bit(&notexpl_pawns_bb, to);	// the to-pawn explodes
	  }
	  
	  clear_bit(&b, from);		// clear the from bit
	  b &= ~explBB[to];			// clear all bits which are involved in the capture/explosion
	  b |= notexpl_pawns_bb;	// and restore pawns
	  
	  // test if own pieces now give check  
	  // the code differs slightly from the en passant code    �������|���|
	  //                                                              V   |
      return  (rook_attacks_bb(ci.ksq, b) & pieces(ROOK, QUEEN, us) & b)//V
            ||(bishop_attacks_bb(ci.ksq, b) & pieces(BISHOP, QUEEN, us) & b);
  }
  ENDNEW
  
  
  // Can we skip the ugly special cases ?
  if (!move_is_special(m))
      return false;

  OLD Color us = side_to_move();
  OLD Bitboard b = occupied_squares();

  // Promotion with check ?
  OLD if (move_is_promotion(m)) {}
  NEW if (move_is_promotion(m) && !is_capture)	// captures are already handled
  {
      clear_bit(&b, from);

      switch (move_promotion_piece(m))
      {
      case KNIGHT:
          return bit_is_set(attacks_from<KNIGHT>(to), ci.ksq);
      case BISHOP:
          return bit_is_set(bishop_attacks_bb(to, b), ci.ksq);
      case ROOK:
          return bit_is_set(rook_attacks_bb(to, b), ci.ksq);
      case QUEEN:
          return bit_is_set(queen_attacks_bb(to, b), ci.ksq);
      default:
          assert(false);
          break;
      }
  }
  
  NEW // this is a special case of the new code above 
  STARTOLD
  // En passant capture with check ? We have already handled the case
  // of direct checks and ordinary discovered check, the only case we
  // need to handle is the unusual case of a discovered check through
  // the captured pawn.
  if (move_is_ep(m))
  {
      Square capsq = make_square(square_file(to), square_rank(from));
      clear_bit(&b, from);
      clear_bit(&b, capsq);
      set_bit(&b, to);
      return  (rook_attacks_bb(ci.ksq, b) & pieces(ROOK, QUEEN, us))
            ||(bishop_attacks_bb(ci.ksq, b) & pieces(BISHOP, QUEEN, us));
  }
  ENDOLD

  // Castling with check ?
  if (move_is_castle(m))
  {
      Square kfrom, kto, rfrom, rto;
      kfrom = from;
      rfrom = to;

      if (rfrom > kfrom)
      {
          kto = relative_square(us, SQ_G1);
          rto = relative_square(us, SQ_F1);
      } else {
          kto = relative_square(us, SQ_C1);
          rto = relative_square(us, SQ_D1);
      }
      clear_bit(&b, kfrom);
      clear_bit(&b, rfrom);
      set_bit(&b, rto);
      set_bit(&b, kto);
      return bit_is_set(rook_attacks_bb(rto, b), ci.ksq);
  }

  return false;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r12
movl %esi, %r10d
shrl $0x6, %r10d
movl %r10d, %edx
andl $0x3f, %edx
movl %esi, %r9d
andl $0x3f, %r9d
testb $0x7, (%rdi,%r9,4)
setne %al
movl %esi, %r11d
andl $0xc000, %r11d          # imm = 0xC000
cmpl $0x8000, %r11d          # imm = 0x8000
sete %bpl
orb %al, %bpl
je 0x1eb4b
movl 0x1090(%rdi), %eax
movq 0x100(%rdi), %r8
testb %bpl, %bpl
je 0x1ed65
leaq 0xf997e(%rip), %rcx     # 0x1183b0
andl $0x7, %esi
andl $0x38, %r10d
orl %esi, %r10d
cmpl $0x8000, %r11d          # imm = 0x8000
cmovnel %r9d, %r10d
movq 0x108(%rdi), %r11
movq 0x128(%rdi), %rsi
andq (%rcx,%r10,8), %r11
leaq 0x13460(%rip), %r10     # 0x31ec0
movq (%r10,%r9,8), %r9
notq %r9
andq %r9, %r8
orq %r11, %r8
andq (%rcx,%rdx,8), %r8
movslq 0x48(%r12), %rdx
leaq 0x26d23(%rip), %rcx     # 0x457a0
movq (%rcx,%rdx,8), %rcx
andq %r8, %rcx
leaq 0xfab5(%rip), %r9       # 0x2e540
movq (%r9,%rdx,8), %r9
movl %r9d, %r10d
imull %ecx, %r10d
shrq $0x20, %rcx
shrq $0x20, %r9
imull %ecx, %r9d
xorl %r10d, %r9d
leaq 0xfd94(%rip), %rcx      # 0x2e840
movb (%rcx,%rdx,4), %cl
shrl %cl, %r9d
leaq 0x26ee7(%rip), %rcx     # 0x459a0
addl (%rcx,%rdx,4), %r9d
leaq 0x26fdc(%rip), %rcx     # 0x45aa0
movq 0x120(%rdi), %r10
orq %rsi, %r10
andq (%rcx,%r9,8), %r10
movq 0x140(%rdi,%rax,8), %r9
andq %r8, %r10
movb $0x1, %al
testq %r9, %r10
jne 0x1ee9e
leaq 0xeefb1(%rip), %rax     # 0x10daa0
movq (%rax,%rdx,8), %rax
andq %r8, %rax
leaq 0xf843(%rip), %rcx      # 0x2e340
movq (%rcx,%rdx,8), %r10
movl %r10d, %ecx
imull %eax, %ecx
shrq $0x20, %rax
shrq $0x20, %r10
imull %eax, %r10d
xorl %ecx, %r10d
leaq 0xfc23(%rip), %rax      # 0x2e740
movb (%rax,%rdx,4), %cl
shrl %cl, %r10d
leaq 0xef176(%rip), %rax     # 0x10dca0
addl (%rax,%rdx,4), %r10d
leaq 0xef26b(%rip), %rax     # 0x10dda0
orq 0x118(%rdi), %rsi
andq (%rax,%r10,8), %rsi
andq %r8, %rsi
testq %r9, %rsi
jmp 0x1ee9b
movl (%rdi,%rdx,4), %r8d
andl $0x7, %r8d
cmpl $0x6, %r8d
jne 0x1ecdd
movl 0x1090(%rdi), %ebx
movq %rbx, %rax
xorq $0x1, %rax
shlq $0x9, %rax
movl %r9d, %ecx
shll $0x6, %ecx
addq 0x1d3fd(%rip), %rcx     # 0x3bf78
movslq 0x310(%rdi,%rax), %r15
cmpb $0x0, (%r15,%rcx)
sete %al
jne 0x1ee9e
movl %edx, %eax
shll $0x6, %eax
addq 0x1d3db(%rip), %rax     # 0x3bf78
cmpb $0x0, (%r15,%rax)
je 0x1ecdd
leaq 0xf95f1(%rip), %rcx     # 0x1181a0
movq (%rcx,%r9,8), %rax
orq (%rcx,%rdx,8), %rax
xorq 0x100(%rdi), %rax
leaq 0xf9b8b(%rip), %rcx     # 0x118750
movq %r12, 0x10(%rsp)
movq 0x140(%rdi), %r12
andq 0x1200(%rcx,%r15,8), %r12
movq 0x148(%rdi), %r14
andq 0x200(%rcx,%r15,8), %r14
orq %r12, %r14
andq 0x108(%rdi), %r14
movq 0x110(%rdi), %r13
andq 0x400(%rcx,%r15,8), %r13
leaq 0x26b98(%rip), %rcx     # 0x457a0
movq (%rcx,%r15,8), %rcx
andq %rax, %rcx
leaq 0xf92a(%rip), %r12      # 0x2e540
movq %rbx, 0x8(%rsp)
movq (%r12,%r15,8), %rbx
movl %ebx, %r12d
imull %ecx, %r12d
shrq $0x20, %rcx
shrq $0x20, %rbx
imull %ecx, %ebx
xorl %r12d, %ebx
leaq 0xfc05(%rip), %rcx      # 0x2e840
movb (%rcx,%r15,4), %cl
shrl %cl, %ebx
movq 0x128(%rdi), %r12
leaq 0x26d51(%rip), %rcx     # 0x459a0
addl (%rcx,%r15,4), %ebx
orq %r14, %r13
leaq 0x26e43(%rip), %rcx     # 0x45aa0
movq 0x120(%rdi), %r14
orq %r12, %r14
andq (%rcx,%rbx,8), %r14
orq %r13, %r14
leaq 0xeee2b(%rip), %rcx     # 0x10daa0
andq (%rcx,%r15,8), %rax
leaq 0xf6c0(%rip), %rcx      # 0x2e340
movq (%rcx,%r15,8), %rbx
movl %ebx, %ecx
imull %eax, %ecx
shrq $0x20, %rax
shrq $0x20, %rbx
imull %eax, %ebx
xorl %ecx, %ebx
leaq 0xfaa3(%rip), %rax      # 0x2e740
movb (%rax,%r15,4), %cl
shrl %cl, %ebx
leaq 0xeeff6(%rip), %rax     # 0x10dca0
addl (%rax,%r15,4), %ebx
leaq 0xef0eb(%rip), %rax     # 0x10dda0
orq 0x118(%rdi), %r12
andq (%rax,%rbx,8), %r12
orq %r14, %r12
movb $0x1, %al
movq 0x8(%rsp), %rcx
testq %r12, 0x140(%rdi,%rcx,8)
movq 0x10(%rsp), %r12
jne 0x1ee9e
leaq 0xf94bc(%rip), %rcx     # 0x1181a0
movq (%rcx,%r9,8), %rbx
movb $0x1, %al
testq %rbx, 0x8(%r12,%r8,8)
jne 0x1ee9e
movq (%r12), %r14
testq %r14, %r14
je 0x1ea15
testq %r14, (%rcx,%rdx,8)
je 0x1ea15
cmpl $0x6, %r8d
je 0x1ed1c
cmpl $0x1, %r8d
jne 0x1ee9e
movslq 0x48(%r12), %r8
movl %edx, %ecx
shll $0x9, %ecx
leaq 0xfba23(%rip), %rbx     # 0x11a750
addq %rbx, %rcx
movq (%rcx,%r8,8), %r14
orq (%rcx,%r9,8), %r14
movl %r9d, %ecx
shll $0x9, %ecx
addq %rbx, %rcx
orq (%rcx,%r8,8), %r14
movl $0x1, %ebx
movl %edx, %ecx
shlq %cl, %rbx
btsq %r9, %rbx
btsq %r8, %rbx
testq %rbx, %r14
jne 0x1ea15
jmp 0x1ee9e
movl %esi, %ecx
andl $0xc000, %ecx           # imm = 0xC000
je 0x1ee62
cmpl $0x4000, %ecx           # imm = 0x4000
jne 0x1edc0
leaq 0xf962e(%rip), %rax     # 0x1183b0
andq (%rax,%rdx,8), %r8
shrl $0xc, %esi
andl $0x3, %esi
leaq 0x12f1d(%rip), %rax     # 0x31cb0
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0xf99ad(%rip), %rax     # 0x118750
movslq 0x48(%r12), %rcx
leaq 0xf93f1(%rip), %rdx     # 0x1181a0
movq (%rdx,%rcx,8), %rcx
testq %rcx, 0x400(%rax,%r9,8)
jmp 0x1ee9b
cmpl $0xc000, %ecx           # imm = 0xC000
jne 0x1ee62
xorl %esi, %esi
cmpl %edx, %r9d
seta %sil
imull $0x38, %eax, %edi
leaq 0xf95d1(%rip), %rax     # 0x1183b0
movq (%rax,%rdx,8), %rcx
andq (%rax,%r9,8), %rcx
leal (%rdi,%rsi,2), %eax
addl $0x3, %eax
andq %r8, %rcx
movslq %eax, %rdx
leaq 0xf93a6(%rip), %rax     # 0x1181a0
orq (%rax,%rdx,8), %rcx
leal (%rdi,%rsi,4), %esi
addl $0x2, %esi
movslq %esi, %rsi
orq (%rax,%rsi,8), %rcx
leaq 0x2698e(%rip), %rsi     # 0x457a0
andq (%rsi,%rdx,8), %rcx
leaq 0x26b83(%rip), %rsi     # 0x459a0
leaq 0xf71c(%rip), %rdi      # 0x2e540
movq (%rdi,%rdx,8), %rdi
movl %edi, %r8d
imull %ecx, %r8d
shrq $0x20, %rcx
shrq $0x20, %rdi
imull %ecx, %edi
xorl %r8d, %edi
leaq 0xf9fc(%rip), %rcx      # 0x2e840
movb (%rcx,%rdx,4), %cl
shrl %cl, %edi
addl (%rsi,%rdx,4), %edi
leaq 0x26c4d(%rip), %rcx     # 0x45aa0
movslq 0x48(%r12), %rdx
movq (%rax,%rdx,8), %rax
testq %rax, (%rcx,%rdi,8)
jmp 0x1ee9b
xorl %eax, %eax
jmp 0x1ee9e
movl %r9d, %edi
movq %r8, %rsi
callq 0x210eb
jmp 0x1ee8b
movl %r9d, %edi
movq %r8, %rsi
callq 0x21179
jmp 0x1ee8b
movl %r9d, %edi
movq %r8, %rsi
callq 0x2109e
movslq 0x48(%r12), %rcx
leaq 0xf9309(%rip), %rdx     # 0x1181a0
testq %rax, (%rdx,%rcx,8)
setne %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop