// File: /workspace/github_cpp_makefile_star2_O1/codeandkey[P]neocortex/src/position.cpp
// Function: neocortex::Position::make_move(neocortex::Move)
bool Position::make_move(Move move) {
	assert(!test_check(!color_to_move));

	State last_state = ply.back();
	ply.push_back(last_state);

	State& next_state = ply.back();

	next_state.last_move = move;
	
	if (color_to_move == piece::BLACK) {
		next_state.fullmove_number++;
	}

	next_state.halfmove_clock++;

	next_state.en_passant_square = square::null;
	next_state.captured_piece = piece::null;
	next_state.captured_square = square::null;
	next_state.was_en_passant = false;
	next_state.was_castle = false;

	/* End early on null move, flip color and update zobrist as well. */
	if (move == Move::null) {
		bool ret = !check(); // null moves illegal in check
		color_to_move = !color_to_move;

		next_state.key = 0;
		next_state.key ^= board.get_tt_key();
		next_state.key ^= zobrist::en_passant(next_state.en_passant_square);
		next_state.key ^= zobrist::castle(next_state.castle_rights);

		if (color_to_move == piece::BLACK) {
			next_state.key ^= zobrist::black_to_move();
		}

		return ret;
	}

	int dst_piece = board.get_piece(move.dst());
	int src_piece = board.remove(move.src());

	assert(piece::color(src_piece) == color_to_move);

	if (piece::type(src_piece) == piece::PAWN) {
		next_state.halfmove_clock = 0;
	}

	/* Check for EP capture */
	if (piece::type(src_piece) == piece::PAWN && move.dst() == last_state.en_passant_square) {
		/* EP, need to remove piece */
		int adv_dir = (color_to_move == piece::WHITE) ? NORTH : SOUTH;
		int capture_square = last_state.en_passant_square - adv_dir;
 		int removed = board.remove(capture_square);

		next_state.captured_piece = removed;
		next_state.captured_square = capture_square;
		next_state.halfmove_clock = 0;
		next_state.was_en_passant = true;

		assert(piece::type(removed) == piece::PAWN);
	}

	/* Move rook also if castling */
	if (piece::type(src_piece) == piece::KING && abs(square::file(move.src()) - square::file(move.dst())) > 1) {
		int is_ks = move.dst() > move.src();
		int castle_rank = (color_to_move == piece::WHITE) ? 0 : 7;
		int rook_file = is_ks ? 7 : 0;
		int rook_dstfile = is_ks ? 5 : 3;

		int rook_src = square::at(castle_rank, rook_file);
		int rook_dst = square::at(castle_rank, rook_dstfile);

		board.place(rook_dst, board.remove(rook_src));

		next_state.was_castle = true;
	}

	/* Test if destination is occupied */
	if (piece::is_valid(dst_piece)) {
		/* Normal capture */
		next_state.captured_piece = board.replace(move.dst(), src_piece);
		next_state.captured_square = move.dst();
		next_state.halfmove_clock = 0;

		/* no king captures */
		assert(piece::type(next_state.captured_piece) != piece::KING);
	} else {
		/* Quiet */
		board.place(move.dst(), src_piece);
	}

	/* Apply promotion */
	if (piece::is_type(move.ptype())) {
		board.replace(move.dst(), piece::make_piece(color_to_move, move.ptype()));
	}

	/* Revoke castling rights */
	if (piece::type(src_piece) == piece::KING) {
		int rights = (color_to_move == piece::WHITE) ? 0x3 : 0xC;
		next_state.castle_rights &= ~rights;
	}

	static const bitboard white_ks_revoke = bb::mask(square::at(0, 4)) | bb::mask(square::at(0, 7));
	static const bitboard white_qs_revoke = bb::mask(square::at(0, 4)) | bb::mask(square::at(0, 0));
	static const bitboard black_ks_revoke = bb::mask(square::at(7, 4)) | bb::mask(square::at(7, 7));
	static const bitboard black_qs_revoke = bb::mask(square::at(7, 4)) | bb::mask(square::at(7, 0));

	bitboard src_dst_mask = bb::mask(move.src()) | bb::mask(move.dst());

	if (src_dst_mask & white_ks_revoke) {
		next_state.castle_rights &= ~CASTLE_WHITE_K;
	}

	if (src_dst_mask & white_qs_revoke) {
		next_state.castle_rights &= ~CASTLE_WHITE_Q;
	}

	if (src_dst_mask & black_ks_revoke) {
		next_state.castle_rights &= ~CASTLE_BLACK_K;
	}

	if (src_dst_mask & black_qs_revoke) {
		next_state.castle_rights &= ~CASTLE_BLACK_Q;
	}

	/* Update en passant */
	if (piece::type(src_piece) == piece::PAWN && abs(square::rank(move.dst()) - square::rank(move.src())) > 1) {
		int epsq = move.dst() + ((color_to_move == piece::WHITE) ? SOUTH : NORTH);
		next_state.en_passant_square = epsq;

		if (color_to_move == piece::WHITE) {
			assert(epsq >= 16 && epsq <= 23);
		}
		else {
			assert(epsq >= 40 && epsq <= 47);
		}
	}

	/* Flip color to move */
	color_to_move = !color_to_move;

	/* Update zobrist key */
	next_state.key = 0;
	next_state.key ^= board.get_tt_key();
	next_state.key ^= zobrist::en_passant(next_state.en_passant_square);
	next_state.key ^= zobrist::castle(next_state.castle_rights);

	next_state.in_check = test_check(color_to_move);

	if (color_to_move == piece::BLACK) {
		next_state.key ^= zobrist::black_to_move();
	}

	/* Check that king is not in attack */
	return !test_check(!color_to_move);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq %rsi, (%rsp)
movl %edx, 0x8(%rsp)
xorl %esi, %esi
cmpl $0x0, 0x170(%rdi)
sete %sil
callq 0xf818
testb %al, %al
jne 0x991f
movq 0x160(%rbx), %rsi
movq -0x8(%rsi), %rax
movq %rax, 0x40(%rsp)
movups -0x38(%rsi), %xmm0
movups -0x28(%rsi), %xmm1
movups -0x18(%rsi), %xmm2
movaps %xmm2, 0x30(%rsp)
movaps %xmm1, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
cmpq 0x168(%rbx), %rsi
je 0x950f
leaq -0x38(%rsi), %rax
movq 0x30(%rax), %rcx
movq %rcx, 0x30(%rsi)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsi)
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
addq $0x38, 0x160(%rbx)
jmp 0x9520
leaq 0x158(%rbx), %rdi
leaq 0x10(%rsp), %rdx
callq 0xf8da
movq 0x160(%rbx), %r15
movl 0x8(%rsp), %eax
movl %eax, -0x30(%r15)
movq (%rsp), %rax
movq %rax, -0x38(%r15)
cmpl $0x1, 0x170(%rbx)
jne 0x9544
incl -0x18(%r15)
incl -0x1c(%r15)
movl $0xffffffff, -0x2c(%r15) # imm = 0xFFFFFFFF
movq $-0x1, -0x24(%r15)
movw $0x0, -0x10(%r15)
leaq 0x21632(%rip), %rsi     # 0x2ab98
movq %rsp, %rdi
callq 0x79fa
testb %al, %al
je 0x95db
movq 0x160(%rbx), %rax
cmpl $0x0, -0x14(%rax)
setne %bpl
xorl %eax, %eax
cmpl $0x0, 0x170(%rbx)
sete %al
movl %eax, 0x170(%rbx)
movq $0x0, -0x8(%r15)
movq 0x150(%rbx), %rax
movq %rax, -0x8(%r15)
movl -0x2c(%r15), %edi
callq 0x167f0
xorq %rax, -0x8(%r15)
movl -0x28(%r15), %edi
callq 0x167b8
xorq %rax, -0x8(%r15)
cmpl $0x1, 0x170(%rbx)
jne 0x990a
callq 0x16856
xorq %rax, -0x8(%r15)
jmp 0x990a
movslq 0x4(%rsp), %rax
movl 0x48(%rbx,%rax,4), %r12d
movl (%rsp), %esi
movq %rbx, %rdi
callq 0x647c
cmpl $0xc, %eax
jae 0x995d
movl %eax, %ebp
movl %eax, %ecx
andl $0x1, %ecx
movl 0x170(%rbx), %eax
cmpl %eax, %ecx
jne 0x997c
cmpl $0x1, %ebp
ja 0x9669
movl $0x0, -0x1c(%r15)
movl 0x1c(%rsp), %ecx
cmpl %ecx, 0x4(%rsp)
jne 0x9669
xorl %edx, %edx
testl %eax, %eax
setne %dl
shll $0x4, %edx
leal (%rdx,%rcx), %r14d
addl $-0x8, %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x647c
movl %eax, -0x24(%r15)
movl %r14d, -0x20(%r15)
movl $0x0, -0x1c(%r15)
movb $0x1, -0x10(%r15)
cmpl $0xc, %eax
jae 0x99e2
cmpl $0x2, %eax
jae 0x9a20
movl %ebp, %r13d
andl $0xe, %r13d
cmpl $0xa, %r13d
jne 0x96ee
movl (%rsp), %eax
cmpl $0x40, %eax
jae 0x999b
movl 0x4(%rsp), %ecx
cmpl $0x40, %ecx
jae 0x999b
movl %eax, %edx
andl $0x7, %edx
movl %ecx, %esi
andl $0x7, %esi
subl %esi, %edx
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
cmpl $0x2, %esi
jb 0x96ee
movl 0x170(%rbx), %edx
testl %edx, %edx
movl $0x38, %edi
cmovel %edx, %edi
xorl %edx, %edx
xorl %r8d, %r8d
cmpl %eax, %ecx
setg %r8b
movl $0x7, %esi
cmovlel %edx, %esi
orl %edi, %esi
leal (%rdi,%r8,2), %r14d
addl $0x3, %r14d
movq %rbx, %rdi
callq 0x647c
movq %rbx, %rdi
movl %r14d, %esi
movl %eax, %edx
callq 0x6320
movb $0x1, -0xf(%r15)
movl 0x4(%rsp), %esi
movq %rbx, %rdi
movl %ebp, %edx
cmpl $0xb, %r12d
ja 0x9746
callq 0x6540
movl %eax, -0x24(%r15)
movl 0x4(%rsp), %ecx
movl %ecx, -0x20(%r15)
movl $0x0, -0x1c(%r15)
cmpl $0xc, %eax
jae 0x99e2
andl $0xe, %eax
cmpl $0xa, %eax
jne 0x974b
leaq 0xf545(%rip), %rdi      # 0x18c73
leaq 0xf470(%rip), %rsi      # 0x18ba5
leaq 0xf4c0(%rip), %rcx      # 0x18bfc
movl $0xdd, %edx
callq 0x4190
callq 0x6320
movl 0x8(%rsp), %eax
cmpl $0x5, %eax
ja 0x9772
movl 0x170(%rbx), %ecx
cmpl $0x2, %ecx
jae 0x9a01
movl 0x4(%rsp), %esi
leal (%rcx,%rax,2), %edx
movq %rbx, %rdi
callq 0x6540
cmpl $0xa, %r13d
jne 0x978e
xorl %eax, %eax
cmpl $0x0, 0x170(%rbx)
sete %al
leal (%rax,%rax,8), %eax
addl $-0xd, %eax
andl %eax, -0x28(%r15)
movb 0x21474(%rip), %al      # 0x2ac08
testb %al, %al
je 0x99ba
movb 0x21476(%rip), %al      # 0x2ac18
testb %al, %al
je 0x99c4
movb 0x21478(%rip), %al      # 0x2ac28
testb %al, %al
je 0x99ce
movb 0x2147a(%rip), %al      # 0x2ac38
testb %al, %al
je 0x99d8
movl (%rsp), %eax
cmpq $0x40, %rax
jae 0x993e
movl 0x4(%rsp), %ecx
cmpq $0x40, %rcx
jae 0x993e
movl $0x1, %edx
shlq %cl, %rdx
btsq %rax, %rdx
testq %rdx, 0x2140c(%rip)     # 0x2ac00
je 0x97fb
andb $-0x2, -0x28(%r15)
testq %rdx, 0x2140e(%rip)     # 0x2ac10
je 0x9809
andb $-0x3, -0x28(%r15)
testq %rdx, 0x21410(%rip)     # 0x2ac20
je 0x9817
andb $-0x5, -0x28(%r15)
testq %rdx, 0x21412(%rip)     # 0x2ac30
je 0x9825
andb $-0x9, -0x28(%r15)
cmpl $0x1, %ebp
ja 0x988d
movl %ecx, %edx
shrl $0x3, %edx
shrl $0x3, %eax
subl %eax, %edx
movl %edx, %eax
negl %eax
cmovsl %edx, %eax
cmpl $0x2, %eax
jb 0x988d
movl 0x170(%rbx), %edx
xorl %eax, %eax
testl %edx, %edx
setne %al
shll $0x4, %eax
addl %ecx, %eax
addl $-0x8, %eax
movl %eax, -0x2c(%r15)
andl $-0x8, %eax
testl %edx, %edx
je 0x9884
cmpl $0x28, %eax
je 0x988d
leaq 0xf456(%rip), %rdi      # 0x18cc2
leaq 0xf332(%rip), %rsi      # 0x18ba5
leaq 0xf382(%rip), %rcx      # 0x18bfc
movl $0x10e, %edx            # imm = 0x10E
callq 0x4190
cmpl $0x10, %eax
jne 0x9a3f
xorl %eax, %eax
cmpl $0x0, 0x170(%rbx)
sete %al
movl %eax, 0x170(%rbx)
movq $0x0, -0x8(%r15)
movq 0x150(%rbx), %rax
movq %rax, -0x8(%r15)
movl -0x2c(%r15), %edi
callq 0x167f0
xorq %rax, -0x8(%r15)
movl -0x28(%r15), %edi
callq 0x167b8
xorq %rax, -0x8(%r15)
movl 0x170(%rbx), %esi
movq %rbx, %rdi
callq 0xf818
movzbl %al, %eax
movl %eax, -0x14(%r15)
cmpl $0x1, 0x170(%rbx)
jne 0x98f3
callq 0x16856
xorq %rax, -0x8(%r15)
xorl %esi, %esi
cmpl $0x0, 0x170(%rbx)
sete %sil
movq %rbx, %rdi
callq 0xf818
movl %eax, %ebp
xorb $0x1, %bpl
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xf2ba(%rip), %rdi      # 0x18be0
leaq 0xf278(%rip), %rsi      # 0x18ba5
leaq 0xf2c8(%rip), %rcx      # 0x18bfc
movl $0x88, %edx
callq 0x4190
leaq 0xe13e(%rip), %rdi      # 0x17a83
leaq 0xe14c(%rip), %rsi      # 0x17a98
leaq 0xe154(%rip), %rcx      # 0x17aa7
movl $0x91, %edx
callq 0x4190
leaq 0xe381(%rip), %rdi      # 0x17ce5
leaq 0xe38a(%rip), %rsi      # 0x17cf5
leaq 0xf817(%rip), %rcx      # 0x19189
movl $0x3a, %edx
callq 0x4190
leaq 0xf2a3(%rip), %rdi      # 0x18c26
leaq 0xf21b(%rip), %rsi      # 0x18ba5
leaq 0xf26b(%rip), %rcx      # 0x18bfc
movl $0xb1, %edx
callq 0x4190
leaq 0xe0e9(%rip), %rdi      # 0x17a8b
leaq 0xe0cd(%rip), %rsi      # 0x17a76
leaq 0xe16c(%rip), %rcx      # 0x17b1c
movl $0x40, %edx
callq 0x4190
callq 0x4590
jmp 0x979c
callq 0x45ba
jmp 0x97aa
callq 0x45e4
jmp 0x97b8
callq 0x4614
jmp 0x97c6
leaq 0xe2fc(%rip), %rdi      # 0x17ce5
leaq 0xe305(%rip), %rsi      # 0x17cf5
leaq 0xe30a(%rip), %rcx      # 0x17d01
movl $0x45, %edx
callq 0x4190
leaq 0xf7a2(%rip), %rdi      # 0x191aa
leaq 0xe2e6(%rip), %rsi      # 0x17cf5
leaq 0xf7b5(%rip), %rcx      # 0x191cb
movl $0x2f, %edx
callq 0x4190
leaq 0xf228(%rip), %rdi      # 0x18c4f
leaq 0xf177(%rip), %rsi      # 0x18ba5
leaq 0xf1c7(%rip), %rcx      # 0x18bfc
movl $0xc3, %edx
callq 0x4190
leaq 0xf263(%rip), %rdi      # 0x18ca9
leaq 0xf158(%rip), %rsi      # 0x18ba5
leaq 0xf1a8(%rip), %rcx      # 0x18bfc
movl $0x10b, %edx            # imm = 0x10B
callq 0x4190