// File: jswaff[P]prophet3/src/evalks.cpp
// Function: eval_king_safety(position*, bool)
int32 eval_king_safety(position *p,bool wtm) {
    int32 score = 0;

    if (wtm) {
        // what side are we on?
        if (get_file(p->white_king) > FILE_E) {
            // check that pawns on f,g,h are not too far away
            if (p->piece[F2]!=PAWN) {
                if (p->piece[F3]==PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[F4]==PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[G2]!=PAWN) {
                if (p->piece[G3]==PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[G4]==PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[H2]!=PAWN) {
                if (p->piece[H3]==PAWN) {
                    score += king_safety_pawn_one_away/2;
                } else if (p->piece[H4]==PAWN) {
                    score += king_safety_pawn_two_away/2;
                } else {
                    score += king_safety_pawn_far_away/2;
                }
            }
        } else if (get_file(p->white_king) < FILE_D) {
            if (p->piece[C2]!=PAWN) {
                if (p->piece[C3]==PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[C4]==PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[B2]!=PAWN) {
                if (p->piece[B3]==PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[B4]==PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[A2]!=PAWN) {
                if (p->piece[A3]==PAWN) {
                    score += king_safety_pawn_one_away/2;
                } else if (p->piece[A4]==PAWN) {
                    score += king_safety_pawn_two_away/2;
                } else {
                    score += king_safety_pawn_far_away/2;
                }
            }
        } else {
            // check if open file
            if (! ((p->white_pawns | p->black_pawns) & bb_files[get_file(p->white_king)])) {
                score += king_safety_middle_open_file;
            }
        }
    } else {
        if (get_file(p->black_king) > FILE_E) {
            // check that pawns on f,g,h are not too far away
            if (p->piece[F7]!=-PAWN) {
                if (p->piece[F6]==-PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[F5]==-PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[G7]!=-PAWN) {
                if (p->piece[G6]==-PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[G5]==-PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[H7]!=-PAWN) {
                if (p->piece[H6]==-PAWN) {
                    score += king_safety_pawn_one_away/2;
                } else if (p->piece[H5]==-PAWN) {
                    score += king_safety_pawn_two_away/2;
                } else {
                    score += king_safety_pawn_far_away/2;
                }
            }
        } else if (get_file(p->black_king) < FILE_D) {
            if (p->piece[C7]!=-PAWN) {
                if (p->piece[C6]==-PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[C5]==-PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[B7]!=-PAWN) {
                if (p->piece[B6]==-PAWN) {
                    score += king_safety_pawn_one_away;
                } else if (p->piece[B5]==-PAWN) {
                    score += king_safety_pawn_two_away;
                } else {
                    score += king_safety_pawn_far_away;
                }
            }
            if (p->piece[A7]!=-PAWN) {
                if (p->piece[A6]==-PAWN) {
                    score += king_safety_pawn_one_away/2;
                } else if (p->piece[A5]==-PAWN) {
                    score += king_safety_pawn_two_away/2;
                } else {
                    score += king_safety_pawn_far_away/2;
                }
            }
        } else {
            if (! ((p->white_pawns | p->black_pawns) & bb_files[get_file(p->black_king)])) {
                score += king_safety_middle_open_file;
            }
        }

    }

    return score;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movl $0x0, -0x10(%rbp)
testb $0x1, -0x9(%rbp)
je 0x797e
movq -0x8(%rbp), %rax
movl 0x14c(%rax), %edi
callq 0x12070
cmpl $0x4, %eax
jle 0x7830
movq -0x8(%rbp), %rax
cmpl $0x1, 0xd4(%rax)
je 0x7797
movq -0x8(%rbp), %rax
cmpl $0x1, 0xb4(%rax)
jne 0x7772
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7795
movq -0x8(%rbp), %rax
cmpl $0x1, 0x94(%rax)
jne 0x778a
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x7793
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x7795
jmp 0x7797
movq -0x8(%rbp), %rax
cmpl $0x1, 0xd8(%rax)
je 0x77e1
movq -0x8(%rbp), %rax
cmpl $0x1, 0xb8(%rax)
jne 0x77bc
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x77df
movq -0x8(%rbp), %rax
cmpl $0x1, 0x98(%rax)
jne 0x77d4
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x77dd
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x77df
jmp 0x77e1
movq -0x8(%rbp), %rax
cmpl $0x1, 0xdc(%rax)
je 0x782b
movq -0x8(%rbp), %rax
cmpl $0x1, 0xbc(%rax)
jne 0x7806
movl -0x10(%rbp), %eax
addl $-0x5, %eax
movl %eax, -0x10(%rbp)
jmp 0x7829
movq -0x8(%rbp), %rax
cmpl $0x1, 0x9c(%rax)
jne 0x781e
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7827
movl -0x10(%rbp), %eax
addl $-0xf, %eax
movl %eax, -0x10(%rbp)
jmp 0x7829
jmp 0x782b
jmp 0x7979
movq -0x8(%rbp), %rax
movl 0x14c(%rax), %edi
callq 0x12070
cmpl $0x3, %eax
jge 0x7928
movq -0x8(%rbp), %rax
cmpl $0x1, 0xc8(%rax)
je 0x7892
movq -0x8(%rbp), %rax
cmpl $0x1, 0xa8(%rax)
jne 0x786d
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7890
movq -0x8(%rbp), %rax
cmpl $0x1, 0x88(%rax)
jne 0x7885
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x788e
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x7890
jmp 0x7892
movq -0x8(%rbp), %rax
cmpl $0x1, 0xc4(%rax)
je 0x78dc
movq -0x8(%rbp), %rax
cmpl $0x1, 0xa4(%rax)
jne 0x78b7
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x78da
movq -0x8(%rbp), %rax
cmpl $0x1, 0x84(%rax)
jne 0x78cf
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x78d8
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x78da
jmp 0x78dc
movq -0x8(%rbp), %rax
cmpl $0x1, 0xc0(%rax)
je 0x7926
movq -0x8(%rbp), %rax
cmpl $0x1, 0xa0(%rax)
jne 0x7901
movl -0x10(%rbp), %eax
addl $-0x5, %eax
movl %eax, -0x10(%rbp)
jmp 0x7924
movq -0x8(%rbp), %rax
cmpl $0x1, 0x80(%rax)
jne 0x7919
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7922
movl -0x10(%rbp), %eax
addl $-0xf, %eax
movl %eax, -0x10(%rbp)
jmp 0x7924
jmp 0x7926
jmp 0x7977
movq -0x8(%rbp), %rax
movq 0x158(%rax), %rax
movq -0x8(%rbp), %rcx
orq 0x160(%rcx), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0x14c(%rax), %edi
callq 0x12070
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
leaq 0xfdc8e(%rip), %rcx     # 0x1055f0
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
jne 0x7975
movl -0x10(%rbp), %eax
addl $-0x32, %eax
movl %eax, -0x10(%rbp)
jmp 0x7977
jmp 0x7979
jmp 0x7b8e
movq -0x8(%rbp), %rax
movl 0x150(%rax), %edi
callq 0x12070
cmpl $0x4, %eax
jle 0x7a5e
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x34(%rax)
je 0x79d7
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x54(%rax)
jne 0x79b5
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x79d5
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x74(%rax)
jne 0x79ca
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x79d3
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x79d5
jmp 0x79d7
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x38(%rax)
je 0x7a18
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x58(%rax)
jne 0x79f6
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7a16
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x78(%rax)
jne 0x7a0b
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x7a14
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x7a16
jmp 0x7a18
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x3c(%rax)
je 0x7a59
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x5c(%rax)
jne 0x7a37
movl -0x10(%rbp), %eax
addl $-0x5, %eax
movl %eax, -0x10(%rbp)
jmp 0x7a57
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x7c(%rax)
jne 0x7a4c
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7a55
movl -0x10(%rbp), %eax
addl $-0xf, %eax
movl %eax, -0x10(%rbp)
jmp 0x7a57
jmp 0x7a59
jmp 0x7b8c
movq -0x8(%rbp), %rax
movl 0x150(%rax), %edi
callq 0x12070
cmpl $0x3, %eax
jge 0x7b3b
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x28(%rax)
je 0x7ab7
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x48(%rax)
jne 0x7a95
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7ab5
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x68(%rax)
jne 0x7aaa
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x7ab3
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x7ab5
jmp 0x7ab7
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x24(%rax)
je 0x7af8
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x44(%rax)
jne 0x7ad6
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7af6
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x64(%rax)
jne 0x7aeb
movl -0x10(%rbp), %eax
addl $-0x14, %eax
movl %eax, -0x10(%rbp)
jmp 0x7af4
movl -0x10(%rbp), %eax
addl $-0x1e, %eax
movl %eax, -0x10(%rbp)
jmp 0x7af6
jmp 0x7af8
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x20(%rax)
je 0x7b39
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x40(%rax)
jne 0x7b17
movl -0x10(%rbp), %eax
addl $-0x5, %eax
movl %eax, -0x10(%rbp)
jmp 0x7b37
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x60(%rax)
jne 0x7b2c
movl -0x10(%rbp), %eax
addl $-0xa, %eax
movl %eax, -0x10(%rbp)
jmp 0x7b35
movl -0x10(%rbp), %eax
addl $-0xf, %eax
movl %eax, -0x10(%rbp)
jmp 0x7b37
jmp 0x7b39
jmp 0x7b8a
movq -0x8(%rbp), %rax
movq 0x158(%rax), %rax
movq -0x8(%rbp), %rcx
orq 0x160(%rcx), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movl 0x150(%rax), %edi
callq 0x12070
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, %ecx
movl %ecx, %edx
leaq 0xfda7b(%rip), %rcx     # 0x1055f0
andq (%rcx,%rdx,8), %rax
cmpq $0x0, %rax
jne 0x7b88
movl -0x10(%rbp), %eax
addl $-0x32, %eax
movl %eax, -0x10(%rbp)
jmp 0x7b8a
jmp 0x7b8c
jmp 0x7b8e
movl -0x10(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw (%rax,%rax)