// File: /workspace/github_cpp_makefile_star2_O2/SethKasmann[P]clever-girl/src/evaluation.cpp
// Function: Evaluate::evalPieces(Color)
void Evaluate::evalPieces(const Color c)
{
    U64 moves, pins;
    U64 mobilityNet;
    int king_threats = 0;
    U64 bottomRank = c == white ? Rank_1 : Rank_8;
    Square kingSq = mState.getKingSquare(c);

    // The mobilityNet is all empty squares not attacked by enemy pawns.
    mobilityNet = mState.getEmptyBB();
    if (c == white)
        mobilityNet &= ~((mState.getPieceBB<pawn>(black) & Not_a_file) >> 7
                       | (mState.getPieceBB<pawn>(black) & Not_h_file) >> 9);
    else
        mobilityNet &= ~((mState.getPieceBB<pawn>(white) & Not_a_file) << 9
                       | (mState.getPieceBB<pawn>(white) & Not_h_file) << 7);

    // Get the pinned pieces for the current player.
    pins = mState.getPinsBB(c);

    // Knight evaluation.
    for (Square p : mState.getPieceList<knight>(c))
    {
        if (p == no_sq)
            break;
        mMaterial[c] += Knight_wt;
        mMaterial[c] += outpost<knight>(p, c);

        if (square_bb[p] & pins)
        {
            mMobility[c] += knightMobility[0];
            continue;
        }

        moves = mState.getAttackBB<knight>(p);
        mPieceAttacksBB[c][knight] |= moves & mState.getOccupancyBB();
        mAllAttacksBB[c] |= mPieceAttacksBB[c][knight];

        mMobility[c] += knightMobility[pop_count(moves & mobilityNet)];

        if (moves & king_net_bb[!c][mState.getKingSquare(!c)] & mobilityNet)
            king_threats += Knight_th;
    }

    // Bishop evaluation.
    for (Square p : mState.getPieceList<bishop>(c))
    {
        if (p == no_sq)
            break;
        mMaterial[c] += Bishop_wt;
        mMaterial[c] += outpost<bishop>(p, c);

        moves = mState.getAttackBB<bishop>(p);

        if (square_bb[p] & pins)
            moves &= coplanar[p][kingSq];

        mPieceAttacksBB[c][bishop] = moves & mState.getOccupancyBB();
        mAllAttacksBB[c] |= mPieceAttacksBB[c][bishop];

        if (moves & king_net_bb[!c][mState.getKingSquare(!c)] & mobilityNet)
            king_threats += Bishop_th;

        mMobility[c] += bishopMobility[pop_count(moves & mobilityNet)];

        // Check for a bad bishop (penalty for pawns on the same square);
        mMaterial[c] += BadBishop * 
            pop_count(squares_of_color(p) & mState.getPieceBB<pawn>(c));
    }

    // Rook evaluation.
    for (Square p : mState.getPieceList<rook>(c))
    {
        if (p == no_sq)
            break;
        mMaterial[c] += Rook_wt;

        moves = mState.getAttackBB<rook>(p);
        if (square_bb[p] & pins)
            moves &= coplanar[p][kingSq];

        mPieceAttacksBB[c][rook] = moves & mState.getOccupancyBB();
        mAllAttacksBB[c] |= mPieceAttacksBB[c][rook];

        if (moves & king_net_bb[!c][mState.getKingSquare(!c)] & mobilityNet)
            king_threats += Rook_th;

        mMobility[c] += rookMobility[pop_count(moves & mobilityNet)];

        // Check if the rook is trapped.
        // TODO: more testing to confirm this is working propertly.
        if (mState.getPieceBB<king>(c) & bottomRank 
            && square_bb[p] & bottomRank)
        {
            if ((kingSq > p && !mState.canCastleKingside(c) &&
                square_bb[kingSq] & Rightside) ||
                (kingSq < p && !mState.canCastleQueenside(c) &&
                square_bb[kingSq] & Leftside)) 
            {
                if (pop_count(moves & mobilityNet & ~(bottomRank)) <= 3)
                    mMaterial[c] += TrappedRook;
            }
        }
    }

    // Queen evaluation.
    for (Square p : mState.getPieceList<queen>(c))
    {
        if (p == no_sq)
            break;
        mMaterial[c] += Queen_wt;
        moves = mState.getAttackBB<queen>(p);
        if (square_bb[p] & pins)
            moves &= coplanar[p][kingSq];

        mPieceAttacksBB[c][queen] = moves & mState.getOccupancyBB();
        mAllAttacksBB[c] |= mPieceAttacksBB[c][queen];

        if (moves & king_net_bb[!c][mState.getKingSquare(!c)] & mobilityNet)
            king_threats += Queen_th;

        // Calculate queen mobility.
        mMobility[c] += queenMobility[pop_count(moves & mobilityNet)];
    }

    // King evaluation.
    mKingSafety[!c] -= Safety_table[king_threats];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, %r10d
movq %rdi, %rbx
movq 0x8(%rdi), %rax
leaq 0x328(%rax), %rcx
movl %esi, %r15d
imulq $0xf0, %r15, %r9
movq 0x80(%rax), %r8
orq 0x78(%rax), %r8
movl $0xff, %edx
movabsq $-0x100000000000000, %rdi # imm = 0xFF00000000000000
movl 0x3f0(%rax,%r9), %esi
movq %rsi, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
testl %r10d, %r10d
cmoveq %rdx, %rdi
movq %rdi, 0x60(%rsp)
je 0xbd62
movq 0x288(%rax), %rsi
movq %rsi, %rdx
shlq $0x9, %rdx
movabsq $-0x101010101010200, %rdi # imm = 0xFEFEFEFEFEFEFE00
andq %rdx, %rdi
shlq $0x7, %rsi
movabsq $0x7f7f7f7f7f7f7f00, %rdx # imm = 0x7F7F7F7F7F7F7F00
jmp 0xbd8b
movq 0x2b8(%rax), %rsi
movq %rsi, %rdx
shrq $0x7, %rdx
movabsq $0xfefefefefefefe, %rdi # imm = 0xFEFEFEFEFEFEFE
andq %rdx, %rdi
shrq $0x9, %rsi
movabsq $0x7f7f7f7f7f7f7f, %rdx # imm = 0x7F7F7F7F7F7F7F
andq %rsi, %rdx
orq %rdi, %rdx
xorl %esi, %esi
cmpl $0x0, 0x18(%rsp)
sete %sil
orq %rdx, %r8
notq %r8
movq %r8, 0x38(%rsp)
movq 0x68(%rax,%r15,8), %rax
movq %rax, 0x40(%rsp)
movq %r9, 0x50(%rsp)
leaq (%rcx,%r9), %r13
addq $0x28, %r13
leaq 0x40(%rbx), %rax
movq %rax, 0x48(%rsp)
imulq $0x30, %r15, %rax
movq %rax, 0x8(%rsp)
addq %rbx, %rax
addq $0x48, %rax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
pushq $-0x28
popq %rbp
movq %rsi, 0x28(%rsp)
imulq $0xf0, %rsi, %rax
movq %rax, 0x30(%rsp)
testq %rbp, %rbp
je 0xbf22
movl 0x28(%r13,%rbp), %r14d
cmpq $0x40, %r14
je 0xbf22
addl $0x12c, 0x2c(%rbx,%r15,4) # imm = 0x12C
movq %rbx, %rdi
movl %r14d, %esi
movl 0x18(%rsp), %edx
callq 0xcb88
addl %eax, 0x2c(%rbx,%r15,4)
movq 0x40(%rsp), %rax
leaq 0x11ac4c(%rip), %rcx    # 0x126a80
testq %rax, (%rcx,%r14,8)
je 0xbe45
addl $-0x1e, 0x14(%rbx,%r15,4)
jmp 0xbf19
leaq 0xac94(%rip), %rax      # 0x16ae0
movq (%rax,%r14,8), %rax
movq 0x8(%rbx), %rcx
movq 0x80(%rcx), %rdx
orq 0x78(%rcx), %rdx
andq %rax, %rdx
movq 0x20(%rsp), %rsi
orq (%rsi), %rdx
movq %rdx, (%rsi)
orq %rdx, 0xa0(%rbx,%r15,8)
movq %rax, %rdx
movq 0x38(%rsp), %rdi
andq %rdi, %rdx
movq %rdx, %rsi
shrq %rsi
movabsq $0x5555555555555555, %r8 # imm = 0x5555555555555555
andq %r8, %rsi
subq %rsi, %rdx
movq %rdx, %rsi
movabsq $0x3333333333333333, %r8 # imm = 0x3333333333333333
andq %r8, %rsi
shrq $0x2, %rdx
andq %r8, %rdx
addq %rsi, %rdx
movq %rdx, %rsi
shrq $0x4, %rsi
addq %rdx, %rsi
movabsq $0xf0f0f0f0f0f0f0f, %rdx # imm = 0xF0F0F0F0F0F0F0F
andq %rdx, %rsi
movabsq $0x101010101010101, %rdx # imm = 0x101010101010101
imulq %rdx, %rsi
shrq $0x38, %rsi
leaq 0xb140(%rip), %rdx      # 0x17020
movl (%rdx,%rsi,4), %edx
addl %edx, 0x14(%rbx,%r15,4)
movq 0x30(%rsp), %rdx
movl 0x3f0(%rcx,%rdx), %ecx
movq 0x28(%rsp), %rdx
shll $0x9, %edx
addq 0x170ad(%rip), %rdx     # 0x22fb0
movq (%rdx,%rcx,8), %rcx
andq %rdi, %rcx
testq %rax, %rcx
leal 0x2(%r12), %eax
cmovel %r12d, %eax
movl %eax, %r12d
addq $0x4, %rbp
jmp 0xbdf3
movabsq $0x55aa55aa55aa55aa, %r13 # imm = 0x55AA55AA55AA55AA
movq 0x8(%rbx), %rdi
movq 0x50(%rsp), %rax
addq %rdi, %rax
addq $0x378, %rax            # imm = 0x378
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %rax
movq 0x8(%rsp), %rcx
leaq 0x10(%rax,%rcx), %rax
movq %rax, 0x58(%rsp)
pushq $-0x28
popq %rbp
testq %rbp, %rbp
je 0xc0f8
movq 0x20(%rsp), %rax
movl 0x28(%rax,%rbp), %r14d
cmpq $0x40, %r14
je 0xc0f8
addl $0x12c, 0x2c(%rbx,%r15,4) # imm = 0x12C
movq %rbx, %rdi
movl %r14d, %esi
movl 0x18(%rsp), %edx
callq 0xcc44
addl %eax, 0x2c(%rbx,%r15,4)
movq 0x8(%rbx), %rdi
movl %r14d, %esi
xorl %edx, %edx
callq 0xcd04
movq 0x40(%rsp), %rcx
leaq 0x11aad2(%rip), %rdx    # 0x126a80
testq %rcx, (%rdx,%r14,8)
je 0xbfcb
movq %r14, %rcx
shlq $0x9, %rcx
addq 0x16fa6(%rip), %rcx     # 0x22f68
movq 0x10(%rsp), %rdx
andq (%rcx,%rdx,8), %rax
movq 0x8(%rbx), %rdi
movq 0x80(%rdi), %rcx
orq 0x78(%rdi), %rcx
andq %rax, %rcx
movq 0x58(%rsp), %rdx
movq %rcx, (%rdx)
orq %rcx, 0xa0(%rbx,%r15,8)
movq 0x30(%rsp), %rcx
movl 0x3f0(%rdi,%rcx), %ecx
movq 0x28(%rsp), %rdx
shll $0x9, %edx
addq 0x16fa8(%rip), %rdx     # 0x22fb0
movq (%rdx,%rcx,8), %rcx
movq 0x38(%rsp), %rdx
andq %rdx, %rcx
testq %rax, %rcx
leal 0x2(%r12), %ecx
cmovel %r12d, %ecx
andq %rdx, %rax
movq %rax, %rdx
shrq %rdx
movabsq $0x5555555555555555, %rsi # imm = 0x5555555555555555
andq %rsi, %rdx
subq %rdx, %rax
movq %rax, %rdx
movabsq $0x3333333333333333, %r8 # imm = 0x3333333333333333
andq %r8, %rdx
shrq $0x2, %rax
andq %r8, %rax
addq %rdx, %rax
movq %rax, %rdx
shrq $0x4, %rdx
addq %rax, %rdx
movabsq $0xf0f0f0f0f0f0f0f, %r9 # imm = 0xF0F0F0F0F0F0F0F
andq %r9, %rdx
movabsq $0x101010101010101, %r10 # imm = 0x101010101010101
imulq %r10, %rdx
shrq $0x38, %rdx
leaq 0xafcd(%rip), %rax      # 0x17050
movl (%rax,%rdx,4), %eax
addl %eax, 0x14(%rbx,%r15,4)
leaq 0x11a9ee(%rip), %rax    # 0x126a80
testq %r13, (%rax,%r14,8)
movq %r13, %rax
movabsq $-0x55aa55aa55aa55ab, %rdx # imm = 0xAA55AA55AA55AA55
cmoveq %rdx, %rax
movq 0x8(%rsp), %rdx
andq 0x288(%rdi,%rdx), %rax
movq %rax, %rdx
shrq %rdx
andq %rsi, %rdx
subq %rdx, %rax
movq %rax, %rdx
andq %r8, %rdx
shrq $0x2, %rax
andq %r8, %rax
addq %rdx, %rax
movq %rax, %rdx
shrq $0x4, %rdx
addq %rax, %rdx
andq %r9, %rdx
imulq %r10, %rdx
shrq $0x38, %rdx
addl %edx, %edx
subl %edx, 0x2c(%rbx,%r15,4)
addq $0x4, %rbp
movl %ecx, %r12d
jmp 0xbf5a
xorl %eax, %eax
cmpl $0x0, 0x18(%rsp)
setne %al
movq 0x50(%rsp), %rcx
leaq 0x3a0(%rdi,%rcx), %rcx
movq %rcx, 0x18(%rsp)
movq 0x48(%rsp), %rcx
movq 0x8(%rsp), %rdx
leaq 0x18(%rcx,%rdx), %rcx
movq %rcx, 0x20(%rsp)
leal 0x1(%rax,%rax,2), %eax
movl %eax, 0x58(%rsp)
pushq $0x2
popq %rax
pushq $0x8
popq %rcx
cmovel %eax, %ecx
movl %ecx, 0x6c(%rsp)
movq 0x60(%rsp), %rax
notq %rax
movq %rax, 0x70(%rsp)
pushq $-0x28
popq %rbp
movl %r12d, %r13d
testq %rbp, %rbp
je 0xc370
movq 0x18(%rsp), %rax
movl 0x28(%rax,%rbp), %r14d
cmpq $0x40, %r14
je 0xc370
addl $0x1f4, 0x2c(%rbx,%r15,4) # imm = 0x1F4
movl %r14d, %esi
xorl %edx, %edx
callq 0xcd46
movq 0x40(%rsp), %rcx
leaq 0x11a8f4(%rip), %rdx    # 0x126a80
testq %rcx, (%rdx,%r14,8)
je 0xc1a9
movq %r14, %rcx
shlq $0x9, %rcx
addq 0x16dc8(%rip), %rcx     # 0x22f68
movq 0x10(%rsp), %rdx
andq (%rcx,%rdx,8), %rax
movq 0x8(%rbx), %rdi
movq 0x80(%rdi), %rcx
orq 0x78(%rdi), %rcx
andq %rax, %rcx
movq 0x20(%rsp), %rdx
movq %rcx, (%rdx)
orq %rcx, 0xa0(%rbx,%r15,8)
movq 0x30(%rsp), %rcx
movl 0x3f0(%rdi,%rcx), %ecx
movq 0x28(%rsp), %rdx
shll $0x9, %edx
addq 0x16dca(%rip), %rdx     # 0x22fb0
movq (%rdx,%rcx,8), %rcx
movq 0x38(%rsp), %rdx
andq %rdx, %rcx
leal 0x3(%r13), %r12d
testq %rax, %rcx
cmovel %r13d, %r12d
andq %rdx, %rax
movq %rax, %rcx
shrq %rcx
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rcx
movq %rax, %rdx
subq %rcx, %rdx
movq %rdx, %rcx
movabsq $0x3333333333333333, %rsi # imm = 0x3333333333333333
andq %rsi, %rcx
shrq $0x2, %rdx
andq %rsi, %rdx
addq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
addq %rdx, %rcx
movabsq $0xf0f0f0f0f0f0f0f, %rdx # imm = 0xF0F0F0F0F0F0F0F
andq %rdx, %rcx
movabsq $0x101010101010101, %rdx # imm = 0x101010101010101
imulq %rdx, %rcx
shrq $0x38, %rcx
leaq 0xae2d(%rip), %rdx      # 0x17090
movl (%rdx,%rcx,4), %ecx
addl %ecx, 0x14(%rbx,%r15,4)
movq 0x60(%rsp), %rcx
movq 0x8(%rsp), %rdx
testq %rcx, 0x2b0(%rdi,%rdx)
je 0xc367
leaq 0x11a7f6(%rip), %rcx    # 0x126a80
movq 0x60(%rsp), %rdx
testq %rdx, (%rcx,%r14,8)
je 0xc367
cmpl %r14d, 0x10(%rsp)
jbe 0xc2a9
movl 0x58(%rsp), %ecx
testl %ecx, 0xc(%rdi)
je 0xc2e2
cmpl %r14d, 0x10(%rsp)
jae 0xc367
movl 0x6c(%rsp), %ecx
testl %ecx, 0xc(%rdi)
jne 0xc367
movq 0x10(%rsp), %rcx
leaq 0x11a7b3(%rip), %rdx    # 0x126a80
movabsq $-0xf0f0f0f0f0f0f10, %rsi # imm = 0xF0F0F0F0F0F0F0F0
testq %rsi, (%rdx,%rcx,8)
jne 0xc2fe
jmp 0xc367
movq 0x10(%rsp), %rcx
leaq 0x11a792(%rip), %rdx    # 0x126a80
movabsq $0xf0f0f0f0f0f0f0f, %rsi # imm = 0xF0F0F0F0F0F0F0F
testq %rsi, (%rdx,%rcx,8)
je 0xc2a9
andq 0x70(%rsp), %rax
movq %rax, %rcx
shrq %rcx
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rcx
subq %rcx, %rax
movq %rax, %rcx
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
andq %rdx, %rcx
shrq $0x2, %rax
andq %rdx, %rax
addq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
addq %rax, %rcx
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %rcx
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %rcx
shrq $0x38, %rcx
cmpl $0x3, %ecx
ja 0xc367
addl $-0x19, 0x2c(%rbx,%r15,4)
addq $0x4, %rbp
jmp 0xc14d
movq 0x50(%rsp), %rax
leaq (%rdi,%rax), %r12
addq $0x3c8, %r12            # imm = 0x3C8
movq 0x48(%rsp), %rax
movq 0x8(%rsp), %rcx
leaq 0x20(%rax,%rcx), %rax
movq %rax, 0x8(%rsp)
pushq $-0x28
popq %rbp
testq %rbp, %rbp
je 0xc4b3
movl 0x28(%r12,%rbp), %r14d
cmpq $0x40, %r14
je 0xc4b3
addl $0x3b6, 0x2c(%rbx,%r15,4) # imm = 0x3B6
movl %r14d, %esi
xorl %edx, %edx
callq 0xcd88
movq 0x40(%rsp), %rcx
leaq 0x11a6b2(%rip), %rdx    # 0x126a80
testq %rcx, (%rdx,%r14,8)
je 0xc3e8
shlq $0x9, %r14
addq 0x16b89(%rip), %r14     # 0x22f68
movq 0x10(%rsp), %rcx
andq (%r14,%rcx,8), %rax
movq 0x8(%rbx), %rdi
movq 0x80(%rdi), %rcx
orq 0x78(%rdi), %rcx
andq %rax, %rcx
movq 0x8(%rsp), %rdx
movq %rcx, (%rdx)
orq %rcx, 0xa0(%rbx,%r15,8)
movq 0x30(%rsp), %rcx
movl 0x3f0(%rdi,%rcx), %ecx
movq 0x28(%rsp), %rdx
shll $0x9, %edx
addq 0x16b8b(%rip), %rdx     # 0x22fb0
movq (%rdx,%rcx,8), %rcx
movq 0x38(%rsp), %rdx
andq %rdx, %rcx
testq %rax, %rcx
leal 0x5(%r13), %ecx
cmovel %r13d, %ecx
andq %rdx, %rax
movq %rax, %rdx
shrq %rdx
movabsq $0x5555555555555555, %rsi # imm = 0x5555555555555555
andq %rsi, %rdx
subq %rdx, %rax
movq %rax, %rdx
movabsq $0x3333333333333333, %rsi # imm = 0x3333333333333333
andq %rsi, %rdx
shrq $0x2, %rax
andq %rsi, %rax
addq %rdx, %rax
movq %rax, %rdx
shrq $0x4, %rdx
addq %rax, %rdx
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %rdx
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %rdx
shrq $0x38, %rdx
leaq 0xac31(%rip), %rax      # 0x170d0
movl (%rax,%rdx,4), %eax
addl %eax, 0x14(%rbx,%r15,4)
addq $0x4, %rbp
movl %ecx, %r13d
jmp 0xc397
movslq %r13d, %rax
leaq 0xac83(%rip), %rcx      # 0x17140
movl (%rcx,%rax,4), %eax
movq 0x28(%rsp), %rcx
subl %eax, 0x1c(%rbx,%rcx,4)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq