// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rdx, %r8
movl %esi, %r13d
movq %rdi, %r15
movl %esi, %eax
shrl $0x6, %eax
movl %eax, %r12d
andl $0x3f, %r12d
movl %esi, %r14d
andl $0x3f, %r14d
movl %esi, %ebx
andl $0xc000, %ebx           # imm = 0xC000
cmpl $0x8000, %ebx           # imm = 0x8000
je 0x18dd6
movl (%r15,%r14,4), %ecx
andl $0x7, %ecx
jne 0x18dd6
movl (%r15,%r12,4), %ecx
andl $0x7, %ecx
cmpl $0x6, %ecx
jne 0x18f74
movl 0x1090(%r15), %edx
movq %rdx, %rax
xorq $0x1, %rax
shlq $0x9, %rax
movslq 0x310(%r15,%rax), %rbp
movl %r14d, %eax
shll $0x6, %eax
addq 0x1c1c4(%rip), %rax     # 0x34f88
cmpb $0x0, (%rbp,%rax)
je 0x18e85
xorl %ebp, %ebp
jmp 0x18f89
andl $0x7, %r13d
andl $0x38, %eax
orl %r13d, %eax
cmpl $0x8000, %ebx           # imm = 0x8000
movl 0x1090(%r15), %ebx
leaq 0xf85bc(%rip), %rcx     # 0x1113b0
cmovnel %r14d, %eax
movq (%rcx,%rax,8), %rax
andq 0x108(%r15), %rax
leaq 0x10ef6(%rip), %rdx     # 0x29d00
movq (%rdx,%r14,8), %r14
notq %r14
andq 0x100(%r15), %r14
orq %rax, %r14
andq (%rcx,%r12,8), %r14
movl 0x48(%r8), %edi
movq %r14, %rsi
movq %r8, %r12
callq 0x8255
movq 0x128(%r15), %rcx
orq 0x120(%r15), %rcx
andq 0x140(%r15,%rbx,8), %rax
andq %rcx, %rax
movb $0x1, %bpl
testq %r14, %rax
jne 0x18f89
movl 0x48(%r12), %edi
movq %r14, %rsi
callq 0x8208
movq 0x128(%r15), %rcx
orq 0x118(%r15), %rcx
andq 0x140(%r15,%rbx,8), %rax
andq %rcx, %rax
testq %r14, %rax
setne %bpl
jmp 0x18f89
movl %r12d, %eax
shll $0x6, %eax
addq 0x1c0f6(%rip), %rax     # 0x34f88
cmpb $0x0, (%rbp,%rax)
je 0x18f74
leaq 0xf82fc(%rip), %rax     # 0x1111a0
movq (%rax,%r14,8), %rsi
orq (%rax,%r12,8), %rsi
xorq 0x100(%r15), %rsi
movq %rsi, 0x18(%rsp)
leaq 0xf8891(%rip), %rax     # 0x111750
movq 0x110(%r15), %r9
movq %rcx, 0x30(%rsp)
movq 0x140(%r15), %rcx
andq 0x1200(%rax,%rbp,8), %rcx
movq 0x148(%r15), %rdi
andq 0x200(%rax,%rbp,8), %rdi
orq %rcx, %rdi
andq 0x108(%r15), %rdi
movq %rdi, 0x28(%rsp)
andq 0x400(%rax,%rbp,8), %r9
movq %r9, 0x8(%rsp)
movl %ebp, %edi
movq %r8, 0x10(%rsp)
movq %rdx, 0x20(%rsp)
callq 0x8255
movq 0x128(%r15), %rcx
orq 0x120(%r15), %rcx
andq %rax, %rcx
orq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movl %ebp, %edi
movq 0x18(%rsp), %rsi
callq 0x8208
movq 0x10(%rsp), %r8
movq 0x128(%r15), %rcx
orq 0x118(%r15), %rcx
andq %rax, %rcx
orq 0x8(%rsp), %rcx
orq 0x28(%rsp), %rcx
movb $0x1, %bpl
movq 0x20(%rsp), %rax
testq %rcx, 0x140(%r15,%rax,8)
movq 0x30(%rsp), %rcx
jne 0x18f89
leaq 0xf8225(%rip), %r9      # 0x1111a0
movq (%r9,%r14,8), %rax
movb $0x1, %bpl
testq %rax, 0x8(%r8,%rcx,8)
je 0x18f9a
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r8), %rax
testq %rax, %rax
je 0x18fd6
testq %rax, (%r9,%r12,8)
je 0x18fd6
cmpl $0x6, %ecx
je 0x18fb2
cmpl $0x1, %ecx
jne 0x18f89
movl 0x48(%r8), %edx
movl %r12d, %edi
movl %r14d, %esi
movq %r8, 0x10(%rsp)
callq 0x1adf0
leaq 0xf81d3(%rip), %r9      # 0x1111a0
movq 0x10(%rsp), %r8
testb %al, %al
je 0x18f89
testl %ebx, %ebx
je 0x18dcf
movq 0x100(%r15), %rsi
cmpl $0x4000, %ebx           # imm = 0x4000
jne 0x1902c
leaq 0xf83bc(%rip), %rax     # 0x1113b0
andq (%rax,%r12,8), %rsi
shrl $0xc, %r13d
andl $0x3, %r13d
leaq 0x10ae9(%rip), %rax     # 0x29af0
movslq (%rax,%r13,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0xf8739(%rip), %rax     # 0x111750
movslq 0x48(%r8), %rcx
movq (%r9,%rcx,8), %rcx
testq %rcx, 0x400(%rax,%r14,8)
jmp 0x18e7c
cmpl $0xc000, %ebx           # imm = 0xC000
jne 0x18dcf
movslq 0x1090(%r15), %rax
xorl %ecx, %ecx
cmpl %r12d, %r14d
seta %cl
imulq $0x38, %rax, %rdx
leaq 0xf835e(%rip), %rdi     # 0x1113b0
movq (%rdi,%r12,8), %rax
andq (%rdi,%r14,8), %rax
leal 0x3(,%rcx,2), %edi
orq %rdx, %rdi
andq %rsi, %rax
orq (%r9,%rdi,8), %rax
leal (%rdx,%rcx,4), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
orq (%r9,%rcx,8), %rax
movq %rax, %rsi
jmp 0x19080
movl %r14d, %edi
movq %r8, %rbx
movq %r9, %r14
callq 0x8255
jmp 0x190ab
movl %r14d, %edi
movq %r8, %rbx
movq %r9, %r14
callq 0x82a2
jmp 0x190ab
movl %r14d, %edi
movq %r8, %rbx
movq %r9, %r14
callq 0x8208
movslq 0x48(%rbx), %rcx
testq %rax, (%r14,%rcx,8)
jmp 0x18e7c