// File: jswaff[P]prophet3/src/apply.cpp
// Function: add_piece_to_destination(position*, unsigned long long)
void add_piece_to_destination(position* p,move m) {
    square_t from_sq=get_from_sq(m),to_sq=get_to_sq(m);
    int32 piece = p->piece[from_sq];
    add_piece(p,piece,to_sq);
    switch (piece) {
    case PAWN:
        p->fifty_counter=0;
        if (to_sq==north(north(from_sq))) {
            p->ep_sq=north(from_sq);
        } else if (get_rank(to_sq)==RANK_8) {
            assert(get_promopiece(m) != NO_PIECE);
            remove_piece(p,to_sq);
            add_piece(p,get_promopiece(m),to_sq);
        }
        break;
    case -PAWN:
        p->fifty_counter=0;
        if (to_sq==south(south(from_sq))) {
            p->ep_sq=south(from_sq);
        } else if (get_rank(to_sq)==RANK_1) {
            assert(get_promopiece(m) != NO_PIECE);
            remove_piece(p,to_sq);
            add_piece(p,-get_promopiece(m),to_sq);
        }
        break;
    case KING:
        p->white_king=to_sq;
        // move rook if this is a castle
        if (from_sq==E1) {
            if (to_sq==G1) {
                assert(is_castle(m));
                p->fifty_counter=0;
                remove_piece(p,H1);
                add_piece(p,ROOK,F1);
            } else if (to_sq==C1) {
                assert(is_castle(m));
                p->fifty_counter=0;
                remove_piece(p,A1);
                add_piece(p,ROOK,D1);
            }
        }
        break;
    case -KING:
        p->black_king=to_sq;
        if (from_sq==E8) {
            if (to_sq==G8) {
                assert(is_castle(m));
                p->fifty_counter=0;
                remove_piece(p,H8);
                add_piece(p,-ROOK,F8);
            } else if (to_sq==C8) {
                assert(is_castle(m));
                p->fifty_counter=0;
                remove_piece(p,A8);
                add_piece(p,-ROOK,D8);
            }
        }
        break;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xaa80
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rdi
callq 0xaaa0
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl -0x14(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x18(%rbp), %edx
callq 0xeb20
movl -0x1c(%rbp), %eax
addl $0x6, %eax
movl %eax, %ecx
movq %rcx, -0x28(%rbp)
subl $0xc, %eax
ja 0x3611
movq -0x28(%rbp), %rax
leaq 0x31d30(%rip), %rcx     # 0x35004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movl $0x0, 0x148(%rax)
movl -0x18(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %edi
callq 0x12410
movl %eax, %edi
callq 0x12410
movl %eax, %ecx
movl -0x2c(%rbp), %eax
cmpl %ecx, %eax
jne 0x331f
movl -0x14(%rbp), %edi
callq 0x12410
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x13c(%rax)
jmp 0x3388
movl -0x18(%rbp), %edi
callq 0x120d0
cmpl $0x0, %eax
jne 0x3386
movq -0x10(%rbp), %rdi
callq 0xaae0
cmpl $0x0, %eax
je 0x333c
jmp 0x335b
leaq 0x31d75(%rip), %rdi     # 0x350b8
leaq 0x31cfc(%rip), %rsi     # 0x35046
movl $0x33, %edx
leaq 0x31d80(%rip), %rcx     # 0x350d6
callq 0x20c0
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0xee40
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0xaae0
movq -0x38(%rbp), %rdi
movl %eax, %esi
movl -0x18(%rbp), %edx
callq 0xeb20
jmp 0x3388
jmp 0x3611
movq -0x8(%rbp), %rax
movl $0x0, 0x148(%rax)
movl -0x18(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x14(%rbp), %edi
callq 0x12490
movl %eax, %edi
callq 0x12490
movl %eax, %ecx
movl -0x3c(%rbp), %eax
cmpl %ecx, %eax
jne 0x33cf
movl -0x14(%rbp), %edi
callq 0x12490
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x13c(%rax)
jmp 0x343a
movl -0x18(%rbp), %edi
callq 0x120d0
cmpl $0x7, %eax
jne 0x3438
movq -0x10(%rbp), %rdi
callq 0xaae0
cmpl $0x0, %eax
je 0x33ec
jmp 0x340b
leaq 0x31cc5(%rip), %rdi     # 0x350b8
leaq 0x31c4c(%rip), %rsi     # 0x35046
movl $0x3d, %edx
leaq 0x31cd0(%rip), %rcx     # 0x350d6
callq 0x20c0
movq -0x8(%rbp), %rdi
movl -0x18(%rbp), %esi
callq 0xee40
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0xaae0
movq -0x48(%rbp), %rdi
xorl %esi, %esi
subl %eax, %esi
movl -0x18(%rbp), %edx
callq 0xeb20
jmp 0x343a
jmp 0x3611
movl -0x18(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x14c(%rax)
cmpl $0x3c, -0x14(%rbp)
jne 0x3526
cmpl $0x3e, -0x18(%rbp)
jne 0x34bd
movq -0x10(%rbp), %rdi
callq 0xab70
testb $0x1, %al
jne 0x346b
jmp 0x346d
jmp 0x348c
leaq 0x31c92(%rip), %rdi     # 0x35106
leaq 0x31bcb(%rip), %rsi     # 0x35046
movl $0x47, %edx
leaq 0x31c4f(%rip), %rcx     # 0x350d6
callq 0x20c0
movq -0x8(%rbp), %rax
movl $0x0, 0x148(%rax)
movq -0x8(%rbp), %rdi
movl $0x3f, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0x4, %esi
movl $0x3d, %edx
callq 0xeb20
jmp 0x3524
cmpl $0x3a, -0x18(%rbp)
jne 0x3522
movq -0x10(%rbp), %rdi
callq 0xab70
testb $0x1, %al
jne 0x34d2
jmp 0x34d4
jmp 0x34f3
leaq 0x31c2b(%rip), %rdi     # 0x35106
leaq 0x31b64(%rip), %rsi     # 0x35046
movl $0x4c, %edx
leaq 0x31be8(%rip), %rcx     # 0x350d6
callq 0x20c0
movq -0x8(%rbp), %rax
movl $0x0, 0x148(%rax)
movq -0x8(%rbp), %rdi
movl $0x38, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0x4, %esi
movl $0x3b, %edx
callq 0xeb20
jmp 0x3524
jmp 0x3526
jmp 0x3611
movl -0x18(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x150(%rax)
cmpl $0x4, -0x14(%rbp)
jne 0x360f
cmpl $0x6, -0x18(%rbp)
jne 0x35a9
movq -0x10(%rbp), %rdi
callq 0xab70
testb $0x1, %al
jne 0x3557
jmp 0x3559
jmp 0x3578
leaq 0x31ba6(%rip), %rdi     # 0x35106
leaq 0x31adf(%rip), %rsi     # 0x35046
movl $0x57, %edx
leaq 0x31b63(%rip), %rcx     # 0x350d6
callq 0x20c0
movq -0x8(%rbp), %rax
movl $0x0, 0x148(%rax)
movq -0x8(%rbp), %rdi
movl $0x7, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
movl $0x5, %edx
callq 0xeb20
jmp 0x360d
cmpl $0x2, -0x18(%rbp)
jne 0x360b
movq -0x10(%rbp), %rdi
callq 0xab70
testb $0x1, %al
jne 0x35be
jmp 0x35c0
jmp 0x35df
leaq 0x31b3f(%rip), %rdi     # 0x35106
leaq 0x31a78(%rip), %rsi     # 0x35046
movl $0x5c, %edx
leaq 0x31afc(%rip), %rcx     # 0x350d6
callq 0x20c0
movq -0x8(%rbp), %rax
movl $0x0, 0x148(%rax)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0xee40
movq -0x8(%rbp), %rdi
movl $0xfffffffc, %esi       # imm = 0xFFFFFFFC
movl $0x3, %edx
callq 0xeb20
jmp 0x360d
jmp 0x360f
jmp 0x3611
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)