// File: 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:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movl %esi, 0xe4(%rsp)
movq %rdx, 0xd8(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x70(%rsp)
movl 0xe4(%rsp), %edi
callq 0xf680
movl %eax, 0xd4(%rsp)
movl 0xe4(%rsp), %edi
callq 0xf690
movq 0x70(%rsp), %rdi
movl %eax, 0xd0(%rsp)
movl 0xd4(%rsp), %esi
callq 0x11f10
movq 0x70(%rsp), %rdi
movl %eax, 0xcc(%rsp)
movl 0xd0(%rsp), %esi
callq 0x11f10
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, 0x7f(%rsp)
jne 0x39b3d
movl 0xe4(%rsp), %edi
callq 0x2e360
cmpl $0x0, %eax
setne %al
movb %al, 0x7f(%rsp)
movb 0x7f(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0xc8(%rsp)
cmpl $0x0, 0xc8(%rsp)
jne 0x39e89
cmpl $0x6, 0xcc(%rsp)
jne 0x39de4
movq 0x70(%rsp), %rdi
callq 0xf5d0
movl %eax, 0xc4(%rsp)
movl 0xc4(%rsp), %edi
callq 0x122b0
movq 0x70(%rsp), %rdi
movl %eax, 0xc0(%rsp)
movl 0xc0(%rsp), %esi
callq 0x1b250
movl %eax, 0xbc(%rsp)
movslq 0xd0(%rsp), %rcx
leaq 0x33cec(%rip), %rax     # 0x6d8a0
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0xbc(%rsp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x39bd6
movb $0x0, 0xf7(%rsp)
jmp 0x3a2fe
movslq 0xd4(%rsp), %rcx
leaq 0x33cbb(%rip), %rax     # 0x6d8a0
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0xbc(%rsp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x39de2
movslq 0xd0(%rsp), %rcx
leaq 0x33c93(%rip), %rax     # 0x6d8a0
shlq $0x6, %rcx
addq %rcx, %rax
movslq 0xbc(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x39de0
movq 0x70(%rsp), %rdi
callq 0xf510
movq %rax, 0xb0(%rsp)
movl 0xd4(%rsp), %edi
movl 0xd0(%rsp), %esi
callq 0x3e630
movq %rax, %rsi
leaq 0xb0(%rsp), %rdi
callq 0x3e610
movq 0x70(%rsp), %rdi
movl 0xbc(%rsp), %esi
movl $0x1, %edx
callq 0x1b460
movq 0x70(%rsp), %rdi
movq %rax, 0x28(%rsp)
movl $0x1, %esi
xorl %edx, %edx
callq 0x16530
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movq 0x28(%rsp), %rax
andq %rcx, %rax
movq %rax, 0x38(%rsp)
movl 0xbc(%rsp), %esi
xorl %edx, %edx
callq 0x1b460
movq 0x70(%rsp), %rdi
movq %rax, 0x30(%rsp)
movl $0x1, %edx
movl %edx, %esi
callq 0x16530
movq 0x30(%rsp), %rcx
movq 0x70(%rsp), %rdi
movq %rax, %rdx
movq 0x38(%rsp), %rax
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, 0x48(%rsp)
movl 0xbc(%rsp), %esi
callq 0x29aa0
movq 0x70(%rsp), %rdi
movq %rax, 0x40(%rsp)
movl $0x2, %esi
callq 0x33a70
movq 0x40(%rsp), %rcx
movq %rax, %rdx
movq 0x48(%rsp), %rax
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, 0x58(%rsp)
movl 0xbc(%rsp), %edi
movq 0xb0(%rsp), %rsi
callq 0xe8c0
movq 0x70(%rsp), %rdi
movq %rax, 0x50(%rsp)
movl $0x4, %esi
movl $0x5, %edx
callq 0x33a90
movq 0x50(%rsp), %rcx
movq %rax, %rdx
movq 0x58(%rsp), %rax
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, 0x68(%rsp)
movl 0xbc(%rsp), %edi
movq 0xb0(%rsp), %rsi
callq 0xe820
movq 0x70(%rsp), %rdi
movq %rax, 0x60(%rsp)
movl $0x3, %esi
movl $0x5, %edx
callq 0x33a90
movq 0x60(%rsp), %rcx
movq 0x70(%rsp), %rdi
movq %rax, %rdx
movq 0x68(%rsp), %rax
andq %rdx, %rcx
orq %rcx, %rax
movq %rax, 0xa8(%rsp)
movl 0xc4(%rsp), %esi
callq 0x298c0
andq 0xa8(%rsp), %rax
movq %rax, 0xa8(%rsp)
cmpq $0x0, 0xa8(%rsp)
je 0x39dde
movb $0x1, 0xf7(%rsp)
jmp 0x3a2fe
jmp 0x39de0
jmp 0x39de2
jmp 0x39de4
movq 0xd8(%rsp), %rax
movl 0xcc(%rsp), %ecx
movq 0x8(%rax,%rcx,8), %rdi
movl 0xd0(%rsp), %esi
callq 0xd760
cmpq $0x0, %rax
je 0x39e17
movb $0x1, 0xf7(%rsp)
jmp 0x3a2fe
movq 0xd8(%rsp), %rax
cmpq $0x0, (%rax)
je 0x39e87
movq 0xd8(%rsp), %rax
movq (%rax), %rdi
movl 0xd4(%rsp), %esi
callq 0xd760
cmpq $0x0, %rax
je 0x39e87
cmpl $0x1, 0xcc(%rsp)
je 0x39e56
cmpl $0x6, 0xcc(%rsp)
jne 0x39e78
movl 0xd4(%rsp), %edi
movl 0xd0(%rsp), %esi
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %edx
callq 0x3e660
testb $0x1, %al
jne 0x39e85
movb $0x1, 0xf7(%rsp)
jmp 0x3a2fe
jmp 0x39e87
jmp 0x39e89
movq 0x70(%rsp), %rdi
callq 0xf5d0
movq 0x70(%rsp), %rdi
movl %eax, 0xa4(%rsp)
callq 0xf510
movq %rax, 0x98(%rsp)
cmpl $0x0, 0xc8(%rsp)
je 0x3a051
movq 0x70(%rsp), %rdi
movl $0x1, %esi
callq 0x33a70
movq %rax, 0x90(%rsp)
movl 0xd4(%rsp), %esi
leaq 0x90(%rsp), %rdi
callq 0x29eb0
movl 0xe4(%rsp), %edi
callq 0x2e360
cmpl $0x0, %eax
je 0x39f2e
movl 0xd0(%rsp), %edi
callq 0xce10
movl %eax, 0x24(%rsp)
movl 0xd4(%rsp), %edi
callq 0xce40
movl 0x24(%rsp), %edi
movl %eax, %esi
callq 0xd740
movl %eax, %esi
leaq 0x90(%rsp), %rdi
callq 0x29eb0
jmp 0x39f42
movl 0xd0(%rsp), %esi
leaq 0x90(%rsp), %rdi
callq 0x29eb0
movl 0xd4(%rsp), %esi
leaq 0x98(%rsp), %rdi
callq 0x29eb0
movslq 0xd0(%rsp), %rcx
leaq 0x20f6b(%rip), %rax     # 0x5aed0
movq (%rax,%rcx,8), %rax
xorq $-0x1, %rax
andq 0x98(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x90(%rsp), %rax
orq 0x98(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %edi
movq 0x98(%rsp), %rsi
callq 0xe8c0
movq 0x70(%rsp), %rdi
movq %rax, 0x18(%rsp)
movl 0xa4(%rsp), %ecx
movl $0x4, %esi
movl $0x5, %edx
callq 0x29b80
movq 0x18(%rsp), %rcx
andq %rax, %rcx
andq 0x98(%rsp), %rcx
movb $0x1, %al
cmpq $0x0, %rcx
movb %al, 0x23(%rsp)
jne 0x3a03f
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %edi
movq 0x98(%rsp), %rsi
callq 0xe820
movq 0x70(%rsp), %rdi
movq %rax, 0x10(%rsp)
movl 0xa4(%rsp), %ecx
movl $0x3, %esi
movl $0x5, %edx
callq 0x29b80
movq %rax, %rcx
movq 0x10(%rsp), %rax
andq %rcx, %rax
andq 0x98(%rsp), %rax
cmpq $0x0, %rax
setne %al
movb %al, 0x23(%rsp)
movb 0x23(%rsp), %al
andb $0x1, %al
movb %al, 0xf7(%rsp)
jmp 0x3a2fe
movl 0xe4(%rsp), %edi
callq 0x3e710
testb $0x1, %al
jne 0x3a06e
movb $0x0, 0xf7(%rsp)
jmp 0x3a2fe
movl 0xe4(%rsp), %edi
callq 0xfb40
testb $0x1, %al
jne 0x3a083
jmp 0x3a1ca
cmpl $0x0, 0xc8(%rsp)
jne 0x3a1ca
movl 0xd4(%rsp), %esi
leaq 0x98(%rsp), %rdi
callq 0x29eb0
movl 0xe4(%rsp), %edi
callq 0xf830
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, 0x8(%rsp)
subl $0x3, %eax
ja 0x3a1c6
movq 0x8(%rsp), %rax
leaq 0x20be4(%rip), %rcx     # 0x5acb4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x70(%rsp), %rdi
movl 0xd0(%rsp), %esi
callq 0x29aa0
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %esi
callq 0xd760
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movb %al, 0xf7(%rsp)
jmp 0x3a2fe
movl 0xd0(%rsp), %edi
movq 0x98(%rsp), %rsi
callq 0xe820
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %esi
callq 0xd760
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movb %al, 0xf7(%rsp)
jmp 0x3a2fe
movl 0xd0(%rsp), %edi
movq 0x98(%rsp), %rsi
callq 0xe8c0
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %esi
callq 0xd760
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movb %al, 0xf7(%rsp)
jmp 0x3a2fe
movl 0xd0(%rsp), %edi
movq 0x98(%rsp), %rsi
callq 0xe960
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %esi
callq 0xd760
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movb %al, 0xf7(%rsp)
jmp 0x3a2fe
jmp 0x3a1c8
jmp 0x3a1ca
movl 0xe4(%rsp), %edi
callq 0x12970
cmpl $0x0, %eax
je 0x3a2f6
movl 0xd4(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0xd0(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0x8c(%rsp), %eax
jle 0x3a23d
movl 0xa4(%rsp), %edi
movl $0x6, %esi
callq 0x1b380
movl %eax, 0x88(%rsp)
movl 0xa4(%rsp), %edi
movl $0x5, %esi
callq 0x1b380
movl %eax, 0x80(%rsp)
jmp 0x3a26d
movl 0xa4(%rsp), %edi
movl $0x2, %esi
callq 0x1b380
movl %eax, 0x88(%rsp)
movl 0xa4(%rsp), %edi
movl $0x3, %esi
callq 0x1b380
movl %eax, 0x80(%rsp)
movl 0x8c(%rsp), %esi
leaq 0x98(%rsp), %rdi
callq 0x29eb0
movl 0x84(%rsp), %esi
leaq 0x98(%rsp), %rdi
callq 0x29eb0
movl 0x80(%rsp), %esi
leaq 0x98(%rsp), %rdi
callq 0xe7c0
movl 0x88(%rsp), %esi
leaq 0x98(%rsp), %rdi
callq 0xe7c0
movl 0x80(%rsp), %edi
movq 0x98(%rsp), %rsi
callq 0xe8c0
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x48(%rax), %esi
callq 0xd760
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movb %al, 0xf7(%rsp)
jmp 0x3a2fe
movb $0x0, 0xf7(%rsp)
movb 0xf7(%rsp), %al
andb $0x1, %al
addq $0xf8, %rsp
retq
nop