// File: codeandkey[P]neocortex/src/board.cpp
// Function: neocortex::Board::replace(int, int)
int Board::replace(int sq, int p) {
	assert(square::is_valid(sq));
	assert(piece::is_valid(state[sq]));

	int res = state[sq];

	bitboard mask = bb::mask(sq);

	piece_occ[piece::type(res)] ^= mask;
	color_occ[piece::color(res)] ^= mask;
	piece_occ[piece::type(p)] ^= mask;
	color_occ[piece::color(p)] ^= mask;

	key ^= zobrist::piece(sq, res);
	key ^= zobrist::piece(sq, p);

	mat_mg -= eval::MATERIAL_MG_LOOKUP[res];
	mat_eg -= eval::MATERIAL_EG_LOOKUP[res];
	mat_mg += eval::MATERIAL_MG_LOOKUP[p];
	mat_eg += eval::MATERIAL_EG_LOOKUP[p];

	state[sq] = p;

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0xc(%rbp), %edi
callq 0x6d10
testb $0x1, %al
jne 0x7f38
jmp 0x7f3a
jmp 0x7f59
leaq 0x1db84(%rip), %rdi     # 0x25ac5
leaq 0x1dcde(%rip), %rsi     # 0x25c26
movl $0x75, %edx
leaq 0x1dd70(%rip), %rcx     # 0x25cc4
callq 0x5290
movq -0x28(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl 0x48(%rax,%rcx,4), %edi
callq 0x8cf0
testb $0x1, %al
jne 0x7f70
jmp 0x7f72
jmp 0x7f91
leaq 0x1dd30(%rip), %rdi     # 0x25ca9
leaq 0x1dca6(%rip), %rsi     # 0x25c26
movl $0x76, %edx
leaq 0x1dd38(%rip), %rcx     # 0x25cc4
callq 0x5290
movq -0x28(%rbp), %rax
movslq -0xc(%rbp), %rcx
movl 0x48(%rax,%rcx,4), %eax
movl %eax, -0x14(%rbp)
movl -0xc(%rbp), %edi
callq 0x6bd0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x14(%rbp), %edi
callq 0x8d20
movq -0x48(%rbp), %rdx
movl %eax, %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
xorq 0x18(%rax,%rcx,8), %rdx
movq %rdx, 0x18(%rax,%rcx,8)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movl -0x14(%rbp), %edi
callq 0x8d70
movq -0x40(%rbp), %rdx
movl %eax, %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
xorq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0x10(%rbp), %edi
callq 0x8d20
movq -0x38(%rbp), %rdx
movl %eax, %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
xorq 0x18(%rax,%rcx,8), %rdx
movq %rdx, 0x18(%rax,%rcx,8)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movl -0x10(%rbp), %edi
callq 0x8d70
movq -0x30(%rbp), %rdx
movl %eax, %ecx
movq -0x28(%rbp), %rax
movslq %ecx, %rcx
xorq 0x8(%rax,%rcx,8), %rdx
movq %rdx, 0x8(%rax,%rcx,8)
movl -0xc(%rbp), %edi
movl -0x14(%rbp), %esi
callq 0x23cc0
movq %rax, %rcx
movq -0x28(%rbp), %rax
xorq 0x150(%rax), %rcx
movq %rcx, 0x150(%rax)
movl -0xc(%rbp), %edi
movl -0x10(%rbp), %esi
callq 0x23cc0
movq %rax, %rcx
movq -0x28(%rbp), %rax
xorq 0x150(%rax), %rcx
movq %rcx, 0x150(%rax)
movslq -0x14(%rbp), %rdx
leaq 0x1e83d(%rip), %rcx     # 0x268d0
movl (%rcx,%rdx,4), %edx
movl 0x148(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x148(%rax)
movslq -0x14(%rbp), %rdx
leaq 0x1e851(%rip), %rcx     # 0x26900
movl (%rcx,%rdx,4), %edx
movl 0x14c(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x14c(%rax)
movslq -0x10(%rbp), %rdx
leaq 0x1e805(%rip), %rcx     # 0x268d0
movl (%rcx,%rdx,4), %ecx
addl 0x148(%rax), %ecx
movl %ecx, 0x148(%rax)
movslq -0x10(%rbp), %rdx
leaq 0x1e81b(%rip), %rcx     # 0x26900
movl (%rcx,%rdx,4), %ecx
addl 0x14c(%rax), %ecx
movl %ecx, 0x14c(%rax)
movl -0x10(%rbp), %edx
movslq -0xc(%rbp), %rcx
movl %edx, 0x48(%rax,%rcx,4)
movl -0x14(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)