// File: /workspace/github_cpp_makefile_star2_O2/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 0x7da2
movq %rsi, %r8
testq %rsi, %rsi
je 0x7dc1
movq %rdi, %rbx
xorl %eax, %eax
cmpl $0x1, 0x138(%r8)
setne %al
movl 0x14c(%r8,%rax,4), %r15d
leaq 0xf5d36(%rip), %rax     # 0xfd8b0
movq (%rax,%r15,8), %r13
movzbl %dl, %esi
movl %ecx, 0x4(%rsp)
movzbl %cl, %edx
movq %r8, %r14
movq %r8, %rdi
callq 0x807b
movq %rax, %r12
andq %r13, %r12
leaq 0xf5910(%rip), %r13     # 0xfd4b0
testq %r12, %r12
je 0x7bcf
movq %r12, %rdi
callq 0x397d
movl %eax, %ebp
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x6
popq %rdx
movl %r15d, %ecx
movl %eax, %r8d
callq 0x7de0
movq %rax, %rbx
movl %ebp, %eax
xorq (%r13,%rax,8), %r12
jmp 0x7ba0
cmpb $0x0, 0x4(%rsp)
je 0x7d74
movl 0x138(%r14), %eax
movl %eax, %ecx
xorl $0x1, %ecx
movl %r15d, %edx
xorl $0x3c, %edx
orl %ecx, %edx
jne 0x7cb9
pushq $0x3c
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3791
testb %al, %al
jne 0x7d74
movq %r14, %rdi
callq 0xa0c3
testb %al, %al
je 0x7c64
pushq $0x3b
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7c64
pushq $0x3a
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7c64
pushq $0x39
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7c64
pushq $0x3b
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3791
testb %al, %al
jne 0x7c64
pushq $0x3c
popq %rsi
pushq $0x3a
popq %rdx
movq %rbx, %rdi
callq 0x7e7e
movq %rax, %rbx
movq %r14, %rdi
callq 0xa0ba
testb %al, %al
je 0x7d74
pushq $0x3d
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7d74
pushq $0x3e
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7d74
pushq $0x3d
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x3791
testb %al, %al
jne 0x7d74
pushq $0x3c
popq %rsi
pushq $0x3e
jmp 0x7d8b
xorl $0x4, %r15d
orl %eax, %r15d
jne 0x7d74
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x3791
testb %al, %al
jne 0x7d74
movq %r14, %rdi
callq 0xa0da
testb %al, %al
je 0x7d38
pushq $0x3
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7d38
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7d38
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7d38
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x3791
testb %al, %al
jne 0x7d38
pushq $0x4
popq %rsi
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x7e7e
movq %rax, %rbx
movq %r14, %rdi
callq 0xa0ce
testb %al, %al
je 0x7d74
pushq $0x5
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7d74
pushq $0x6
popq %rsi
movq %r14, %rdi
callq 0xa4ea
testb %al, %al
je 0x7d74
pushq $0x5
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x3791
testb %al, %al
je 0x7d86
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x4
popq %rsi
pushq $0x6
popq %rdx
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7e7e
leaq 0x23632(%rip), %rdi     # 0x2b3db
leaq 0x20cb8(%rip), %rsi     # 0x28a68
leaq 0x20e49(%rip), %rcx     # 0x28c00
movl $0xdd, %edx
callq 0x20c0
leaq 0x2b5f7(%rip), %rdi     # 0x333bf
leaq 0x20c99(%rip), %rsi     # 0x28a68
leaq 0x20e2a(%rip), %rcx     # 0x28c00
movl $0xde, %edx
callq 0x20c0