// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %r9
movl %esi, %r15d
movq 0x80(%r9), %r12
orq 0x78(%r9), %r12
imulq $0xf0, %r15, %rdi
leaq 0x68(%r9), %rax
movl $0xff, %ecx
movabsq $-0x100000000000000, %r8 # imm = 0xFF00000000000000
movl 0x3f0(%r9,%rdi), %edx
movq %rdx, 0x8(%rsp)
movl %esi, 0x10(%rsp)
testl %esi, %esi
cmoveq %rcx, %r8
movq %r8, 0x60(%rsp)
movq %rdi, 0x48(%rsp)
je 0xe56f
movq 0x288(%r9), %rdx
addq $0x328, %r9             # imm = 0x328
movq %rdx, %rcx
shlq $0x9, %rcx
movabsq $-0x101010101010200, %rsi # imm = 0xFEFEFEFEFEFEFE00
andq %rcx, %rsi
shlq $0x7, %rdx
movabsq $0x7f7f7f7f7f7f7f00, %rcx # imm = 0x7F7F7F7F7F7F7F00
andq %rdx, %rcx
orq %rsi, %rcx
leaq (%rax,%r15,8), %rax
addq %rdi, %r9
addq $0x28, %r9
jmp 0xe5a5
movq 0x2b8(%r9), %rdx
movq %rdx, %rcx
shrq $0x7, %rcx
movabsq $0xfefefefefefefe, %rsi # imm = 0xFEFEFEFEFEFEFE
andq %rcx, %rsi
shrq $0x9, %rdx
movabsq $0x7f7f7f7f7f7f7f, %rcx # imm = 0x7F7F7F7F7F7F7F
andq %rdx, %rcx
orq %rsi, %rcx
addq $0x350, %r9             # imm = 0x350
xorl %edx, %edx
cmpl $0x0, 0x10(%rsp)
sete %dl
movq (%rax), %rax
movq %rax, 0x38(%rsp)
orq %rcx, %r12
notq %r12
leaq 0x40(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq (%r15,%r15,2), %rax
shlq $0x4, %rax
movq %rax, 0x58(%rsp)
addq %rbx, %rax
addq $0x48, %rax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movq %rdx, 0x30(%rsp)
imulq $0xf0, %rdx, %rax
movq %rax, 0x20(%rsp)
xorl %ebp, %ebp
movq %r9, 0x28(%rsp)
movl (%r9,%rbp), %r14d
cmpq $0x40, %r14
je 0xe723
addl $0x12c, 0x2c(%rbx,%r15,4) # imm = 0x12C
movq %rbx, %rdi
movl %r14d, %esi
movl 0x10(%rsp), %edx
callq 0xf5ec
addl %eax, 0x2c(%rbx,%r15,4)
movq 0x38(%rsp), %rax
leaq 0xea43f(%rip), %rcx     # 0xf8a70
testq %rax, (%rcx,%r14,8)
je 0xe642
addl $-0x1e, 0x14(%rbx,%r15,4)
jmp 0xe710
leaq 0xd3f7(%rip), %rax      # 0x1ba40
movq (%rax,%r14,8), %rax
movq 0x8(%rbx), %rcx
movq 0x80(%rcx), %rdx
orq 0x78(%rcx), %rdx
andq %rax, %rdx
movq 0x18(%rsp), %rsi
orq (%rsi), %rdx
movq %rdx, (%rsi)
orq %rdx, 0xa0(%rbx,%r15,8)
movq %rax, %rdx
andq %r12, %rdx
movq %rdx, %rsi
shrq %rsi
movabsq $0x5555555555555555, %rdi # imm = 0x5555555555555555
andq %rdi, %rsi
subq %rsi, %rdx
movq %rdx, %rsi
movabsq $0x3333333333333333, %rdi # imm = 0x3333333333333333
andq %rdi, %rsi
shrq $0x2, %rdx
andq %rdi, %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 0xd788(%rip), %rdx      # 0x1be60
movl (%rdx,%rsi,4), %edx
addl %edx, 0x14(%rbx,%r15,4)
movq 0x20(%rsp), %rdx
movl 0x3f0(%rcx,%rdx), %ecx
movq 0x30(%rsp), %rdx
shll $0x9, %edx
addq 0x168bd(%rip), %rdx     # 0x24fb8
movq (%rdx,%rcx,8), %rcx
andq %r12, %rcx
testq %rax, %rcx
leal 0x2(%r13), %eax
cmovel %r13d, %eax
movl %eax, %r13d
addq $0x4, %rbp
cmpq $0x28, %rbp
movq 0x28(%rsp), %r9
jne 0xe5fa
movq 0x8(%rbx), %rdx
movq 0x48(%rsp), %rax
movl 0x378(%rdx,%rax), %r14d
cmpl $0x40, %r14d
je 0xe931
movq 0x40(%rsp), %rcx
movq 0x58(%rsp), %rsi
addq %rsi, %rcx
addq $0x10, %rcx
movq %rcx, 0x28(%rsp)
addq %rdx, %rax
addq $0x37c, %rax            # imm = 0x37C
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
movq 0x30(%rsp), %rax
shll $0x9, %eax
addq 0x16845(%rip), %rax     # 0x24fb8
movq %rax, 0x18(%rsp)
addl $0x12c, 0x2c(%rbx,%r15,4) # imm = 0x12C
movq %rbx, %rdi
movl %r14d, %esi
movl 0x10(%rsp), %edx
callq 0xf6ba
movq 0x8(%rbx), %rdx
movq 0x80(%rdx), %rdi
orq 0x78(%rdx), %rdi
movl %r14d, %esi
leaq 0xd097(%rip), %rcx      # 0x1b840
movq (%rcx,%rsi,8), %r8
andq %rdi, %r8
leaq 0xce89(%rip), %rcx      # 0x1b640
imulq (%rcx,%rsi,8), %r8
leaq 0xcd7d(%rip), %rcx      # 0x1b540
movb (%rcx,%rsi,4), %cl
shrq %cl, %r8
leaq 0x16a40(%rip), %rcx     # 0x25210
movq (%rcx,%rsi,8), %rcx
movq (%rcx,%r8,8), %rcx
movq 0x38(%rsp), %r8
leaq 0xea28c(%rip), %r9      # 0xf8a70
testq %r8, (%r9,%rsi,8)
je 0xe801
movq %rsi, %r8
shlq $0x9, %r8
addq 0x16770(%rip), %r8      # 0x24f68
movq 0x8(%rsp), %r9
andq (%r8,%r9,8), %rcx
addl 0x2c(%rbx,%r15,4), %eax
andq %rcx, %rdi
movq 0x28(%rsp), %r8
movq %rdi, (%r8)
orq %rdi, 0xa0(%rbx,%r15,8)
movq 0x20(%rsp), %rdi
movl 0x3f0(%rdx,%rdi), %edi
movq 0x18(%rsp), %r8
movq (%r8,%rdi,8), %rdi
andq %r12, %rdi
testq %rcx, %rdi
leal 0x2(%r13), %edi
cmovnel %edi, %r13d
andq %r12, %rcx
movq %rcx, %rdi
shrq %rdi
movabsq $0x5555555555555555, %r8 # imm = 0x5555555555555555
andq %r8, %rdi
subq %rdi, %rcx
movq %rcx, %rdi
movabsq $0x3333333333333333, %r9 # imm = 0x3333333333333333
andq %r9, %rdi
shrq $0x2, %rcx
andq %r9, %rcx
addq %rdi, %rcx
movq %rcx, %rdi
shrq $0x4, %rdi
addq %rcx, %rdi
movabsq $0xf0f0f0f0f0f0f0f, %r10 # imm = 0xF0F0F0F0F0F0F0F
andq %r10, %rdi
movabsq $0x101010101010101, %r11 # imm = 0x101010101010101
imulq %r11, %rdi
shrq $0x38, %rdi
leaq 0xd5f1(%rip), %rcx      # 0x1be90
movl (%rcx,%rdi,4), %ecx
addl %ecx, 0x14(%rbx,%r15,4)
leaq 0xea1c2(%rip), %rcx     # 0xf8a70
movabsq $0x55aa55aa55aa55aa, %rdi # imm = 0x55AA55AA55AA55AA
testq %rdi, (%rcx,%rsi,8)
movq %rdi, %rcx
movabsq $-0x55aa55aa55aa55ab, %rsi # imm = 0xAA55AA55AA55AA55
cmoveq %rsi, %rcx
movq 0x58(%rsp), %rsi
andq 0x288(%rdx,%rsi), %rcx
movq %rcx, %rsi
shrq %rsi
andq %r8, %rsi
subq %rsi, %rcx
movq %rcx, %rsi
andq %r9, %rsi
shrq $0x2, %rcx
andq %r9, %rcx
addq %rsi, %rcx
movq %rcx, %rsi
shrq $0x4, %rsi
addq %rcx, %rsi
andq %r10, %rsi
imulq %r11, %rsi
shrq $0x38, %rsi
addl %esi, %esi
subl %esi, %eax
movl %eax, 0x2c(%rbx,%r15,4)
cmpq $0x24, %rbp
je 0xe931
movq 0x50(%rsp), %rax
movl (%rax,%rbp), %r14d
addq $0x4, %rbp
cmpl $0x40, %r14d
jne 0xe778
leaq 0x328(%rdx), %rax
movq %rax, 0x50(%rsp)
movq %r15, %rax
shlq $0x4, %rax
xorl %ecx, %ecx
cmpl $0x0, 0x10(%rsp)
setne %cl
movq 0x48(%rsp), %rsi
leaq 0x3a0(%rdx,%rsi), %r14
leaq (%rax,%rax,2), %rsi
movq 0x40(%rsp), %rax
leaq 0x18(%rax,%rsi), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
leaq 0x3f0(%rdx,%rax), %rax
leaq 0x2b0(%rdx,%rsi), %r10
leal 0x1(%rcx,%rcx,2), %ecx
movl %ecx, 0x28(%rsp)
movl $0x2, %ecx
movl $0x8, %esi
cmovel %ecx, %esi
movl %esi, 0x20(%rsp)
movq 0x60(%rsp), %rcx
notq %rcx
movq %rcx, 0x18(%rsp)
movq $-0x28, %rbp
movl %r13d, %r8d
movl 0x28(%r14,%rbp), %edi
cmpq $0x40, %rdi
je 0xebe9
movl 0x2c(%rbx,%r15,4), %esi
leal 0x1f4(%rsi), %ecx
movl %ecx, 0x2c(%rbx,%r15,4)
movq 0x80(%rdx), %r9
orq 0x78(%rdx), %r9
leaq 0xc95c(%rip), %rcx      # 0x1b340
movq (%rcx,%rdi,8), %r13
andq %r9, %r13
leaq 0xc74e(%rip), %rcx      # 0x1b140
imulq (%rcx,%rdi,8), %r13
leaq 0xc642(%rip), %rcx      # 0x1b040
movb (%rcx,%rdi,4), %cl
shrq %cl, %r13
leaq 0x16a05(%rip), %rcx     # 0x25410
movq (%rcx,%rdi,8), %rcx
movq (%rcx,%r13,8), %rcx
movq 0x38(%rsp), %r11
leaq 0xea051(%rip), %r13     # 0xf8a70
testq %r11, (%r13,%rdi,8)
je 0xea3e
movq %rdi, %r13
shlq $0x9, %r13
addq 0x16534(%rip), %r13     # 0x24f68
movq 0x8(%rsp), %r11
andq (%r13,%r11,8), %rcx
andq %rcx, %r9
movq 0x10(%rsp), %r11
movq %r9, (%r11)
orq %r9, 0xa0(%rbx,%r15,8)
movl (%rax), %r9d
movq 0x30(%rsp), %r13
shll $0x9, %r13d
addq 0x16554(%rip), %r13     # 0x24fb8
movq (%r13,%r9,8), %r9
andq %r12, %r9
leal 0x3(%r8), %r13d
testq %rcx, %r9
cmovel %r8d, %r13d
andq %r12, %rcx
movq %rcx, %r8
shrq %r8
movabsq $0x5555555555555555, %r9 # imm = 0x5555555555555555
andq %r9, %r8
movq %rcx, %r9
subq %r8, %r9
movq %r9, %r8
movabsq $0x3333333333333333, %r11 # imm = 0x3333333333333333
andq %r11, %r8
shrq $0x2, %r9
andq %r11, %r9
addq %r8, %r9
movq %r9, %r8
shrq $0x4, %r8
addq %r9, %r8
movabsq $0xf0f0f0f0f0f0f0f, %r9 # imm = 0xF0F0F0F0F0F0F0F
andq %r9, %r8
movabsq $0x101010101010101, %r9 # imm = 0x101010101010101
imulq %r9, %r8
shrq $0x38, %r8
leaq 0xd3f3(%rip), %r9       # 0x1bed0
movl (%r9,%r8,4), %r8d
addl %r8d, 0x14(%rbx,%r15,4)
movq 0x60(%rsp), %r8
testq %r8, (%r10)
je 0xebdd
leaq 0xe9f75(%rip), %r8      # 0xf8a70
movq 0x60(%rsp), %r9
testq %r9, (%r8,%rdi,8)
je 0xebdd
cmpl %edi, 0x8(%rsp)
jbe 0xeb1b
movl 0x28(%rsp), %r8d
testl %r8d, 0xc(%rdx)
je 0xeb53
cmpl %edi, 0x8(%rsp)
jae 0xebdd
movl 0x20(%rsp), %edi
testl %edi, 0xc(%rdx)
jne 0xebdd
movq 0x8(%rsp), %rdi
leaq 0xe9f32(%rip), %r8      # 0xf8a70
movabsq $-0xf0f0f0f0f0f0f10, %r9 # imm = 0xF0F0F0F0F0F0F0F0
testq %r9, (%r8,%rdi,8)
jne 0xeb6f
jmp 0xebdd
movq 0x8(%rsp), %r8
leaq 0xe9f11(%rip), %r9      # 0xf8a70
movabsq $0xf0f0f0f0f0f0f0f, %r11 # imm = 0xF0F0F0F0F0F0F0F
testq %r11, (%r9,%r8,8)
je 0xeb1b
andq 0x18(%rsp), %rcx
movq %rcx, %rdi
shrq %rdi
movabsq $0x5555555555555555, %r8 # imm = 0x5555555555555555
andq %r8, %rdi
subq %rdi, %rcx
movq %rcx, %rdi
movabsq $0x3333333333333333, %r8 # imm = 0x3333333333333333
andq %r8, %rdi
shrq $0x2, %rcx
andq %r8, %rcx
addq %rdi, %rcx
movq %rcx, %rdi
shrq $0x4, %rdi
addq %rcx, %rdi
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rdi
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rdi
shrq $0x38, %rdi
cmpl $0x3, %edi
ja 0xebdd
addl $0x1db, %esi            # imm = 0x1DB
movl %esi, 0x2c(%rbx,%r15,4)
addq $0x4, %rbp
jne 0xe9b0
jmp 0xebec
movl %r8d, %r13d
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %rsi
addq %rcx, %rsi
addq $0xa0, %rsi
movq 0x40(%rsp), %rcx
movq 0x58(%rsp), %rdi
leaq (%rcx,%rdi), %r10
addq $0x20, %r10
movq $-0x28, %r11
leaq 0xd2f0(%rip), %r9       # 0x1bf10
movl 0x28(%rsi,%r11), %ebp
cmpq $0x40, %rbp
je 0xed7c
addl $0x3b6, 0x2c(%rbx,%r15,4) # imm = 0x3B6
movq 0x80(%rdx), %r14
orq 0x78(%rdx), %r14
leaq 0xcbf6(%rip), %rcx      # 0x1b840
movq (%rcx,%rbp,8), %rdi
andq %r14, %rdi
leaq 0xc9e8(%rip), %rcx      # 0x1b640
imulq (%rcx,%rbp,8), %rdi
leaq 0xc8dc(%rip), %rcx      # 0x1b540
movb (%rcx,%rbp,4), %cl
shrq %cl, %rdi
leaq 0xc6cf(%rip), %rcx      # 0x1b340
movq (%rcx,%rbp,8), %r8
andq %r14, %r8
leaq 0xc4c1(%rip), %rcx      # 0x1b140
imulq (%rcx,%rbp,8), %r8
leaq 0xc3b5(%rip), %rcx      # 0x1b040
movb (%rcx,%rbp,4), %cl
shrq %cl, %r8
leaq 0x16778(%rip), %rcx     # 0x25410
movq (%rcx,%rbp,8), %rcx
movq (%rcx,%r8,8), %rcx
leaq 0x16569(%rip), %r8      # 0x25210
movq (%r8,%rbp,8), %r8
orq (%r8,%rdi,8), %rcx
movq 0x38(%rsp), %rdi
leaq 0xe9db5(%rip), %r8      # 0xf8a70
testq %rdi, (%r8,%rbp,8)
je 0xecd6
shlq $0x9, %rbp
addq 0x1629c(%rip), %rbp     # 0x24f68
movq 0x8(%rsp), %rdi
andq (%rbp,%rdi,8), %rcx
andq %rcx, %r14
movq %r14, (%r10)
orq %r14, 0xa0(%rbx,%r15,8)
movl (%rax), %edi
movq 0x30(%rsp), %r8
shll $0x9, %r8d
addq 0x162c2(%rip), %r8      # 0x24fb8
movq (%r8,%rdi,8), %rdi
andq %r12, %rdi
testq %rcx, %rdi
leal 0x5(%r13), %r14d
cmovel %r13d, %r14d
andq %r12, %rcx
movq %rcx, %rdi
shrq %rdi
movabsq $0x5555555555555555, %r8 # imm = 0x5555555555555555
andq %r8, %rdi
subq %rdi, %rcx
movq %rcx, %rdi
movabsq $0x3333333333333333, %r8 # imm = 0x3333333333333333
andq %r8, %rdi
shrq $0x2, %rcx
andq %r8, %rcx
addq %rdi, %rcx
movq %rcx, %rdi
shrq $0x4, %rdi
addq %rcx, %rdi
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rdi
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rdi
shrq $0x38, %rdi
movl (%r9,%rdi,4), %ecx
addl %ecx, 0x14(%rbx,%r15,4)
movl %r14d, %r13d
addq $0x4, %r11
jne 0xec20
jmp 0xed7f
movl %r13d, %r14d
movslq %r14d, %rax
leaq 0xd1f7(%rip), %rcx      # 0x1bf80
movl (%rcx,%rax,4), %eax
movq 0x30(%rsp), %rcx
subl %eax, 0x1c(%rbx,%rcx,4)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq