// File: /workspace/github_cpp_makefile_star2_O2/codeandkey[P]neocortex/src/position.cpp
// Function: neocortex::Position::unmake_move(neocortex::Move)
void Position::unmake_move(Move move) {
	assert(ply.size() > 1);
	assert(ply.back().last_move == move);

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

	/* Flip CTM early for readability */
	color_to_move = !color_to_move;

	/* If unmaking null move, good to go */
	if (move == Move::null) {
		return;
	}

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

	/* Move piece back to source */
	int dst_piece = board.remove(move.dst());
	board.place(move.src(), dst_piece);

	/* Unmove rook in castling */
	if (piece::type(dst_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 ? 5 : 3;
		int rook_dstfile = is_ks ? 7 : 0;

		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));
	}

	/* Replace captured pieces */
	if (piece::is_valid(last_state.captured_piece)) {
		board.place(last_state.captured_square, last_state.captured_piece);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq %rsi, 0x8(%rsp)
movl %edx, 0x10(%rsp)
movq 0x160(%rdi), %rdi
movq %rdi, %rax
subq 0x158(%rbx), %rax
pushq $0x38
popq %rcx
cqto
idivq %rcx
cmpq $0x1, %rax
jbe 0x89b0
addq $-0x38, %rdi
leaq 0x8(%rsp), %rsi
callq 0x6fee
testb %al, %al
je 0x89cf
movq 0x160(%rbx), %rax
leaq -0x38(%rax), %rcx
movl -0x24(%rax), %ebp
movl -0x20(%rax), %r14d
movq %rcx, 0x160(%rbx)
xorl %eax, %eax
cmpl $0x0, 0x170(%rbx)
sete %al
movl %eax, 0x170(%rbx)
leaq 0x1e31f(%rip), %rsi     # 0x26bc8
leaq 0x8(%rsp), %rdi
callq 0x6fee
testb %al, %al
jne 0x89a1
cmpl $0x5, 0x10(%rsp)
ja 0x88e1
movl 0xc(%rsp), %r15d
movl 0x170(%rbx), %edi
xorl %esi, %esi
callq 0x7ae8
movq %rbx, %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x5c54
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0x5b86
movl %eax, %r15d
movl 0x8(%rsp), %esi
movq %rbx, %rdi
movl %eax, %edx
callq 0x5a44
movl %r15d, %edi
callq 0x63ef
cmpl $0x5, %eax
jne 0x898f
movl 0x8(%rsp), %edi
callq 0x50da
movl %eax, %r15d
movl 0xc(%rsp), %edi
callq 0x50da
subl %eax, %r15d
movl %r15d, %eax
negl %eax
cmovsl %r15d, %eax
cmpl $0x2, %eax
jb 0x898f
movl 0xc(%rsp), %eax
movl 0x170(%rbx), %r15d
testl %r15d, %r15d
pushq $0x7
popq %rcx
cmovnel %ecx, %r15d
xorl %r12d, %r12d
xorl %edx, %edx
cmpl 0x8(%rsp), %eax
setg %dl
leal 0x3(%rdx,%rdx), %esi
cmovgl %ecx, %r12d
movl %r15d, %edi
callq 0x5131
movl %eax, %r13d
movl %r15d, %edi
movl %r12d, %esi
callq 0x5131
movl %eax, %r15d
movq %rbx, %rdi
movl %r13d, %esi
callq 0x5b86
movq %rbx, %rdi
movl %r15d, %esi
movl %eax, %edx
callq 0x5a44
cmpl $0xb, %ebp
ja 0x89a1
movq %rbx, %rdi
movl %r14d, %esi
movl %ebp, %edx
callq 0x5a44
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc494(%rip), %rdi      # 0x14e4b
leaq 0xc357(%rip), %rsi      # 0x14d15
leaq 0xc495(%rip), %rcx      # 0x14e5a
movl $0x126, %edx            # imm = 0x126
callq 0x41f0
leaq 0xc4b0(%rip), %rdi      # 0x14e86
leaq 0xc338(%rip), %rsi      # 0x14d15
leaq 0xc476(%rip), %rcx      # 0x14e5a
movl $0x127, %edx            # imm = 0x127
callq 0x41f0