// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movq %rdx, %rbx
movl %esi, %eax
shrl $0x6, %eax
movl %eax, %edx
andl $0x3f, %edx
movl %esi, %r8d
andl $0x3f, %r8d
movl %esi, %r9d
andl $0xc000, %r9d           # imm = 0xC000
cmpl $0x8000, %r9d           # imm = 0x8000
je 0x1ef6b
movl (%rdi,%r8,4), %ecx
andl $0x7, %ecx
jne 0x1ef6b
movl (%rdi,%rdx,4), %r10d
andl $0x7, %r10d
cmpl $0x6, %r10d
jne 0x1f1ca
movl 0x1090(%rdi), %r11d
movq %r11, %rax
xorq $0x1, %rax
shlq $0x9, %rax
movslq 0x310(%rdi,%rax), %rax
movl %r8d, %ecx
shll $0x6, %ecx
addq 0x1e01e(%rip), %rcx     # 0x3cf78
cmpb $0x0, (%rax,%rcx)
je 0x1f095
xorl %eax, %eax
jmp 0x1f1de
andl $0x7, %esi
andl $0x38, %eax
orl %esi, %eax
cmpl $0x8000, %r9d           # imm = 0x8000
leaq 0xfa42f(%rip), %rcx     # 0x1193b0
cmovnel %r8d, %eax
movq (%rcx,%rax,8), %rax
andq 0x108(%rdi), %rax
leaq 0x13fa9(%rip), %rsi     # 0x32f40
movq (%rsi,%r8,8), %rsi
notq %rsi
andq 0x100(%rdi), %rsi
orq %rax, %rsi
andq (%rcx,%rdx,8), %rsi
movl 0x1090(%rdi), %eax
movslq 0x48(%rbx), %rdx
leaq 0x277e3(%rip), %rcx     # 0x467a0
movq (%rcx,%rdx,8), %rcx
andq %rsi, %rcx
leaq 0x279d5(%rip), %r8      # 0x469a0
leaq 0x105ee(%rip), %r9      # 0x2f5c0
movq (%r9,%rdx,8), %r9
movl %r9d, %r10d
imull %ecx, %r10d
shrq $0x20, %rcx
shrq $0x20, %r9
imull %ecx, %r9d
xorl %r10d, %r9d
leaq 0x108cd(%rip), %rcx     # 0x2f8c0
movb (%rcx,%rdx,4), %cl
shrl %cl, %r9d
addl (%r8,%rdx,4), %r9d
leaq 0x27a9c(%rip), %rcx     # 0x46aa0
movq 0x128(%rdi), %r8
movq 0x120(%rdi), %r10
orq %r8, %r10
andq (%rcx,%r9,8), %r10
movq 0x140(%rdi,%rax,8), %r9
andq %rsi, %r10
movb $0x1, %al
testq %r9, %r10
jne 0x1f1de
leaq 0xefa6a(%rip), %rax     # 0x10eaa0
movq (%rax,%rdx,8), %rax
andq %rsi, %rax
leaq 0x1037c(%rip), %rcx     # 0x2f3c0
movq (%rcx,%rdx,8), %r10
movl %r10d, %ecx
imull %eax, %ecx
shrq $0x20, %rax
shrq $0x20, %r10
imull %eax, %r10d
xorl %ecx, %r10d
leaq 0x1075c(%rip), %rax     # 0x2f7c0
movb (%rax,%rdx,4), %cl
shrl %cl, %r10d
leaq 0xefc2f(%rip), %rax     # 0x10eca0
addl (%rax,%rdx,4), %r10d
leaq 0xefd24(%rip), %rax     # 0x10eda0
orq 0x118(%rdi), %r8
andq (%rax,%r10,8), %r8
andq %rsi, %r8
testq %r9, %r8
setne %al
jmp 0x1f1de
movl %edx, %ecx
shll $0x6, %ecx
addq 0x1ded7(%rip), %rcx     # 0x3cf78
cmpb $0x0, (%rax,%rcx)
je 0x1f1ca
leaq 0xfa0ee(%rip), %rcx     # 0x1191a0
movq (%rcx,%r8,8), %r14
orq (%rcx,%rdx,8), %r14
xorq 0x100(%rdi), %r14
leaq 0xfa688(%rip), %rcx     # 0x119750
movq 0x140(%rdi), %r15
andq 0x1200(%rcx,%rax,8), %r15
movq 0x148(%rdi), %r13
andq 0x200(%rcx,%rax,8), %r13
orq %r15, %r13
andq 0x108(%rdi), %r13
movq 0x110(%rdi), %r12
andq 0x400(%rcx,%rax,8), %r12
leaq 0x2769a(%rip), %rcx     # 0x467a0
movq (%rcx,%rax,8), %rcx
andq %r14, %rcx
leaq 0x104ac(%rip), %r15     # 0x2f5c0
movq (%r15,%rax,8), %rbp
movl %ebp, %r15d
imull %ecx, %r15d
shrq $0x20, %rcx
shrq $0x20, %rbp
imull %ecx, %ebp
xorl %r15d, %ebp
leaq 0x1078c(%rip), %rcx     # 0x2f8c0
movb (%rcx,%rax,4), %cl
shrl %cl, %ebp
movq 0x128(%rdi), %r15
leaq 0x27859(%rip), %rcx     # 0x469a0
addl (%rcx,%rax,4), %ebp
orq %r13, %r12
leaq 0x2794c(%rip), %rcx     # 0x46aa0
movq 0x120(%rdi), %r13
orq %r15, %r13
andq (%rcx,%rbp,8), %r13
orq %r12, %r13
leaq 0xef934(%rip), %rcx     # 0x10eaa0
andq (%rcx,%rax,8), %r14
leaq 0x10249(%rip), %rcx     # 0x2f3c0
movq (%rcx,%rax,8), %r12
movl %r12d, %ecx
imull %r14d, %ecx
shrq $0x20, %r14
shrq $0x20, %r12
imull %r14d, %r12d
xorl %ecx, %r12d
leaq 0x10628(%rip), %rcx     # 0x2f7c0
movb (%rcx,%rax,4), %cl
shrl %cl, %r12d
leaq 0xefafb(%rip), %rcx     # 0x10eca0
addl (%rcx,%rax,4), %r12d
leaq 0xefbf0(%rip), %rax     # 0x10eda0
orq 0x118(%rdi), %r15
andq (%rax,%r12,8), %r15
orq %r13, %r15
movb $0x1, %al
testq %r15, 0x140(%rdi,%r11,8)
jne 0x1f1de
leaq 0xf9fcf(%rip), %r14     # 0x1191a0
movq (%r14,%r8,8), %rcx
movb $0x1, %al
testq %rcx, 0x8(%rbx,%r10,8)
je 0x1f1ed
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %rcx
testq %rcx, %rcx
je 0x1f247
testq %rcx, (%r14,%rdx,8)
je 0x1f247
cmpl $0x6, %r10d
je 0x1f207
cmpl $0x1, %r10d
jne 0x1f1de
movslq 0x48(%rbx), %r10
movl %edx, %ecx
shll $0x9, %ecx
leaq 0xfc539(%rip), %r11     # 0x11b750
addq %r11, %rcx
movq (%rcx,%r10,8), %r15
orq (%rcx,%r8,8), %r15
movl %r8d, %ecx
shll $0x9, %ecx
addq %r11, %rcx
orq (%rcx,%r10,8), %r15
movl $0x1, %r11d
movl %edx, %ecx
shlq %cl, %r11
btsq %r8, %r11
btsq %r10, %r11
testq %r11, %r15
je 0x1f1de
testl %r9d, %r9d
je 0x1ef64
movq 0x100(%rdi), %rax
cmpl $0x4000, %r9d           # imm = 0x4000
jne 0x1f29d
leaq 0xfa149(%rip), %rcx     # 0x1193b0
andq (%rcx,%rdx,8), %rax
shrl $0xc, %esi
andl $0x3, %esi
leaq 0x13ab8(%rip), %rcx     # 0x32d30
movslq (%rcx,%rsi,4), %rdx
addq %rcx, %rdx
jmpq *%rdx
leaq 0xfa4c8(%rip), %rax     # 0x119750
movslq 0x48(%rbx), %rcx
movq (%r14,%rcx,8), %rcx
testq %rcx, 0x400(%rax,%r8,8)
jmp 0x1f08d
cmpl $0xc000, %r9d           # imm = 0xC000
jne 0x1ef64
movslq 0x1090(%rdi), %rcx
xorl %esi, %esi
cmpl %edx, %r8d
seta %sil
imulq $0x38, %rcx, %rcx
leaq 0xfa0eb(%rip), %rdi     # 0x1193b0
movq (%rdi,%rdx,8), %r9
andq (%rdi,%r8,8), %r9
leal 0x3(,%rsi,2), %edx
orq %rcx, %rdx
andq %rax, %r9
orq (%r14,%rdx,8), %r9
leal (%rcx,%rsi,4), %eax
addl $0x2, %eax
cltq
orq (%r14,%rax,8), %r9
leaq 0x274af(%rip), %rax     # 0x467a0
andq (%rax,%rdx,8), %r9
leaq 0x276a4(%rip), %rax     # 0x469a0
leaq 0x102bd(%rip), %rcx     # 0x2f5c0
movq (%rcx,%rdx,8), %rsi
movl %esi, %ecx
imull %r9d, %ecx
shrq $0x20, %r9
shrq $0x20, %rsi
imull %r9d, %esi
xorl %ecx, %esi
leaq 0x1059e(%rip), %rcx     # 0x2f8c0
movb (%rcx,%rdx,4), %cl
shrl %cl, %esi
addl (%rax,%rdx,4), %esi
leaq 0x2776f(%rip), %rax     # 0x46aa0
movslq 0x48(%rbx), %rcx
movq (%r14,%rcx,8), %rcx
testq %rcx, (%rax,%rsi,8)
jmp 0x1f08d
movl %r8d, %edi
movq %rax, %rsi
callq 0x215cf
jmp 0x1f367
movl %r8d, %edi
movq %rax, %rsi
callq 0x2165d
jmp 0x1f367
movl %r8d, %edi
movq %rax, %rsi
callq 0x21582
movslq 0x48(%rbx), %rcx
testq %rax, (%r14,%rcx,8)
jmp 0x1f08d