// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rdi, %r13
movq %rsi, %rdi
callq 0x7372
movl %eax, %r14d
movq %r15, %rdi
callq 0x736b
movl %eax, %ebp
movl %eax, %eax
cmpl $0x0, (%r13,%rax,4)
jne 0xbef2
movq %r15, %rdi
callq 0x7390
movl %eax, %edi
callq 0x4c9c
movl %eax, 0x4(%rsp)
movl %eax, 0x20(%rsp)
pushq $0x1
popq %rdx
movq %r13, %rdi
movl %r14d, %esi
callq 0x37fc
leaq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq %r13, %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0x37fc
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl 0x138(%r13), %eax
movl %eax, (%rsp)
movl %r14d, %r15d
movl (%r13,%r15,4), %r14d
movl %r14d, %edi
callq 0x4c9c
movl %eax, %r12d
pushq $0x1
popq %rbx
shlq $0x8, %r15
addq 0x391a2(%rip), %r15     # 0x44fa0
movq %r15, 0x18(%rsp)
addl $0x6, %r14d
roll $0x1e, %r14d
cmpl $0x4, %r14d
jb 0xbe2f
movl %ebp, %eax
movq 0x18(%rsp), %rcx
movl (%rcx,%rax,4), %edx
movq %r13, %rdi
movl %ebp, %esi
leaq 0x10(%rsp), %rcx
leaq 0x8(%rsp), %r8
callq 0xbc3f
leaq 0x10(%rsp), %r14
cmpl $0x1, (%rsp)
je 0xbe3f
leaq 0x8(%rsp), %r14
movq (%r14), %rsi
movq %r13, %rdi
callq 0xbbd8
cmpl $0x40, %eax
je 0xbeb9
movl %eax, %ebp
movl %eax, %eax
leaq 0xf1656(%rip), %rcx     # 0xfd4b0
movq (%rcx,%rax,8), %rcx
xorq %rcx, (%r14)
movl (%r13,%rax,4), %r14d
testl %r14d, %r14d
je 0xbed5
movq %r13, %r15
subl 0x4(%rsp), %r12d
movl %r12d, 0x20(%rsp,%rbx,4)
incq %rbx
movl %r14d, %edi
callq 0x4c9c
movl %eax, %r13d
movl (%rsp), %edi
callq 0xd4d8
movl %eax, (%rsp)
movl %r12d, 0x4(%rsp)
movl %r13d, %r12d
movq %r15, %r13
jmp 0xbe03
xorl %eax, %eax
subl 0x18(%rsp,%rbx,4), %eax
movl 0x1c(%rsp,%rbx,4), %ecx
cmpl %ecx, %eax
cmovlel %ecx, %eax
negl %eax
movl %eax, 0x18(%rsp,%rbx,4)
decq %rbx
cmpq $0x2, %rbx
jge 0xbea1
movl 0x20(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e4a7(%rip), %rdi     # 0x2a383
leaq 0x1e3d2(%rip), %rsi     # 0x2a2b5
leaq 0x1e473(%rip), %rcx     # 0x2a35d
pushq $0x6c
popq %rdx
callq 0x20c0
leaq 0x1e446(%rip), %rdi     # 0x2a33f
leaq 0x1e3b5(%rip), %rsi     # 0x2a2b5
leaq 0x1e456(%rip), %rcx     # 0x2a35d
pushq $0x4a
jmp 0xbeec