// File: bjohnsonfl[P]Overhead-Chess-Engine-/evaluate.cpp
// Function: evaluate(position&)
value evaluate (position& pos)
    {
       
        value eval = pos.get_material(white) - pos.get_material(black);
       
       int totalMaterial = pos.get_material(white) + pos.get_material(black);
        
        gamePhase phase = (totalMaterial >= gamePhaseCutoff) ? middleGame : endGame;
    //PIECE SQUARE TABLE EVALUATIONS
    //////////////////////////////////////////////////////////
        
        bitboard pieces = 0;
        square sq = a1;
        //white pieces
        for(piece pt = w_knight; pt < w_king; pt ++)
        {
            pieces = pos.get_pieces(pt);
            while(pieces)
            {
                sq = lsb_sq(pieces);
                eval =value( eval + pSqTble[pt][sq]);
                pop_lsb(&pieces);
            };
        }
        
        if(phase == middleGame)
        {
            //w_pawn
            pieces = pos.get_pieces(w_pawn);
            while(pieces)
            {
                sq = lsb_sq(pieces);
                eval =value( eval + pSqTble[w_pawn][sq]);
                pop_lsb(&pieces);
            };
            //w_king
                pieces = pos.get_pieces(w_king);
                sq = lsb_sq(pieces);
                eval =value( eval + pSqTble[w_king][sq]);
                pop_lsb(&pieces);
           
        }
        else{
            
            pieces = pos.get_pieces(w_pawn);
           //w_pawn middleGame
            while(pieces)
            {
                sq = lsb_sq(pieces);
                eval =value( eval + pSqTbleEndGame[w_pawn][sq]);
                pop_lsb(&pieces);
            };
            //w_king Endgame
            pieces = pos.get_pieces(w_king);
            sq = lsb_sq(pieces);
            eval =value( eval + pSqTbleEndGame[w_king][sq]);
            pop_lsb(&pieces);
        }
        
        //black pieces
     //////////////////////////////////////////////////////////
        for(piece pt = b_knight; pt < b_king; pt ++)
        {
            pieces = pos.get_pieces(pt);
            while(pieces)
            {
                sq = lsb_sq(pieces);
            
               
                eval =value( eval - pSqTble[piece_To_Piecetype(pt)][oppSq[sq]]);
                pop_lsb(&pieces);
            };
        }
        
        if(phase == middleGame)
        {
            //B_pawn
            pieces = pos.get_pieces(b_pawn);
            while(pieces)
            {
                sq = lsb_sq(pieces);
                eval =value( eval - pSqTble[piece_To_Piecetype(b_pawn)][oppSq[sq]]);
                pop_lsb(&pieces);
            };
            //B_king
            pieces = pos.get_pieces(b_king);
            sq = lsb_sq(pieces);
            eval =value( eval - pSqTble[piece_To_Piecetype(b_king)][oppSq[sq]]);
            pop_lsb(&pieces);
            
        }
        else{
            
            pieces = pos.get_pieces(b_pawn);
            //w_pawn EndGame
            while(pieces)
            {
                sq = lsb_sq(pieces);
                eval =value( eval - pSqTbleEndGame[piece_To_Piecetype(b_pawn)][oppSq[sq]]);
                pop_lsb(&pieces);
            };
            //b_king Endgame
            pieces = pos.get_pieces(b_king);
            sq = lsb_sq(pieces);
            eval =value( eval - pSqTbleEndGame[piece_To_Piecetype(b_king)][oppSq[sq]]);
            pop_lsb(&pieces);
        }
 ///////////////////////////////////////////////////////////
        
        if (pos.get_sideToPlay() == white)
            return eval;
        else return -eval;
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xb310
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xb310
movl -0x3c(%rbp), %edi
movl %eax, %esi
callq 0xa0b0
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xb310
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xb310
movl -0x38(%rbp), %edi
movl %eax, %esi
callq 0xa0f0
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %edx
movl 0xecc99(%rip), %esi     # 0xf7b90
movl $0x1, %eax
xorl %ecx, %ecx
cmpl %esi, %edx
cmovgel %ecx, %eax
movl %eax, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x1, -0x30(%rbp)
cmpl $0x5, -0x30(%rbp)
jge 0xaf7e
movq -0x10(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0xa140
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xaf6f
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl -0x30(%rbp), %ecx
movl %ecx, %edx
leaq 0x72ec(%rip), %rcx      # 0x12240
shlq $0x8, %rdx
addq %rdx, %rcx
movl -0x2c(%rbp), %edx
addl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xaf32
jmp 0xaf71
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x9f80
jmp 0xaf1c
cmpl $0x0, -0x1c(%rbp)
jne 0xb00f
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xafca
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0x7287(%rip), %rcx      # 0x12240
addl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xaf97
movq -0x10(%rbp), %rdi
movl $0x5, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0x7249(%rip), %rcx      # 0x12240
addl 0x500(%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xb091
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xb051
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0x7800(%rip), %rcx      # 0x12840
addl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xb01e
movq -0x10(%rbp), %rdi
movl $0x5, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
leaq 0x77c2(%rip), %rcx      # 0x12840
addl 0x500(%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
movl $0x7, -0x34(%rbp)
cmpl $0xb, -0x34(%rbp)
jge 0xb115
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0xa140
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xb106
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x34(%rbp), %edi
callq 0xa0d0
movl %eax, %ecx
movl -0x40(%rbp), %eax
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x7161(%rip), %rcx      # 0x12240
shlq $0x8, %rdx
addq %rdx, %rcx
movl -0x2c(%rbp), %edx
movl %edx, %esi
leaq 0xec99e(%rip), %rdx     # 0xf7a90
movl (%rdx,%rsi,4), %edx
subl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xb0ae
jmp 0xb108
leaq -0x34(%rbp), %rdi
xorl %esi, %esi
callq 0x9f80
jmp 0xb098
cmpl $0x0, -0x1c(%rbp)
jne 0xb1f3
movq -0x10(%rbp), %rdi
movl $0x6, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xb18b
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x44(%rbp)
movl $0x6, %edi
callq 0xa0d0
movl %eax, %ecx
movl -0x44(%rbp), %eax
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x70dc(%rip), %rcx      # 0x12240
shlq $0x8, %rdx
addq %rdx, %rcx
movl -0x2c(%rbp), %edx
movl %edx, %esi
leaq 0xec919(%rip), %rdx     # 0xf7a90
movl (%rdx,%rsi,4), %edx
subl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xb131
movq -0x10(%rbp), %rdi
movl $0xb, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x48(%rbp)
movl $0xb, %edi
callq 0xa0d0
movl %eax, %ecx
movl -0x48(%rbp), %eax
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x7077(%rip), %rcx      # 0x12240
shlq $0x8, %rdx
addq %rdx, %rcx
movl -0x2c(%rbp), %edx
movl %edx, %esi
leaq 0xec8b4(%rip), %rdx     # 0xf7a90
movl (%rdx,%rsi,4), %edx
subl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xb2c2
movq -0x10(%rbp), %rdi
movl $0x6, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0xb25f
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x6, %edi
callq 0xa0d0
movl %eax, %ecx
movl -0x4c(%rbp), %eax
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x7608(%rip), %rcx      # 0x12840
shlq $0x8, %rdx
addq %rdx, %rcx
movl -0x2c(%rbp), %edx
movl %edx, %esi
leaq 0xec845(%rip), %rdx     # 0xf7a90
movl (%rdx,%rsi,4), %edx
subl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
jmp 0xb205
movq -0x10(%rbp), %rdi
movl $0xb, %esi
callq 0xa140
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x7780
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x50(%rbp)
movl $0xb, %edi
callq 0xa0d0
movl %eax, %ecx
movl -0x50(%rbp), %eax
movl %ecx, %ecx
movl %ecx, %edx
leaq 0x75a3(%rip), %rcx      # 0x12840
shlq $0x8, %rdx
addq %rdx, %rcx
movl -0x2c(%rbp), %edx
movl %edx, %esi
leaq 0xec7e0(%rip), %rdx     # 0xf7a90
movl (%rdx,%rsi,4), %edx
subl (%rcx,%rdx,4), %eax
movl %eax, -0x14(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x77a0
movq -0x10(%rbp), %rdi
callq 0x75c0
cmpl $0x0, %eax
jne 0xb2d8
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xb2e3
movl -0x14(%rbp), %edi
callq 0xad60
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)