// File: /workspace/github_cpp_makefile_star2_O3/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 0xfba6
testb %al, %al
jne 0x9a1f
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 0x95db
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)
movq 0x160(%rbx), %r15
addq $0x38, %r15
movq %r15, 0x160(%rbx)
jmp 0x95f3
leaq 0x158(%rbx), %rdi
leaq 0x10(%rsp), %rdx
callq 0xfc68
movq 0x160(%rbx), %r15
movl 0x8(%rsp), %eax
movl %eax, -0x30(%r15)
movq (%rsp), %rax
movq %rax, -0x38(%r15)
cmpl $0x1, 0x170(%rbx)
jne 0x9610
incl -0x18(%r15)
incl -0x1c(%r15)
movl $0xffffffff, -0x2c(%r15) # imm = 0xFFFFFFFF
movq $-0x1, -0x24(%r15)
movw $0x0, -0x10(%r15)
leaq 0x22566(%rip), %rsi     # 0x2bb98
movq %rsp, %rdi
callq 0x7b06
testb %al, %al
je 0x969f
movq 0x150(%rbx), %rax
movq 0x160(%rbx), %rcx
cmpl $0x0, -0x14(%rcx)
setne %bpl
xorl %ecx, %ecx
cmpl $0x0, 0x170(%rbx)
sete %cl
movl %ecx, 0x170(%rbx)
movq %rax, -0x8(%r15)
movl -0x2c(%r15), %edi
callq 0x16ac0
xorq %rax, -0x8(%r15)
movl -0x28(%r15), %edi
callq 0x16a88
xorq %rax, -0x8(%r15)
cmpl $0x1, 0x170(%rbx)
jne 0x99e0
callq 0x16b26
xorq %rax, -0x8(%r15)
jmp 0x99e0
movslq 0x4(%rsp), %rax
movl 0x48(%rbx,%rax,4), %r12d
movl (%rsp), %esi
movq %rbx, %rdi
callq 0x6586
cmpl $0xc, %eax
jae 0x9a5d
movl %eax, %ebp
andl $0x1, %eax
cmpl 0x170(%rbx), %eax
jne 0x9a7c
cmpl $0x1, %ebp
ja 0x9745
movl $0x0, -0x1c(%r15)
movl 0x4(%rsp), %ecx
cmpl 0x1c(%rsp), %ecx
jne 0x97c6
shll $0x4, %eax
leal (%rax,%rcx), %r14d
addl $-0x8, %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x6586
movl %eax, -0x24(%r15)
movl %r14d, -0x20(%r15)
movl $0x0, -0x1c(%r15)
movb $0x1, -0x10(%r15)
cmpl $0xc, %eax
jae 0x9ac3
cmpl $0x2, %eax
jb 0x97c6
leaq 0x105e2(%rip), %rdi     # 0x19d0f
leaq 0x10531(%rip), %rsi     # 0x19c65
leaq 0x10581(%rip), %rcx     # 0x19cbc
movl $0xc3, %edx
callq 0x4190
movl %ebp, %ecx
andl $0xe, %ecx
cmpl $0xa, %ecx
jne 0x97c6
movl (%rsp), %ecx
cmpl $0x40, %ecx
jae 0x9ae2
movl 0x4(%rsp), %edx
cmpl $0x40, %edx
jae 0x9ae2
movl %ecx, %esi
andl $0x7, %esi
movl %edx, %edi
andl $0x7, %edi
subl %edi, %esi
movl %esi, %edi
negl %edi
cmovsl %esi, %edi
movb $0x1, %r13b
cmpl $0x2, %edi
jb 0x97c9
testl %eax, %eax
movl $0x38, %edi
cmovel %eax, %edi
xorl %eax, %eax
xorl %r8d, %r8d
cmpl %ecx, %edx
movl $0x7, %esi
cmovbel %eax, %esi
seta %r8b
orl %edi, %esi
leal (%rdi,%r8,2), %r14d
addl $0x3, %r14d
movq %rbx, %rdi
callq 0x6586
movq %rbx, %rdi
movl %r14d, %esi
movl %eax, %edx
callq 0x642c
movb $0x1, -0xf(%r15)
jmp 0x97c9
xorl %r13d, %r13d
movl 0x4(%rsp), %esi
movq %rbx, %rdi
movl %ebp, %edx
cmpl $0xb, %r12d
ja 0x9821
callq 0x664c
movl %eax, -0x24(%r15)
movl 0x4(%rsp), %ecx
movl %ecx, -0x20(%r15)
movl $0x0, -0x1c(%r15)
cmpl $0xc, %eax
jae 0x9ac3
andl $0xe, %eax
cmpl $0xa, %eax
jne 0x9826
leaq 0x1052a(%rip), %rdi     # 0x19d33
leaq 0x10455(%rip), %rsi     # 0x19c65
leaq 0x104a5(%rip), %rcx     # 0x19cbc
movl $0xdd, %edx
callq 0x4190
callq 0x642c
movl 0x8(%rsp), %eax
cmpl $0x5, %eax
ja 0x984d
movl 0x170(%rbx), %ecx
cmpl $0x2, %ecx
jae 0x9b01
movl 0x4(%rsp), %esi
leal (%rcx,%rax,2), %edx
movq %rbx, %rdi
callq 0x664c
testb %r13b, %r13b
je 0x9868
xorl %eax, %eax
cmpl $0x0, 0x170(%rbx)
sete %al
leal (%rax,%rax,8), %eax
addl $-0xd, %eax
andl %eax, -0x28(%r15)
movb 0x2239a(%rip), %al      # 0x2bc08
testb %al, %al
je 0x9a9b
movb 0x2239c(%rip), %al      # 0x2bc18
testb %al, %al
je 0x9aa5
movb 0x2239e(%rip), %al      # 0x2bc28
testb %al, %al
je 0x9aaf
movb 0x223a0(%rip), %al      # 0x2bc38
testb %al, %al
je 0x9ab9
movl (%rsp), %eax
cmpq $0x40, %rax
jae 0x9a3e
movl 0x4(%rsp), %ecx
cmpq $0x40, %rcx
jae 0x9a3e
movl $0x1, %edx
shlq %cl, %rdx
btsq %rax, %rdx
testq %rdx, 0x22332(%rip)     # 0x2bc00
je 0x98d5
andb $-0x2, -0x28(%r15)
testq %rdx, 0x22334(%rip)     # 0x2bc10
je 0x98e3
andb $-0x3, -0x28(%r15)
testq %rdx, 0x22336(%rip)     # 0x2bc20
je 0x98f1
andb $-0x5, -0x28(%r15)
testq %rdx, 0x22338(%rip)     # 0x2bc30
je 0x98ff
andb $-0x9, -0x28(%r15)
cmpl $0x2, %ebp
jae 0x9967
movl %ecx, %edx
shrl $0x3, %edx
shrl $0x3, %eax
subl %eax, %edx
movl %edx, %esi
negl %esi
cmovsl %edx, %esi
movl 0x170(%rbx), %eax
cmpl $0x2, %esi
jb 0x996d
xorl %edx, %edx
testl %eax, %eax
setne %dl
shll $0x4, %edx
addl %edx, %ecx
addl $-0x8, %ecx
movl %ecx, -0x2c(%r15)
andl $-0x8, %ecx
testl %eax, %eax
je 0x99f5
movl $0x1, %eax
cmpl $0x28, %ecx
je 0x996d
leaq 0x10433(%rip), %rdi     # 0x19d82
leaq 0x1030f(%rip), %rsi     # 0x19c65
leaq 0x1035f(%rip), %rcx     # 0x19cbc
movl $0x10e, %edx            # imm = 0x10E
callq 0x4190
movl 0x170(%rbx), %eax
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
movl %ecx, 0x170(%rbx)
movq 0x150(%rbx), %rax
movq %rax, -0x8(%r15)
movl -0x2c(%r15), %edi
callq 0x16ac0
xorq %rax, -0x8(%r15)
movl -0x28(%r15), %edi
callq 0x16a88
xorq %rax, -0x8(%r15)
movl 0x170(%rbx), %esi
movq %rbx, %rdi
callq 0xfba6
movzbl %al, %eax
movl %eax, -0x14(%r15)
movl 0x170(%rbx), %eax
cmpl $0x1, %eax
jne 0x99ce
callq 0x16b26
xorq %rax, -0x8(%r15)
movl 0x170(%rbx), %eax
xorl %esi, %esi
testl %eax, %eax
sete %sil
movq %rbx, %rdi
callq 0xfba6
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
xorl %eax, %eax
cmpl $0x10, %ecx
je 0x996d
leaq 0x10362(%rip), %rdi     # 0x19d69
leaq 0x10257(%rip), %rsi     # 0x19c65
leaq 0x102a7(%rip), %rcx     # 0x19cbc
movl $0x10b, %edx            # imm = 0x10B
callq 0x4190
leaq 0x1027a(%rip), %rdi     # 0x19ca0
leaq 0x10238(%rip), %rsi     # 0x19c65
leaq 0x10288(%rip), %rcx     # 0x19cbc
movl $0x88, %edx
callq 0x4190
leaq 0xf1d3(%rip), %rdi      # 0x18c18
leaq 0x1086a(%rip), %rsi     # 0x1a2b6
leaq 0x10872(%rip), %rcx     # 0x1a2c5
movl $0x91, %edx
callq 0x4190
leaq 0xf2ba(%rip), %rdi      # 0x18d1e
leaq 0xf2c3(%rip), %rsi      # 0x18d2e
leaq 0x107d7(%rip), %rcx     # 0x1a249
movl $0x3a, %edx
callq 0x4190
leaq 0x10263(%rip), %rdi     # 0x19ce6
leaq 0x101db(%rip), %rsi     # 0x19c65
leaq 0x1022b(%rip), %rcx     # 0x19cbc
movl $0xb1, %edx
callq 0x4190
callq 0x4590
jmp 0x9876
callq 0x45ba
jmp 0x9884
callq 0x45e4
jmp 0x9892
callq 0x4614
jmp 0x98a0
leaq 0xf254(%rip), %rdi      # 0x18d1e
leaq 0xf25d(%rip), %rsi      # 0x18d2e
leaq 0xf262(%rip), %rcx      # 0x18d3a
movl $0x45, %edx
callq 0x4190
leaq 0xf137(%rip), %rdi      # 0x18c20
leaq 0xef86(%rip), %rsi      # 0x18a76
leaq 0xf049(%rip), %rcx      # 0x18b40
movl $0x40, %edx
callq 0x4190
leaq 0x10762(%rip), %rdi     # 0x1a26a
leaq 0xf21f(%rip), %rsi      # 0x18d2e
leaq 0x10775(%rip), %rcx     # 0x1a28b
movl $0x2f, %edx
callq 0x4190