// File: /workspace/github_cpp_makefile_star2_O1/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 $0xa8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq %rsi, %rdi
callq 0x77b7
movl %eax, %r15d
movq %r14, %rdi
callq 0x77b0
movl %eax, %ebp
movl %eax, %eax
cmpl $0x0, (%rbx,%rax,4)
jne 0xc952
movq %r14, %rdi
callq 0x77d5
movl %eax, %edi
callq 0x4ed8
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x39df
leaq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0x39df
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movl 0x138(%rbx), %r12d
movl %r15d, %eax
movq %rax, 0x8(%rsp)
movl (%rbx,%rax,4), %r15d
movl %r15d, %edi
callq 0x4ed8
movl %eax, %r13d
movl $0x1, %r14d
movq 0x8(%rsp), %rax
shlq $0x8, %rax
addq 0x3574c(%rip), %rax     # 0x41fa0
movq %rax, 0x8(%rsp)
addl $0x6, %r15d
roll $0x1e, %r15d
cmpl $0x4, %r15d
jb 0xc885
movl %ebp, %eax
movq 0x8(%rsp), %rcx
movl (%rcx,%rax,4), %edx
movq %rbx, %rdi
movl %ebp, %esi
leaq 0x18(%rsp), %rcx
leaq 0x10(%rsp), %r8
callq 0xc684
leaq 0x18(%rsp), %r15
cmpl $0x1, %r12d
je 0xc895
leaq 0x10(%rsp), %r15
movq (%r15), %rsi
movq %rbx, %rdi
callq 0xc608
cmpl $0x40, %eax
je 0xc8e8
movl %eax, %ebp
movl %eax, %eax
leaq 0xedc00(%rip), %rcx     # 0xfa4b0
movq (%rcx,%rax,8), %rcx
xorq %rcx, (%r15)
movl (%rbx,%rax,4), %r15d
testl %r15d, %r15d
je 0xc933
subl 0x1c(%rsp,%r14,4), %r13d
movl %r13d, 0x20(%rsp,%r14,4)
incq %r14
movl %r15d, %edi
callq 0x4ed8
movl %eax, %r13d
movl %r12d, %edi
callq 0xdfc4
movl %eax, %r12d
jmp 0xc859
cmpq $0x2, %r14
jb 0xc91d
decq %r14
leal -0x1(%r14), %eax
xorl %ecx, %ecx
subl 0x20(%rsp,%rax,4), %ecx
movl %r14d, %edx
movl 0x20(%rsp,%rdx,4), %edx
cmpl %edx, %ecx
cmovlel %edx, %ecx
negl %ecx
movl %ecx, 0x20(%rsp,%rax,4)
leaq -0x1(%r14), %rax
incq %r14
cmpq $0x2, %r14
movq %rax, %r14
jg 0xc8f1
movl 0x20(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1fa19(%rip), %rdi     # 0x2c353
leaq 0x1f944(%rip), %rsi     # 0x2c285
leaq 0x1f9e5(%rip), %rcx     # 0x2c32d
movl $0x6c, %edx
callq 0x20d0
leaq 0x1f9b6(%rip), %rdi     # 0x2c30f
leaq 0x1f925(%rip), %rsi     # 0x2c285
leaq 0x1f9c6(%rip), %rcx     # 0x2c32d
movl $0x4a, %edx
callq 0x20d0