// File: /workspace/github_cpp_makefile_star2_O1/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 %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
testl %esi, %esi
je 0x595a
movl 0x14c(%rbx), %edi
callq 0xca4a
cmpl $0x5, %eax
jl 0x5998
xorl %eax, %eax
cmpl $0x1, 0xd4(%rbx)
je 0x5938
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $0x1, 0xb4(%rbx)
je 0x5938
cmpl $0x1, 0x94(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
cmpl $0x1, 0xd8(%rbx)
je 0x5aed
cmpl $0x1, 0xb8(%rbx)
jne 0x5a84
addl $-0xa, %eax
jmp 0x5aed
movl 0x150(%rbx), %edi
callq 0xca4a
cmpl $0x5, %eax
jl 0x59fb
cmpl $-0x1, 0x34(%rbx)
je 0x5a80
cmpl $-0x1, 0x54(%rbx)
je 0x5a92
cmpl $-0x1, 0x74(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
jmp 0x5a97
movl 0x14c(%rbx), %edi
callq 0xca4a
cmpl $0x2, %eax
jg 0x5a35
xorl %eax, %eax
cmpl $0x1, 0xc8(%rbx)
je 0x59d9
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $0x1, 0xa8(%rbx)
je 0x59d9
cmpl $0x1, 0x88(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
cmpl $0x1, 0xc4(%rbx)
je 0x5b48
cmpl $0x1, 0xa4(%rbx)
jne 0x5adc
addl $-0xa, %eax
jmp 0x5b48
movl 0x150(%rbx), %edi
callq 0xca4a
cmpl $0x2, %eax
jg 0x5a4b
cmpl $-0x1, 0x28(%rbx)
je 0x5ab3
cmpl $-0x1, 0x48(%rbx)
je 0x5b0d
cmpl $-0x1, 0x68(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
jmp 0x5b12
movq 0x160(%rbx), %r14
orq 0x158(%rbx), %r14
movl 0x14c(%rbx), %edi
jmp 0x5a5f
movq 0x160(%rbx), %r14
orq 0x158(%rbx), %r14
movl 0x150(%rbx), %edi
callq 0xca4a
movl %eax, %eax
leaq 0xf49c3(%rip), %rcx     # 0xfa430
xorl %edx, %edx
testq %r14, (%rcx,%rax,8)
movl $0xffffffce, %eax       # imm = 0xFFFFFFCE
cmovnel %edx, %eax
jmp 0x5b5d
xorl %eax, %eax
jmp 0x5a97
cmpl $0x1, 0x98(%rbx)
jne 0x5aea
addl $-0x14, %eax
jmp 0x5aed
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $-0x1, 0x38(%rbx)
je 0x5aba
cmpl $-0x1, 0x58(%rbx)
je 0x5aae
cmpl $-0x1, 0x78(%rbx)
je 0x5ab7
addl $-0x1e, %eax
jmp 0x5aba
addl $-0xa, %eax
jmp 0x5aba
xorl %eax, %eax
jmp 0x5b12
addl $-0x14, %eax
cmpl $-0x1, 0x3c(%rbx)
je 0x5b5d
cmpl $-0x1, 0x5c(%rbx)
je 0x5b5a
cmpl $-0x1, 0x7c(%rbx)
je 0x5b08
addl $-0xf, %eax
jmp 0x5b5d
cmpl $0x1, 0x84(%rbx)
jne 0x5b45
addl $-0x14, %eax
jmp 0x5b48
addl $-0x1e, %eax
cmpl $0x1, 0xdc(%rbx)
je 0x5b5d
cmpl $0x1, 0xbc(%rbx)
je 0x5b5a
cmpl $0x1, 0x9c(%rbx)
jne 0x5ad4
addl $-0xa, %eax
jmp 0x5b5d
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $-0x1, 0x24(%rbx)
je 0x5b31
cmpl $-0x1, 0x44(%rbx)
je 0x5b29
cmpl $-0x1, 0x64(%rbx)
je 0x5b2e
addl $-0x1e, %eax
jmp 0x5b31
addl $-0xa, %eax
jmp 0x5b31
addl $-0x14, %eax
cmpl $-0x1, 0x20(%rbx)
je 0x5b5d
cmpl $-0x1, 0x40(%rbx)
je 0x5b5a
cmpl $-0x1, 0x60(%rbx)
jne 0x5ad4
jmp 0x5b08
addl $-0x1e, %eax
cmpl $0x1, 0xc0(%rbx)
je 0x5b5d
cmpl $0x1, 0xa0(%rbx)
jne 0x5b65
addl $-0x5, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
cmpl $0x1, 0x80(%rbx)
jne 0x5ad4
jmp 0x5b08