// File: /workspace/github_cpp_makefile_star2_O3/SethKasmann[P]clever-girl/src/state.cpp
// Function: State::see(unsigned short) const
int State::see(Move m) const
{
    Prop prop;
    Color color;
    Square src, dst, mayAttack;
    PieceType target;
    U64 attackers, from, occupancy, xRay, potential;
    int gain[32] = { 0 };
    int d = 0;

    color = mUs;
    src = getSrc(m);
    dst = getDst(m);
    from = square_bb[src];
    // Check if the move is en passant.
    if (onSquare(src) == pawn && square_bb[dst] & mEnPassant)
        gain[d] = PieceValue[pawn];
    else if (getPiecePromo(m) == queen)
        gain[d] = Queen_wt - Pawn_wt;
    else
        gain[d] = PieceValue[onSquare(dst)];
    occupancy = getOccupancyBB();
    attackers = allAttackers(dst);
    // Get X ray attacks and add in pawns from attackers.
    xRay = getXRayAttacks(dst);

    while (from)
    {
        // Update the target piece and the square it came from.
        src = get_lsb(from);
        target = onSquare(src);

        // Update the depth and color.
        d++;

        // Storing the potential gain, if defended.
        gain[d] = PieceValue[target] - gain[d - 1];

        // Prune if the gain cannot be improved.
        if (std::max(-gain[d - 1], gain[d]) < 0)
            break;

        // Remove the from bit to simulate making the move.
        attackers ^= from;
        occupancy ^= from;

        // If the target is not a night, piece movement could cause a discovered
        // attacker.
        if (target != knight)
        {
            xRay &= occupancy;
            // Create a bitboard of potential discovered attackers.
            potential = coplanar[src][dst] & xRay;
            while (potential)
            {
                mayAttack = pop_lsb(potential);
                // If no bits are between the new square and the dst, there
                // is a new attacker.
                if (!(between[mayAttack][dst] & occupancy))
                {
                    attackers |= square_bb[mayAttack];
                    break;
                }
            }
        }

        // Get the next piece for the opposing player.
        color = !color;
        if (!(attackers & getOccupancyBB(color)))
            break;

        if (attackers & getPieceBB<pawn>(color))
            from = get_lsb_bb(attackers & getPieceBB<pawn>(color));
        else if (attackers & getPieceBB<knight>(color))
            from = get_lsb_bb(attackers & getPieceBB<knight>(color));
        else if (attackers & getPieceBB<bishop>(color))
            from = get_lsb_bb(attackers & getPieceBB<bishop>(color));
        else if (attackers & getPieceBB<rook>(color))
            from = get_lsb_bb(attackers & getPieceBB<rook>(color));
        else if (attackers & getPieceBB<queen>(color))
            from = get_lsb_bb(attackers & getPieceBB<queen>(color));
        else
        {
            from = get_lsb_bb(attackers & getPieceBB<king>(color));
            // Break if the king would be attacked.
            if (attackers & getOccupancyBB(!color))
                break;
        }
    }

    // Negamax the gain array to determine the final SEE value.
    while (--d > 0)
        gain[d - 1] = -std::max(-gain[d - 1], gain[d]);

    return gain[0];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
movaps %xmm0, -0x10(%rsp)
movaps %xmm0, -0x20(%rsp)
movaps %xmm0, -0x30(%rsp)
movaps %xmm0, -0x40(%rsp)
movaps %xmm0, -0x50(%rsp)
movaps %xmm0, -0x60(%rsp)
movl %esi, %eax
shrl $0x6, %eax
andl $0x3f, %eax
leaq 0xe13b7(%rip), %rdx     # 0xf8a70
movl %esi, %ecx
andl $0x3f, %ecx
movq (%rdx,%rcx,8), %rbx
cmpl $0x0, 0x188(%rdi,%rcx,4)
je 0x17709
andl $0x7000, %esi           # imm = 0x7000
cmpl $0x4000, %esi           # imm = 0x4000
jne 0x176ed
movl $0x352, -0x60(%rsp)     # imm = 0x352
movzwl %ax, %edx
movl $0x352, %r9d            # imm = 0x352
jmp 0x1772b
movzwl %ax, %edx
movl 0x188(%rdi,%rdx,4), %eax
leaq 0x6ba2(%rip), %rcx      # 0x1e2a0
movl (%rcx,%rax,4), %r9d
movl %r9d, -0x60(%rsp)
jmp 0x1772b
movzwl %ax, %edx
movq 0x30(%rdi), %rcx
leaq 0xe1359(%rip), %r8      # 0xf8a70
testq %rcx, (%r8,%rdx,8)
je 0x176cc
movl $0x64, -0x60(%rsp)
movl $0x64, %r9d
testq %rbx, %rbx
je 0x17a27
movl (%rdi), %r11d
movq 0x2a0(%rdi), %rax
orq 0x2d0(%rdi), %rax
leaq 0x288(%rdi), %rsi
movq 0x2d8(%rdi), %rcx
orq 0x2a8(%rdi), %rcx
leal (,%rdx,8), %r14d
orq %rcx, %rax
movq %rax, -0x78(%rsp)
leaq 0x102f0f(%rip), %r8     # 0x11a680
movq 0x2c8(%rdi), %r15
orq 0x298(%rdi), %r15
orq %rcx, %r15
movq (%r8,%r14), %r13
andq %r15, %r13
leaq (%r11,%r11,2), %rcx
shlq $0x4, %rcx
leaq 0x40a8(%rip), %r8       # 0x1b840
movq 0x80(%rdi), %r10
orq 0x78(%rdi), %r10
movq (%r8,%r14), %r8
andq %r10, %r8
leaq 0x3e8f(%rip), %r12      # 0x1b640
imulq (%r12,%r14), %r8
movq (%rsi,%rcx), %rax
leal (,%rdx,4), %r12d
leaq 0x3d77(%rip), %rcx      # 0x1b540
movb (%rcx,%r12), %cl
shrq %cl, %r8
leaq 0xda39(%rip), %rcx      # 0x25210
movq (%rcx,%r14), %rcx
andq (%rcx,%r8,8), %r15
movl 0x4(%rdi), %ecx
leaq (%rcx,%rcx,2), %r8
shlq $0x9, %rcx
leaq 0xe187f(%rip), %rbp     # 0xf9070
addq %rbp, %rcx
andq (%r14,%rcx), %rax
movq %rax, -0x68(%rsp)
movq %r11, %rcx
shlq $0x9, %rcx
addq %rbp, %rcx
leaq 0x103072(%rip), %rbp    # 0x11a880
movq (%rbp,%r14), %rax
shlq $0x4, %r8
movq (%rsi,%r8), %r8
andq (%r14,%rcx), %r8
andq -0x78(%rsp), %rax
movq 0x2c0(%rdi), %rbp
orq 0x290(%rdi), %rbp
orq %rax, %r13
movq %r13, -0x70(%rsp)
leaq 0x41ff(%rip), %rax      # 0x1ba40
andq (%rax,%r14), %rbp
leaq 0x37f4(%rip), %rax      # 0x1b040
movb (%rax,%r12), %cl
leaq 0x3ae9(%rip), %rax      # 0x1b340
movq (%rax,%r14), %rax
andq %r10, %rax
leaq 0x38db(%rip), %r12      # 0x1b140
imulq (%r12,%r14), %rax
shrq %cl, %rax
leaq 0xdb9c(%rip), %rcx      # 0x25410
movq (%rcx,%r14), %rcx
movq -0x78(%rsp), %r12
andq (%rcx,%rax,8), %r12
orq -0x68(%rsp), %r8
movq 0x2e0(%rdi), %rcx
orq 0x2b0(%rdi), %rcx
orq %r8, %rbp
leaq 0x43a2(%rip), %rax      # 0x1bc40
andq (%rax,%r14), %rcx
orq %rbp, %r15
orq %r12, %rcx
orq %r15, %rcx
movl $0x1, %r8d
xorl %eax, %eax
leaq 0xf23b6(%rip), %r13     # 0x109c70
movl %r11d, %r12d
movq %rax, %r14
movq %rsi, %r15
movl %r9d, %esi
movl %r8d, %ebp
bsfq %rbx, %r8
movl 0x188(%rdi,%r8,4), %r11d
incq %rax
leaq 0x69c1(%rip), %r9       # 0x1e2a0
movl (%r9,%r11,4), %r9d
subl %esi, %r9d
movl %r9d, -0x5c(%rsp,%r14,4)
negl %esi
cmpl %esi, %r9d
cmovgl %r9d, %esi
testl %esi, %esi
js 0x179fb
xorq %rbx, %rcx
xorq %rbx, %r10
cmpl $0x1, %r11d
je 0x17954
movq -0x70(%rsp), %rsi
andq %r10, %rsi
shll $0x9, %r8d
addq 0xd64d(%rip), %r8       # 0x24f68
movq (%r8,%rdx,8), %r8
movq %rsi, -0x70(%rsp)
andq %rsi, %r8
testq %r8, %r8
je 0x17954
leaq -0x1(%r8), %rsi
andq %r8, %rsi
bsfq %r8, %r11
movl %r11d, %ebx
shll $0x9, %ebx
addq %r13, %rbx
movq %rsi, %r8
testq %r10, (%rbx,%rdx,8)
jne 0x17927
leaq 0xe1120(%rip), %rsi     # 0xf8a70
orq (%rsi,%r11,8), %rcx
xorl %r11d, %r11d
testl %r12d, %r12d
sete %r11b
testq %rcx, 0x78(%rdi,%r11,8)
je 0x179fb
leaq (%r11,%r11,2), %rbx
movl %ebx, %esi
shll $0x4, %esi
movq (%r15,%rsi), %r8
andq %rcx, %r8
jne 0x179b7
movl %ebx, %esi
shll $0x4, %esi
movq 0x8(%r15,%rsi), %r8
andq %rcx, %r8
jne 0x179b7
movl %ebx, %esi
shll $0x4, %esi
movq 0x10(%r15,%rsi), %r8
andq %rcx, %r8
jne 0x179b7
movl %ebx, %esi
shll $0x4, %esi
movq 0x18(%r15,%rsi), %r8
andq %rcx, %r8
jne 0x179b7
movl %ebx, %esi
shll $0x4, %esi
movq 0x20(%r15,%rsi), %r8
andq %rcx, %r8
je 0x179d2
movq %r15, %rsi
movq %r8, %rbx
negq %rbx
andq %r8, %rbx
leal 0x1(%rbp), %r8d
testq %rbx, %rbx
jne 0x178ba
jmp 0x179fb
xorl %esi, %esi
testl %r12d, %r12d
setne %sil
testq %rcx, 0x78(%rdi,%rsi,8)
jne 0x179fb
shll $0x4, %ebx
movq 0x28(%r15,%rbx), %rsi
andq %rcx, %rsi
movq %rsi, %rbx
negq %rbx
andq %rsi, %rbx
movq %r15, %rsi
jmp 0x179c3
testq %r14, %r14
je 0x17a27
movl %eax, %eax
movl -0x64(%rsp,%rax,4), %ecx
movl %ebp, %eax
incq %rax
xorl %edx, %edx
subl -0x6c(%rsp,%rax,4), %edx
cmpl %edx, %ecx
cmovgl %ecx, %edx
negl %edx
movl %edx, -0x6c(%rsp,%rax,4)
decq %rax
movl %edx, %ecx
cmpq $0x2, %rax
ja 0x17a0b
movl -0x60(%rsp), %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq