// File: /workspace/github_cpp_makefile_star2_O2/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 0x555f
movl 0x14c(%rbx), %edi
callq 0xbfd8
cmpl $0x5, %eax
jl 0x5595
xorl %eax, %eax
cmpl $0x1, 0xd4(%rbx)
je 0x565c
cmpl $0x1, 0xb4(%rbx)
jne 0x564c
pushq $-0xa
popq %rax
jmp 0x565c
movl 0x150(%rbx), %edi
callq 0xbfd8
cmpl $0x5, %eax
jl 0x55c9
cmpl $-0x1, 0x34(%rbx)
je 0x5648
cmpl $-0x1, 0x54(%rbx)
je 0x5688
cmpl $-0x1, 0x74(%rbx)
pushq $-0x14
popq %rcx
pushq $-0x1e
popq %rax
cmovel %ecx, %eax
jmp 0x568b
movl 0x14c(%rbx), %edi
callq 0xbfd8
cmpl $0x2, %eax
jg 0x55ff
xorl %eax, %eax
cmpl $0x1, 0xc8(%rbx)
je 0x56e3
cmpl $0x1, 0xa8(%rbx)
jne 0x56d3
pushq $-0xa
popq %rax
jmp 0x56e3
movl 0x150(%rbx), %edi
callq 0xbfd8
cmpl $0x2, %eax
jg 0x5615
cmpl $-0x1, 0x28(%rbx)
je 0x56a7
cmpl $-0x1, 0x48(%rbx)
je 0x572b
cmpl $-0x1, 0x68(%rbx)
pushq $-0x14
popq %rcx
pushq $-0x1e
popq %rax
cmovel %ecx, %eax
jmp 0x572e
movq 0x160(%rbx), %r14
orq 0x158(%rbx), %r14
movl 0x14c(%rbx), %edi
jmp 0x5629
movq 0x160(%rbx), %r14
orq 0x158(%rbx), %r14
movl 0x150(%rbx), %edi
callq 0xbfd8
movl %eax, %eax
leaq 0xf7df9(%rip), %rcx     # 0xfd430
xorl %edx, %edx
testq %r14, (%rcx,%rax,8)
pushq $-0x32
popq %rax
cmovnel %edx, %eax
jmp 0x577d
xorl %eax, %eax
jmp 0x568b
cmpl $0x1, 0x94(%rbx)
pushq $-0x14
popq %rcx
pushq $-0x1e
popq %rax
cmovel %ecx, %eax
cmpl $0x1, 0xd8(%rbx)
je 0x56fd
cmpl $0x1, 0xb8(%rbx)
jne 0x567a
addl $-0xa, %eax
jmp 0x56fd
cmpl $0x1, 0x98(%rbx)
jne 0x56fa
addl $-0x14, %eax
jmp 0x56fd
pushq $-0xa
popq %rax
cmpl $-0x1, 0x38(%rbx)
je 0x56b1
cmpl $-0x1, 0x58(%rbx)
je 0x56a2
cmpl $-0x1, 0x78(%rbx)
je 0x56ae
addl $-0x1e, %eax
jmp 0x56b1
addl $-0xa, %eax
jmp 0x56b1
xorl %eax, %eax
jmp 0x572e
addl $-0x14, %eax
cmpl $-0x1, 0x3c(%rbx)
je 0x577d
cmpl $-0x1, 0x5c(%rbx)
je 0x577a
cmpl $-0x1, 0x7c(%rbx)
je 0x5718
addl $-0xf, %eax
jmp 0x577d
cmpl $0x1, 0x88(%rbx)
pushq $-0x14
popq %rcx
pushq $-0x1e
popq %rax
cmovel %ecx, %eax
cmpl $0x1, 0xc4(%rbx)
je 0x5768
cmpl $0x1, 0xa4(%rbx)
jne 0x571d
addl $-0xa, %eax
jmp 0x5768
addl $-0x1e, %eax
cmpl $0x1, 0xdc(%rbx)
je 0x577d
cmpl $0x1, 0xbc(%rbx)
je 0x577a
cmpl $0x1, 0x9c(%rbx)
jne 0x56cb
addl $-0xa, %eax
jmp 0x577d
cmpl $0x1, 0x84(%rbx)
jne 0x5765
addl $-0x14, %eax
jmp 0x5768
pushq $-0xa
popq %rax
cmpl $-0x1, 0x24(%rbx)
je 0x574d
cmpl $-0x1, 0x44(%rbx)
je 0x5745
cmpl $-0x1, 0x64(%rbx)
je 0x574a
addl $-0x1e, %eax
jmp 0x574d
addl $-0xa, %eax
jmp 0x574d
addl $-0x14, %eax
cmpl $-0x1, 0x20(%rbx)
je 0x577d
cmpl $-0x1, 0x40(%rbx)
je 0x577a
cmpl $-0x1, 0x60(%rbx)
jne 0x56cb
jmp 0x5718
addl $-0x1e, %eax
cmpl $0x1, 0xc0(%rbx)
je 0x577d
cmpl $0x1, 0xa0(%rbx)
jne 0x5785
addl $-0x5, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
cmpl $0x1, 0x80(%rbx)
jne 0x56cb
jmp 0x5718