// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %r15
movq %rdi, %rbp
movq %rsi, %rdi
callq 0x785b
movl %eax, %ebx
movq %r15, %rdi
callq 0x7854
movl %eax, %r14d
movl %eax, %eax
cmpl $0x0, (%rbp,%rax,4)
jne 0xc947
movq %r15, %rdi
callq 0x7879
movl %eax, %edi
callq 0x4f30
movl %eax, 0x10(%rsp)
movl %eax, 0x30(%rsp)
movq %rbp, %rdi
movl %ebx, %esi
movl $0x1, %edx
callq 0x39eb
leaq 0x20(%rsp), %rcx
movq %rax, (%rcx)
movq %rbp, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x39eb
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movl 0x138(%rbp), %eax
movl %eax, 0xc(%rsp)
movl %ebx, %r13d
movl (%rbp,%r13,4), %r12d
movl %r12d, %edi
callq 0x4f30
movl %eax, %r15d
movl $0x1, %ebx
shlq $0x8, %r13
addq 0x3577c(%rip), %r13     # 0x41fa0
movq %r13, 0x28(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x14(%rsp)
movq %rbp, %r13
addl $0x6, %r12d
roll $0x1e, %r12d
cmpl $0x4, %r12d
jb 0xc862
movl %r14d, %eax
movq 0x28(%rsp), %rcx
movl (%rcx,%rax,4), %edx
movq %r13, %rdi
movl %r14d, %esi
leaq 0x20(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0xc658
leaq 0x20(%rsp), %r12
cmpl $0x1, 0xc(%rsp)
je 0xc873
leaq 0x18(%rsp), %r12
movq (%r12), %rsi
movq %r13, %rdi
callq 0xc5dc
cmpl $0x40, %eax
je 0xc8db
movl %eax, %r14d
movl %eax, %eax
leaq 0xedc20(%rip), %rcx     # 0xfa4b0
movq (%rcx,%rax,8), %rcx
xorq %rcx, (%r12)
movl (%r13,%rax,4), %r12d
testl %r12d, %r12d
je 0xc928
movq %r13, %rbp
subl 0x14(%rsp), %r15d
movl %r15d, 0x30(%rsp,%rbx,4)
incq %rbx
movl %r12d, %edi
callq 0x4f30
movl %eax, %r13d
movl 0xc(%rsp), %edi
callq 0xdf74
movl %eax, 0xc(%rsp)
movl %r15d, 0x14(%rsp)
movl %r13d, %r15d
jmp 0xc831
cmpq $0x2, %rbx
movl 0x10(%rsp), %eax
jb 0xc916
decq %rbx
leal -0x1(%rbx), %eax
xorl %ecx, %ecx
subl 0x30(%rsp,%rax,4), %ecx
movl %ebx, %edx
movl 0x30(%rsp,%rdx,4), %edx
cmpl %edx, %ecx
cmovlel %edx, %ecx
negl %ecx
movl %ecx, 0x30(%rsp,%rax,4)
leaq -0x1(%rbx), %rax
incq %rbx
cmpq $0x2, %rbx
movq %rax, %rbx
jg 0xc8e8
movl 0x30(%rsp), %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1faa4(%rip), %rdi     # 0x2c3d3
leaq 0x1f9cf(%rip), %rsi     # 0x2c305
leaq 0x1fa70(%rip), %rcx     # 0x2c3ad
movl $0x6c, %edx
callq 0x20d0
leaq 0x1fa41(%rip), %rdi     # 0x2c38f
leaq 0x1f9b0(%rip), %rsi     # 0x2c305
leaq 0x1fa51(%rip), %rcx     # 0x2c3ad
movl $0x4a, %edx
callq 0x20d0