// File: /workspace/github_cpp_makefile_star2_O3/bjohnsonfl[P]Overhead-Chess-Engine-/moveGen.cpp
// Function: generateCastling(position const&, candidate*)
candidate* generateCastling(const position& pos, candidate* moveList)
{
    player us = pos.get_sideToPlay();
    player them = player(!us);
    bitboard occupied  = pos.get_pieces();
    bitboard enemy = pos.get_pieces(them);
    int castlightRights = us == white ? pos.get_castlingRights(white) : pos.get_castlingRights(black) >> 2;
    square sq, kSq; //rkSq
    bool illegal = false;
    kSq = lsb_sq(pos.get_king(us));
    
    illegal = ((pos.squareAttackedBy(kSq)) & enemy) == true ? true :false;
    
    square a_1 = us == white ? a1 : a8;
    square b_1 = us == white ? b1 : b8;
    square h_1 = us == white ? h1 : h8;
    
    if((castlightRights & 0x1) && !illegal) // KING SIDE CASTLE
    {
        sq = square(kSq + 1);
        while(!illegal && (sq < h_1))
        {
            if(squareBitMask[sq] & occupied) illegal = true;
            if((pos.squareAttackedBy(sq)) & enemy) illegal = true;
           
            sq = square( sq + 1 );
        }
        
        if(!illegal)
        {
            //make move
            (*moveList++).mv = make_move(kSq, h_1, pieceType(0) , castling);
            
        }
        
        
        illegal = false;
    }
    if ((castlightRights & 0x2) && !illegal) // QUEEN SIDE CASTLE
    {
        sq = square(kSq - 1);
        //BUGGGGGGGGGG b1 is never cheked
        while(!illegal && (sq > a_1))
        {
            if(squareBitMask[sq] & occupied) illegal = true;
            if(((pos.squareAttackedBy(sq)) & enemy) && sq != b_1) illegal = true;
           // printBitboard(pos.squareAttackedBy(sq));
           // printBitboard(pos.squareAttackedBy(sq) & enemy);
            
            sq = square( sq - 1 );
        }
        
        if(!illegal)
        {
            //make move
            (*moveList++).mv = make_move(kSq, a_1, pieceType(0) , castling);
           
        }
    }
    
    
    return moveList;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
movq %rdi, %r14
xorl %eax, %eax
cmpl $0x0, 0x17c(%rdi)
sete %al
movq 0x188(%rdi), %rcx
movq 0x160(%rdi,%rax,8), %rax
movq %rax, 0x28(%rsp)
movl 0x8(%rcx), %ebp
je 0x3b8c
shrl $0x2, %ebp
movl $0x38, %r13d
movl $0x39, %eax
movq %rax, 0x20(%rsp)
movl $0x3f, %eax
movq %rax, (%rsp)
movl $0x158, %eax            # imm = 0x158
jmp 0x3ba7
xorl %r13d, %r13d
movl $0x1, %eax
movq %rax, 0x20(%rsp)
movl $0x7, %eax
movq %rax, (%rsp)
movl $0x128, %eax            # imm = 0x128
movq 0x170(%r14), %rcx
movq %rcx, 0x30(%rsp)
bsfq (%r14,%rax), %rbx
movq %r14, %rdi
movl %ebx, %esi
callq 0x57e6
andq 0x28(%rsp), %rax
cmpq $0x1, %rax
sete %r12b
testb $0x1, %bpl
je 0x3c5e
cmpq $0x1, %rax
je 0x3c5e
leal 0x1(%rbx), %eax
movq (%rsp), %rcx
cmpl %ecx, %eax
movq %rcx, %rax
jae 0x3c4a
movl %ebp, 0x8(%rsp)
movq %r13, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %rbx, %r12
incq %r12
movl %eax, %ebx
leaq 0x81d6(%rip), %rbp      # 0xbde0
movq (%rbp,%r12,8), %r13
andq 0x30(%rsp), %r13
movq %r14, %rdi
movl %r12d, %esi
callq 0x57e6
andq 0x28(%rsp), %rax
orq %r13, %rax
setne %al
jne 0x3c34
incq %r12
cmpq %rbx, %r12
jb 0x3c0a
testb %al, %al
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rbx
movl 0x8(%rsp), %ebp
movq (%rsp), %rax
jne 0x3c5b
shll $0x6, %eax
addl %ebx, %eax
addl $0x8000, %eax           # imm = 0x8000
movl %eax, (%r15)
addq $0x8, %r15
xorl %r12d, %r12d
testb $0x2, %bpl
je 0x3d22
testb %r12b, %r12b
jne 0x3d22
leal -0x1(%rbx), %ebp
cmpl %r13d, %ebp
jle 0x3d0e
movq %rbx, 0x10(%rsp)
movq %r15, 0x8(%rsp)
movl %ebp, %ebx
movq %r13, 0x18(%rsp)
movl %r13d, %eax
movq %rax, (%rsp)
decq %rbx
negq 0x20(%rsp)
movq 0x20(%rsp), %rax
leaq (%rax,%rbx), %r13
leaq 0x8133(%rip), %rax      # 0xbde0
movq 0x8(%rax,%rbx,8), %r15
movq %r14, %rdi
movl %ebp, %esi
callq 0x57e6
testq %r15, 0x30(%rsp)
movzbl %r12b, %ecx
movl $0x1, %edx
cmovnel %edx, %ecx
cmpq $-0x1, %r13
movl $0x1, %r12d
cmovel %ecx, %r12d
testq %rax, 0x28(%rsp)
cmovel %ecx, %r12d
testb $0x1, %r12b
jne 0x3cf9
decl %ebp
leaq -0x1(%rbx), %rax
cmpq (%rsp), %rbx
movq %rax, %rbx
jg 0x3c9d
testb $0x1, %r12b
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rbx
jne 0x3d22
shll $0x6, %r13d
leal (%rbx,%r13), %eax
addl $0x8000, %eax           # imm = 0x8000
movl %eax, (%r15)
addq $0x8, %r15
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq