// File: jswaff[P]prophet3/src/see.cpp
// Function: score_capture(position*, unsigned long long)
int32 score_capture(position *pos,move mv) {
    square_t to_sq = get_to_sq(mv);
    square_t from_sq = get_from_sq(mv);
    assert(pos->piece[from_sq]==NO_PIECE);

    // simulate the initial attack
    int32 scores[32];
    scores[0] = piece_val(get_captured_piece(mv));
    int32 scores_ind = 1;

    // play out the sequence
    uint64 white_attackers = get_attackers(pos,to_sq,WHITE);
    uint64 black_attackers = get_attackers(pos,to_sq,BLACK);

    uint64* attackers;

    color_t stm = pos->player;
    square_t current_sq = from_sq;
    piece_t current_piece = (piece_t)pos->piece[to_sq];
    int32 attacked_piece_val = piece_val(current_piece);

    while (true) {
        // xray attackers
        if (current_piece != KNIGHT && current_piece != -KNIGHT
                && current_piece != KING && current_piece != -KING)
        {
            dir_t dir = direction_tbl[to_sq][current_sq];
            add_xray_attackers(pos,current_sq,dir,&white_attackers,&black_attackers);
        }

        ///
        attackers = stm==WHITE ? &white_attackers : &black_attackers;
        current_sq = find_least_valuable(pos,*attackers);
        if (current_sq==NO_SQUARE) break;

        *attackers ^= bb_squares[current_sq];
        current_piece = (piece_t)pos->piece[current_sq];
        assert(current_piece != NO_PIECE);

        scores[scores_ind] = attacked_piece_val - scores[scores_ind-1];
        scores_ind++;
        attacked_piece_val = piece_val(current_piece);
        stm = opposite_player(stm);
    }

    // evaluate the sequence
    while (scores_ind > 1) {
        scores_ind--;
        scores[scores_ind-1] = -max(-scores[scores_ind-1],scores[scores_ind]);
    }

    return scores[0];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0xaaa0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rdi
callq 0xaa80
movl %eax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl -0x18(%rbp), %ecx
cmpl $0x0, (%rax,%rcx,4)
jne 0x11c7a
jmp 0x11c99
leaq 0x26641(%rip), %rdi     # 0x382c2
leaq 0x265b0(%rip), %rsi     # 0x38238
movl $0x4a, %edx
leaq 0x2664c(%rip), %rcx     # 0x382e0
callq 0x20c0
movq -0x10(%rbp), %rdi
callq 0xab00
movl %eax, %edi
callq 0x68c0
movl %eax, -0xa0(%rbp)
movl $0x1, -0xa4(%rbp)
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
movl $0x1, %edx
callq 0x4500
movq %rax, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %esi
xorl %edx, %edx
callq 0x4500
movq %rax, -0xb8(%rbp)
movq -0x8(%rbp), %rax
movl 0x138(%rax), %eax
movl %eax, -0xc4(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0xc8(%rbp)
movq -0x8(%rbp), %rax
movl -0x14(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %edi
callq 0x68c0
movl %eax, -0xd0(%rbp)
cmpl $0x2, -0xcc(%rbp)
je 0x11d89
cmpl $-0x2, -0xcc(%rbp)
je 0x11d89
cmpl $0x6, -0xcc(%rbp)
je 0x11d89
cmpl $-0x6, -0xcc(%rbp)
je 0x11d89
movl -0x14(%rbp), %eax
movl %eax, %ecx
leaq 0xbdd90(%rip), %rax     # 0xcfae0
shlq $0x8, %rcx
addq %rcx, %rax
movl -0xc8(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xd4(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc8(%rbp), %esi
movl -0xd4(%rbp), %edx
leaq -0xb0(%rbp), %rcx
leaq -0xb8(%rbp), %r8
callq 0x11a30
cmpl $0x1, -0xc4(%rbp)
jne 0x11da2
leaq -0xb0(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x11db2
leaq -0xb8(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x11db2
movq -0xe0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rax
movq (%rax), %rsi
callq 0x119b0
movl %eax, -0xc8(%rbp)
cmpl $0x40, -0xc8(%rbp)
jne 0x11de7
jmp 0x11ea0
movl -0xc8(%rbp), %eax
movl %eax, %ecx
leaq 0xf387a(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rcx
movq -0xc0(%rbp), %rax
xorq (%rax), %rcx
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movl -0xc8(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xcc(%rbp)
cmpl $0x0, -0xcc(%rbp)
je 0x11e25
jmp 0x11e44
leaq 0x264da(%rip), %rdi     # 0x38306
leaq 0x26405(%rip), %rsi     # 0x38238
movl $0x6c, %edx
leaq 0x264a1(%rip), %rcx     # 0x382e0
callq 0x20c0
movl -0xd0(%rbp), %ecx
movl -0xa4(%rbp), %eax
subl $0x1, %eax
cltq
subl -0xa0(%rbp,%rax,4), %ecx
movslq -0xa4(%rbp), %rax
movl %ecx, -0xa0(%rbp,%rax,4)
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
movl -0xcc(%rbp), %edi
callq 0x68c0
movl %eax, -0xd0(%rbp)
movl -0xc4(%rbp), %edi
callq 0x14130
movl %eax, -0xc4(%rbp)
jmp 0x11d20
jmp 0x11ea2
cmpl $0x1, -0xa4(%rbp)
jle 0x11ef9
movl -0xa4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
subl $0x1, %eax
cltq
xorl %edi, %edi
subl -0xa0(%rbp,%rax,4), %edi
movslq -0xa4(%rbp), %rax
movl -0xa0(%rbp,%rax,4), %esi
callq 0x11980
xorl %ecx, %ecx
subl %eax, %ecx
movl -0xa4(%rbp), %eax
subl $0x1, %eax
cltq
movl %ecx, -0xa0(%rbp,%rax,4)
jmp 0x11ea2
movl -0xa0(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax,%rax)