// File: /workspace/github_cpp_makefile_star2_O3/jswaff[P]prophet3/src/movegen.cpp
// Function: gen_king_moves(unsigned long long*, position*, bool, bool)
move* gen_king_moves(move* m,position* p,bool caps,bool noncaps) {
    assert(m);
    assert(p);

    square_t from = p->player==WHITE ? p->white_king : p->black_king;
    uint64 mv_map = bb_king_moves[from] & get_target_squares(p,caps,noncaps);
    while (mv_map) {
        square_t sq = (square_t)get_lsb(mv_map);
        m=add_move(m,p,KING,from,sq);
        mv_map ^= bb_squares[sq];
    }

    // castling moves
    if (noncaps) {
        if (p->player==WHITE && from==E1 && !attacked(p,E1,BLACK)) {
            if (can_castle_wq(p) && is_empty_sq(p,D1) && is_empty_sq(p,C1) && is_empty_sq(p,B1)
                    && !attacked(p,D1,BLACK)) {
                m=add_castle(m,E1,C1);
            }
            if (can_castle_wk(p) && is_empty_sq(p,F1) && is_empty_sq(p,G1)
                    && !attacked(p,F1,BLACK)) {
                m=add_castle(m,E1,G1);
            }
        } else if (p->player==BLACK && from==E8 && !attacked(p,E8,WHITE)) {
            if (can_castle_bq(p) && is_empty_sq(p,D8) && is_empty_sq(p,C8) && is_empty_sq(p,B8)
                    && !attacked(p,D8,WHITE)) {
                m=add_castle(m,E8,C8);
            }
            if (can_castle_bk(p) && is_empty_sq(p,F8) && is_empty_sq(p,G8)
                    && !attacked(p,F8,WHITE)) {
                m=add_castle(m,E8,G8);
            }
        }
    }

    return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
testq %rdi, %rdi
je 0x839b
movq %rsi, %r14
testq %rsi, %rsi
je 0x83ba
movq %rdi, %rbx
movl 0x138(%r14), %esi
xorl %eax, %eax
cmpl $0x1, %esi
setne %al
movl 0x14c(%r14,%rax,4), %r15d
leaq 0xf27c5(%rip), %rax     # 0xfa8b0
testb %dl, %dl
je 0x8101
xorl %edx, %edx
cmpl $0x1, %esi
sete %dl
movq 0x1a8(%r14,%rdx,8), %r12
jmp 0x8104
xorl %r12d, %r12d
movq (%rax,%r15,8), %rax
testb %cl, %cl
je 0x8120
movq 0x1b0(%r14), %rdx
orq 0x1a8(%r14), %rdx
notq %rdx
orq %rdx, %r12
movl %ecx, 0x4(%rsp)
andq %rax, %r12
je 0x816a
leaq 0xf2380(%rip), %rbp     # 0xfa4b0
movq %r12, %rdi
callq 0x3b67
movl %eax, %r13d
movq %rbx, %rdi
movq %r14, %rsi
movl $0x6, %edx
movl %r15d, %ecx
movl %eax, %r8d
callq 0x83d9
movq %rax, %rbx
movl %r13d, %eax
movq (%rbp,%rax,8), %rax
movq %rax, %rcx
xorq %r12, %rcx
cmpq %r12, %rax
movq %rcx, %r12
jne 0x8130
cmpb $0x0, 0x4(%rsp)
je 0x8389
movl 0x138(%r14), %eax
movl %eax, %ecx
xorl $0x1, %ecx
movl %r15d, %edx
xorl $0x3c, %edx
orl %ecx, %edx
jne 0x8283
movq %r14, %rdi
movl $0x3c, %esi
xorl %edx, %edx
callq 0x393b
testb %al, %al
jne 0x8389
movq %r14, %rdi
callq 0xa92a
testb %al, %al
je 0x821b
movq %r14, %rdi
movl $0x3b, %esi
callq 0xae0c
testb %al, %al
je 0x821b
movq %r14, %rdi
movl $0x3a, %esi
callq 0xae0c
testb %al, %al
je 0x821b
movq %r14, %rdi
movl $0x39, %esi
callq 0xae0c
testb %al, %al
je 0x821b
movq %r14, %rdi
movl $0x3b, %esi
xorl %edx, %edx
callq 0x393b
testb %al, %al
jne 0x821b
movq %rbx, %rdi
movl $0x6, %esi
movl $0x3c, %edx
movl $0x3a, %ecx
callq 0x776a
movq %rbx, %rdi
callq 0x784f
addq $0x8, %rbx
movq %r14, %rdi
callq 0xa921
testb %al, %al
je 0x8389
movq %r14, %rdi
movl $0x3d, %esi
callq 0xae0c
testb %al, %al
je 0x8389
movq %r14, %rdi
movl $0x3e, %esi
callq 0xae0c
testb %al, %al
je 0x8389
movq %r14, %rdi
movl $0x3d, %esi
xorl %edx, %edx
callq 0x393b
testb %al, %al
jne 0x8389
movq %rbx, %rdi
movl $0x6, %esi
movl $0x3c, %edx
movl $0x3e, %ecx
jmp 0x8378
xorl $0x4, %r15d
orl %eax, %r15d
jne 0x8389
movq %r14, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x393b
testb %al, %al
jne 0x8389
movq %r14, %rdi
callq 0xa941
testb %al, %al
je 0x8322
movq %r14, %rdi
movl $0x3, %esi
callq 0xae0c
testb %al, %al
je 0x8322
movq %r14, %rdi
movl $0x2, %esi
callq 0xae0c
testb %al, %al
je 0x8322
movq %r14, %rdi
movl $0x1, %esi
callq 0xae0c
testb %al, %al
je 0x8322
movq %r14, %rdi
movl $0x3, %esi
movl $0x1, %edx
callq 0x393b
testb %al, %al
jne 0x8322
movq %rbx, %rdi
movl $0x6, %esi
movl $0x4, %edx
movl $0x2, %ecx
callq 0x776a
movq %rbx, %rdi
callq 0x784f
addq $0x8, %rbx
movq %r14, %rdi
callq 0xa935
testb %al, %al
je 0x8389
movq %r14, %rdi
movl $0x5, %esi
callq 0xae0c
testb %al, %al
je 0x8389
movq %r14, %rdi
movl $0x6, %esi
callq 0xae0c
testb %al, %al
je 0x8389
movq %r14, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0x393b
testb %al, %al
jne 0x8389
movq %rbx, %rdi
movl $0x6, %esi
movl $0x4, %edx
movl $0x6, %ecx
callq 0x776a
movq %rbx, %rdi
callq 0x784f
addq $0x8, %rbx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x25089(%rip), %rdi     # 0x2d42b
leaq 0x2270f(%rip), %rsi     # 0x2aab8
leaq 0x228a0(%rip), %rcx     # 0x2ac50
movl $0xdd, %edx
callq 0x20d0
leaq 0x2cf3b(%rip), %rdi     # 0x352fc
leaq 0x226f0(%rip), %rsi     # 0x2aab8
leaq 0x22881(%rip), %rcx     # 0x2ac50
movl $0xde, %edx
callq 0x20d0