// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movb %cl, %al
movb %dl, %cl
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
andb $0x1, %cl
movb %cl, -0x11(%rbp)
andb $0x1, %al
movb %al, -0x12(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0xb928
jmp 0xb947
leaq 0x2d8f4(%rip), %rdi     # 0x39223
leaq 0x2b133(%rip), %rsi     # 0x36a69
movl $0xdd, %edx
leaq 0x2b2bf(%rip), %rcx     # 0x36c01
callq 0x20c0
cmpq $0x0, -0x10(%rbp)
je 0xb950
jmp 0xb96f
leaq 0x36247(%rip), %rdi     # 0x41b9e
leaq 0x2b10b(%rip), %rsi     # 0x36a69
movl $0xde, %edx
leaq 0x2b297(%rip), %rcx     # 0x36c01
callq 0x20c0
movq -0x10(%rbp), %rax
cmpl $0x1, 0x138(%rax)
jne 0xb98b
movq -0x10(%rbp), %rax
movl 0x14c(%rax), %eax
movl %eax, -0x28(%rbp)
jmp 0xb998
movq -0x10(%rbp), %rax
movl 0x150(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %ecx
leaq 0xfa0c6(%rip), %rax     # 0x105a70
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movb -0x11(%rbp), %cl
movb -0x12(%rbp), %al
andb $0x1, %cl
andb $0x1, %al
movzbl %cl, %esi
movzbl %al, %edx
callq 0xc040
movq %rax, %rcx
movq -0x30(%rbp), %rax
andq %rcx, %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0xba24
movq -0x20(%rbp), %rdi
callq 0x4870
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0x18(%rbp), %ecx
movl -0x24(%rbp), %r8d
movl $0x6, %edx
callq 0xbc70
movq %rax, -0x8(%rbp)
movl -0x24(%rbp), %eax
movl %eax, %ecx
leaq 0xf9c5a(%rip), %rax     # 0x105670
movq (%rax,%rcx,8), %rax
xorq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0xb9da
testb $0x1, -0x12(%rbp)
je 0xbc58
movq -0x10(%rbp), %rax
cmpl $0x1, 0x138(%rax)
jne 0xbb3e
cmpl $0x3c, -0x18(%rbp)
jne 0xbb3e
movq -0x10(%rbp), %rdi
movl $0x3c, %esi
xorl %edx, %edx
callq 0x4430
testb $0x1, %al
jne 0xbb3e
movq -0x10(%rbp), %rdi
callq 0xf200
testb $0x1, %al
jne 0xba70
jmp 0xbad7
movq -0x10(%rbp), %rdi
movl $0x3b, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xba84
jmp 0xbad7
movq -0x10(%rbp), %rdi
movl $0x3a, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xba98
jmp 0xbad7
movq -0x10(%rbp), %rdi
movl $0x39, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbaac
jmp 0xbad7
movq -0x10(%rbp), %rdi
movl $0x3b, %esi
xorl %edx, %edx
callq 0x4430
testb $0x1, %al
jne 0xbad7
movq -0x8(%rbp), %rdi
movl $0x3c, %esi
movl $0x3a, %edx
callq 0xbd40
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf1e0
testb $0x1, %al
jne 0xbae6
jmp 0xbb39
movq -0x10(%rbp), %rdi
movl $0x3d, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbafa
jmp 0xbb39
movq -0x10(%rbp), %rdi
movl $0x3e, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbb0e
jmp 0xbb39
movq -0x10(%rbp), %rdi
movl $0x3d, %esi
xorl %edx, %edx
callq 0x4430
testb $0x1, %al
jne 0xbb39
movq -0x8(%rbp), %rdi
movl $0x3c, %esi
movl $0x3e, %edx
callq 0xbd40
movq %rax, -0x8(%rbp)
jmp 0xbc56
movq -0x10(%rbp), %rax
cmpl $0x0, 0x138(%rax)
jne 0xbc54
cmpl $0x4, -0x18(%rbp)
jne 0xbc54
movq -0x10(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x4430
testb $0x1, %al
jne 0xbc54
movq -0x10(%rbp), %rdi
callq 0xf240
testb $0x1, %al
jne 0xbb83
jmp 0xbbed
movq -0x10(%rbp), %rdi
movl $0x3, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbb97
jmp 0xbbed
movq -0x10(%rbp), %rdi
movl $0x2, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbbab
jmp 0xbbed
movq -0x10(%rbp), %rdi
movl $0x1, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbbbf
jmp 0xbbed
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x1, %edx
callq 0x4430
testb $0x1, %al
jne 0xbbed
movq -0x8(%rbp), %rdi
movl $0x4, %esi
movl $0x2, %edx
callq 0xbd40
movq %rax, -0x8(%rbp)
movq -0x10(%rbp), %rdi
callq 0xf220
testb $0x1, %al
jne 0xbbfc
jmp 0xbc52
movq -0x10(%rbp), %rdi
movl $0x5, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbc10
jmp 0xbc52
movq -0x10(%rbp), %rdi
movl $0x6, %esi
callq 0xf9c0
testb $0x1, %al
jne 0xbc24
jmp 0xbc52
movq -0x10(%rbp), %rdi
movl $0x5, %esi
movl $0x1, %edx
callq 0x4430
testb $0x1, %al
jne 0xbc52
movq -0x8(%rbp), %rdi
movl $0x4, %esi
movl $0x6, %edx
callq 0xbd40
movq %rax, -0x8(%rbp)
jmp 0xbc54
jmp 0xbc56
jmp 0xbc58
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)