// File: jswaff[P]prophet3/src/pos.cpp
// Function: remove_piece(position*, square_t)
piece_t remove_piece(position* p,square_t sq) {
    assert(p != 0);
    assert(sq >= A8 && sq <= H1);
    assert(p->piece[sq] != NO_PIECE);

    int piece = p->piece[sq];
    uint64 bb_sq = bb_squares[sq];
    if (piece > NO_PIECE) {
        p->white_pieces ^= bb_sq;
        if (piece==PAWN) {
            p->white_pawns ^= bb_sq;
            p->pawn_key ^= zkeys.pieces[PAWN][WHITE][sq];
        } else if (piece==KNIGHT) {
            p->white_knights ^= bb_sq;
        } else if (piece==BISHOP) {
            p->white_bishops ^= bb_sq;
        } else if (piece==ROOK) {
            p->white_rooks ^= bb_sq;
        } else if (piece==QUEEN) {
            p->white_queens ^= bb_sq;
        }
        p->piece_counts[WHITE][piece]--;
        assert(p->piece_counts[WHITE][piece] >= 0);
        p->hash_key ^= zkeys.pieces[piece][WHITE][sq];
    } else {
        p->black_pieces ^= bb_sq;
        if (piece==-PAWN) {
            p->black_pawns ^= bb_sq;
            p->pawn_key ^= zkeys.pieces[PAWN][BLACK][sq];
        } else if (piece==-KNIGHT) {
            p->black_knights ^= bb_sq;
        } else if (piece==-BISHOP) {
            p->black_bishops ^= bb_sq;
        } else if (piece==-ROOK) {
            p->black_rooks ^= bb_sq;
        } else if (piece==-QUEEN) {
            p->black_queens ^= bb_sq;
        }
        p->piece_counts[BLACK][-piece]--;
        assert(p->piece_counts[BLACK][-piece] >= 0);
        p->hash_key ^= zkeys.pieces[-piece][BLACK][sq];
    }

    p->piece[sq] = NO_PIECE;
    return (piece_t)piece;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0xee58
jmp 0xee77
leaq 0x28cd9(%rip), %rdi     # 0x37b38
leaq 0x28814(%rip), %rsi     # 0x3767a
movl $0x7b, %edx
leaq 0x28ccd(%rip), %rcx     # 0x37b3f
callq 0x20c0
xorl %eax, %eax
cmpl $0x0, -0xc(%rbp)
movb %al, -0x19(%rbp)
jl 0xee8c
cmpl $0x3f, -0xc(%rbp)
setle %al
movb %al, -0x19(%rbp)
movb -0x19(%rbp), %al
testb $0x1, %al
jne 0xee95
jmp 0xee97
jmp 0xeeb6
leaq 0x263a4(%rip), %rdi     # 0x35242
leaq 0x287d5(%rip), %rsi     # 0x3767a
movl $0x7c, %edx
leaq 0x28c8e(%rip), %rcx     # 0x37b3f
callq 0x20c0
movq -0x8(%rbp), %rax
movl -0xc(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
je 0xeec5
jmp 0xeee4
leaq 0x28c9e(%rip), %rdi     # 0x37b6a
leaq 0x287a7(%rip), %rsi     # 0x3767a
movl $0x7d, %edx
leaq 0x28c60(%rip), %rcx     # 0x37b3f
callq 0x20c0
movq -0x8(%rbp), %rax
movl -0xc(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xf6773(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rax
movq %rax, -0x18(%rbp)
cmpl $0x0, -0x10(%rbp)
jle 0xf067
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x1a8(%rax), %rcx
movq %rcx, 0x1a8(%rax)
cmpl $0x1, -0x10(%rbp)
jne 0xef69
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x158(%rax), %rcx
movq %rcx, 0x158(%rax)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xf480b(%rip), %rax     # 0x103758
movq 0x600(%rax,%rcx,8), %rcx
movq -0x8(%rbp), %rax
xorq 0x1c0(%rax), %rcx
movq %rcx, 0x1c0(%rax)
jmp 0xefe7
cmpl $0x2, -0x10(%rbp)
jne 0xef87
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x168(%rax), %rcx
movq %rcx, 0x168(%rax)
jmp 0xefe5
cmpl $0x3, -0x10(%rbp)
jne 0xefa5
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x178(%rax), %rcx
movq %rcx, 0x178(%rax)
jmp 0xefe3
cmpl $0x4, -0x10(%rbp)
jne 0xefc3
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x188(%rax), %rcx
movq %rcx, 0x188(%rax)
jmp 0xefe1
cmpl $0x5, -0x10(%rbp)
jne 0xefdf
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x198(%rax), %rcx
movq %rcx, 0x198(%rax)
jmp 0xefe1
jmp 0xefe3
jmp 0xefe5
jmp 0xefe7
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
movl 0x11c(%rax,%rcx,4), %edx
addl $-0x1, %edx
movl %edx, 0x11c(%rax,%rcx,4)
movq -0x8(%rbp), %rax
movslq -0x10(%rbp), %rcx
cmpl $0x0, 0x11c(%rax,%rcx,4)
jb 0xf014
jmp 0xf033
leaq 0x28b68(%rip), %rdi     # 0x37b83
leaq 0x28658(%rip), %rsi     # 0x3767a
movl $0x90, %edx
leaq 0x28b11(%rip), %rcx     # 0x37b3f
callq 0x20c0
movslq -0x10(%rbp), %rcx
leaq 0xf471a(%rip), %rax     # 0x103758
shlq $0xa, %rcx
addq %rcx, %rax
movl -0xc(%rbp), %ecx
movq 0x200(%rax,%rcx,8), %rcx
movq -0x8(%rbp), %rax
xorq 0x1b8(%rax), %rcx
movq %rcx, 0x1b8(%rax)
jmp 0xf1c2
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x1b0(%rax), %rcx
movq %rcx, 0x1b0(%rax)
cmpl $-0x1, -0x10(%rbp)
jne 0xf0c1
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x160(%rax), %rcx
movq %rcx, 0x160(%rax)
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0xf46b3(%rip), %rax     # 0x103758
movq 0x400(%rax,%rcx,8), %rcx
movq -0x8(%rbp), %rax
xorq 0x1c0(%rax), %rcx
movq %rcx, 0x1c0(%rax)
jmp 0xf13f
cmpl $-0x2, -0x10(%rbp)
jne 0xf0df
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x170(%rax), %rcx
movq %rcx, 0x170(%rax)
jmp 0xf13d
cmpl $-0x3, -0x10(%rbp)
jne 0xf0fd
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x180(%rax), %rcx
movq %rcx, 0x180(%rax)
jmp 0xf13b
cmpl $-0x4, -0x10(%rbp)
jne 0xf11b
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x190(%rax), %rcx
movq %rcx, 0x190(%rax)
jmp 0xf139
cmpl $-0x5, -0x10(%rbp)
jne 0xf137
movq -0x18(%rbp), %rcx
movq -0x8(%rbp), %rax
xorq 0x1a0(%rax), %rcx
movq %rcx, 0x1a0(%rax)
jmp 0xf139
jmp 0xf13b
jmp 0xf13d
jmp 0xf13f
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
subl -0x10(%rbp), %ecx
movslq %ecx, %rcx
movl 0x100(%rax,%rcx,4), %edx
addl $-0x1, %edx
movl %edx, 0x100(%rax,%rcx,4)
movq -0x8(%rbp), %rax
xorl %ecx, %ecx
subl -0x10(%rbp), %ecx
movslq %ecx, %rcx
cmpl $0x0, 0x100(%rax,%rcx,4)
jb 0xf174
jmp 0xf193
leaq 0x28a2b(%rip), %rdi     # 0x37ba6
leaq 0x284f8(%rip), %rsi     # 0x3767a
movl $0xa1, %edx
leaq 0x289b1(%rip), %rcx     # 0x37b3f
callq 0x20c0
xorl %eax, %eax
subl -0x10(%rbp), %eax
movslq %eax, %rcx
leaq 0xf45b6(%rip), %rax     # 0x103758
shlq $0xa, %rcx
addq %rcx, %rax
movl -0xc(%rbp), %ecx
movq (%rax,%rcx,8), %rcx
movq -0x8(%rbp), %rax
xorq 0x1b8(%rax), %rcx
movq %rcx, 0x1b8(%rax)
movq -0x8(%rbp), %rax
movl -0xc(%rbp), %ecx
movl $0x0, (%rax,%rcx,4)
movl -0x10(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)