// File: /workspace/github_cpp_makefile_star2_O1/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 0x830a
movq %rsi, %r14
testq %rsi, %rsi
je 0x8329
movq %rdi, %rbx
movl 0x138(%r14), %esi
xorl %eax, %eax
cmpl $0x1, %esi
setne %al
movl 0x14c(%r14,%rax,4), %r15d
leaq 0xf2857(%rip), %rax     # 0xfa8b0
testb %dl, %dl
je 0x806f
xorl %edx, %edx
cmpl $0x1, %esi
sete %dl
movq 0x1a8(%r14,%rdx,8), %r12
jmp 0x8072
xorl %r12d, %r12d
movq (%rax,%r15,8), %rax
testb %cl, %cl
je 0x808e
movq 0x1b0(%r14), %rdx
orq 0x1a8(%r14), %rdx
notq %rdx
orq %rdx, %r12
movl %ecx, 0x4(%rsp)
andq %rax, %r12
je 0x80d8
leaq 0xf2412(%rip), %rbp     # 0xfa4b0
movq %r12, %rdi
callq 0x3b5b
movl %eax, %r13d
movq %rbx, %rdi
movq %r14, %rsi
movl $0x6, %edx
movl %r15d, %ecx
movl %eax, %r8d
callq 0x8348
movq %rax, %rbx
movl %r13d, %eax
movq (%rbp,%rax,8), %rax
movq %rax, %rcx
xorq %r12, %rcx
cmpq %r12, %rax
movq %rcx, %r12
jne 0x809e
cmpb $0x0, 0x4(%rsp)
je 0x82f8
cmpl $0x3c, %r15d
jne 0x810a
cmpl $0x1, 0x138(%r14)
jne 0x810a
movq %r14, %rdi
movl $0x3c, %esi
xorl %edx, %edx
callq 0x392f
testb %al, %al
je 0x821f
cmpl $0x4, %r15d
jne 0x82f8
cmpl $0x0, 0x138(%r14)
jne 0x82f8
movq %r14, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x392f
testb %al, %al
jne 0x82f8
movq %r14, %rdi
callq 0xa986
testb %al, %al
je 0x81b4
movq %r14, %rdi
movl $0x3, %esi
callq 0xae04
testb %al, %al
je 0x81b4
movq %r14, %rdi
movl $0x2, %esi
callq 0xae04
testb %al, %al
je 0x81b4
movq %r14, %rdi
movl $0x1, %esi
callq 0xae04
testb %al, %al
je 0x81b4
movq %r14, %rdi
movl $0x3, %esi
movl $0x1, %edx
callq 0x392f
testb %al, %al
jne 0x81b4
movq %rbx, %rdi
movl $0x6, %esi
movl $0x4, %edx
movl $0x2, %ecx
callq 0x76c6
movq %rbx, %rdi
callq 0x77ab
addq $0x8, %rbx
movq %r14, %rdi
callq 0xa97a
testb %al, %al
je 0x82f8
movq %r14, %rdi
movl $0x5, %esi
callq 0xae04
testb %al, %al
je 0x82f8
movq %r14, %rdi
movl $0x6, %esi
callq 0xae04
testb %al, %al
je 0x82f8
movq %r14, %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0x392f
testb %al, %al
jne 0x82f8
movq %rbx, %rdi
movl $0x6, %esi
movl $0x4, %edx
movl $0x6, %ecx
jmp 0x82e7
movq %r14, %rdi
callq 0xa96f
testb %al, %al
je 0x8294
movq %r14, %rdi
movl $0x3b, %esi
callq 0xae04
testb %al, %al
je 0x8294
movq %r14, %rdi
movl $0x3a, %esi
callq 0xae04
testb %al, %al
je 0x8294
movq %r14, %rdi
movl $0x39, %esi
callq 0xae04
testb %al, %al
je 0x8294
movq %r14, %rdi
movl $0x3b, %esi
xorl %edx, %edx
callq 0x392f
testb %al, %al
jne 0x8294
movq %rbx, %rdi
movl $0x6, %esi
movl $0x3c, %edx
movl $0x3a, %ecx
callq 0x76c6
movq %rbx, %rdi
callq 0x77ab
addq $0x8, %rbx
movq %r14, %rdi
callq 0xa966
testb %al, %al
je 0x82f8
movq %r14, %rdi
movl $0x3d, %esi
callq 0xae04
testb %al, %al
je 0x82f8
movq %r14, %rdi
movl $0x3e, %esi
callq 0xae04
testb %al, %al
je 0x82f8
movq %r14, %rdi
movl $0x3d, %esi
xorl %edx, %edx
callq 0x392f
testb %al, %al
jne 0x82f8
movq %rbx, %rdi
movl $0x6, %esi
movl $0x3c, %edx
movl $0x3e, %ecx
callq 0x76c6
movq %rbx, %rdi
callq 0x77ab
addq $0x8, %rbx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2509a(%rip), %rdi     # 0x2d3ab
leaq 0x22720(%rip), %rsi     # 0x2aa38
leaq 0x228b1(%rip), %rcx     # 0x2abd0
movl $0xdd, %edx
callq 0x20d0
leaq 0x2cf4c(%rip), %rdi     # 0x3527c
leaq 0x22701(%rip), %rsi     # 0x2aa38
leaq 0x22892(%rip), %rcx     # 0x2abd0
movl $0xde, %edx
callq 0x20d0