// File: /workspace/github_cpp_makefile_star2_O3/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 0x5a0a
movl 0x14c(%rbx), %edi
callq 0xca3e
cmpl $0x5, %eax
jl 0x5a48
xorl %eax, %eax
cmpl $0x1, 0xd4(%rbx)
je 0x59e8
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $0x1, 0xb4(%rbx)
je 0x59e8
cmpl $0x1, 0x94(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
cmpl $0x1, 0xd8(%rbx)
je 0x5b9d
cmpl $0x1, 0xb8(%rbx)
jne 0x5b34
addl $-0xa, %eax
jmp 0x5b9d
movl 0x150(%rbx), %edi
callq 0xca3e
cmpl $0x5, %eax
jl 0x5aab
cmpl $-0x1, 0x34(%rbx)
je 0x5b30
cmpl $-0x1, 0x54(%rbx)
je 0x5b42
cmpl $-0x1, 0x74(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
jmp 0x5b47
movl 0x14c(%rbx), %edi
callq 0xca3e
cmpl $0x2, %eax
jg 0x5ae5
xorl %eax, %eax
cmpl $0x1, 0xc8(%rbx)
je 0x5a89
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $0x1, 0xa8(%rbx)
je 0x5a89
cmpl $0x1, 0x88(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
cmpl $0x1, 0xc4(%rbx)
je 0x5bf8
cmpl $0x1, 0xa4(%rbx)
jne 0x5b8c
addl $-0xa, %eax
jmp 0x5bf8
movl 0x150(%rbx), %edi
callq 0xca3e
cmpl $0x2, %eax
jg 0x5afb
cmpl $-0x1, 0x28(%rbx)
je 0x5b63
cmpl $-0x1, 0x48(%rbx)
je 0x5bbd
cmpl $-0x1, 0x68(%rbx)
movl $0xffffffec, %ecx       # imm = 0xFFFFFFEC
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmovel %ecx, %eax
jmp 0x5bc2
movq 0x160(%rbx), %r14
orq 0x158(%rbx), %r14
movl 0x14c(%rbx), %edi
jmp 0x5b0f
movq 0x160(%rbx), %r14
orq 0x158(%rbx), %r14
movl 0x150(%rbx), %edi
callq 0xca3e
movl %eax, %eax
leaq 0xf4913(%rip), %rcx     # 0xfa430
xorl %edx, %edx
testq %r14, (%rcx,%rax,8)
movl $0xffffffce, %eax       # imm = 0xFFFFFFCE
cmovnel %edx, %eax
jmp 0x5c0d
xorl %eax, %eax
jmp 0x5b47
cmpl $0x1, 0x98(%rbx)
jne 0x5b9a
addl $-0x14, %eax
jmp 0x5b9d
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $-0x1, 0x38(%rbx)
je 0x5b6a
cmpl $-0x1, 0x58(%rbx)
je 0x5b5e
cmpl $-0x1, 0x78(%rbx)
je 0x5b67
addl $-0x1e, %eax
jmp 0x5b6a
addl $-0xa, %eax
jmp 0x5b6a
xorl %eax, %eax
jmp 0x5bc2
addl $-0x14, %eax
cmpl $-0x1, 0x3c(%rbx)
je 0x5c0d
cmpl $-0x1, 0x5c(%rbx)
je 0x5c0a
cmpl $-0x1, 0x7c(%rbx)
je 0x5bb8
addl $-0xf, %eax
jmp 0x5c0d
cmpl $0x1, 0x84(%rbx)
jne 0x5bf5
addl $-0x14, %eax
jmp 0x5bf8
addl $-0x1e, %eax
cmpl $0x1, 0xdc(%rbx)
je 0x5c0d
cmpl $0x1, 0xbc(%rbx)
je 0x5c0a
cmpl $0x1, 0x9c(%rbx)
jne 0x5b84
addl $-0xa, %eax
jmp 0x5c0d
movl $0xfffffff6, %eax       # imm = 0xFFFFFFF6
cmpl $-0x1, 0x24(%rbx)
je 0x5be1
cmpl $-0x1, 0x44(%rbx)
je 0x5bd9
cmpl $-0x1, 0x64(%rbx)
je 0x5bde
addl $-0x1e, %eax
jmp 0x5be1
addl $-0xa, %eax
jmp 0x5be1
addl $-0x14, %eax
cmpl $-0x1, 0x20(%rbx)
je 0x5c0d
cmpl $-0x1, 0x40(%rbx)
je 0x5c0a
cmpl $-0x1, 0x60(%rbx)
jne 0x5b84
jmp 0x5bb8
addl $-0x1e, %eax
cmpl $0x1, 0xc0(%rbx)
je 0x5c0d
cmpl $0x1, 0xa0(%rbx)
jne 0x5c15
addl $-0x5, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
cmpl $0x1, 0x80(%rbx)
jne 0x5b84
jmp 0x5bb8