// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
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 0x39b3
shrl $0x2, %ebp
pushq $0x38
popq %r13
pushq $0x39
popq %rax
movq %rax, 0x30(%rsp)
pushq $0x3f
popq %rax
movq %rax, 0x20(%rsp)
movl $0x158, %eax            # imm = 0x158
jmp 0x39cb
xorl %r13d, %r13d
pushq $0x1
popq %rax
movq %rax, 0x30(%rsp)
pushq $0x7
popq %rax
movq %rax, 0x20(%rsp)
movl $0x128, %eax            # imm = 0x128
movq 0x170(%r14), %rbx
bsfq (%r14,%rax), %rsi
movq %r14, %rdi
movq %rsi, 0x18(%rsp)
callq 0x525a
andq 0x28(%rsp), %rax
cmpq $0x1, %rax
sete %r15b
movl %ebp, (%rsp)
testb $0x1, %bpl
je 0x3a8c
cmpq $0x1, %rax
je 0x3a8c
movq %r13, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movl 0x20(%rsp), %r15d
movq 0x18(%rsp), %rax
leaq 0x1(%rax), %r12
xorl %eax, %eax
leaq 0x83b7(%rip), %r13      # 0xbde0
testb $0x1, %al
jne 0x3a55
cmpq %r15, %r12
jae 0x3a55
movq (%r13,%r12,8), %rbp
andq %rbx, %rbp
movq %r14, %rdi
movl %r12d, %esi
callq 0x525a
andq 0x28(%rsp), %rax
orq %rbp, %rax
setne %al
incq %r12
jmp 0x3a29
testb $0x1, %al
je 0x3a63
xorl %r15d, %r15d
movq 0x8(%rsp), %r12
jmp 0x3a87
movq 0x20(%rsp), %rcx
shll $0x6, %ecx
movq 0x18(%rsp), %rax
addl %ecx, %eax
addl $0x8000, %eax           # imm = 0x8000
movq 0x8(%rsp), %r12
movl %eax, (%r12)
addq $0x8, %r12
xorl %r15d, %r15d
movq 0x10(%rsp), %r13
movl (%rsp), %eax
testb $0x2, %al
je 0x3b35
testb %r15b, %r15b
jne 0x3b35
movq %r12, 0x8(%rsp)
movq %r13, 0x10(%rsp)
movl %r13d, %eax
movq %rax, (%rsp)
movq 0x18(%rsp), %r12
decq %r12
pushq $0x1
popq %r13
testb $0x1, %r15b
jne 0x3b07
cmpq (%rsp), %r12
jle 0x3b07
movl %r12d, %eax
leaq 0x830d(%rip), %rcx      # 0xbde0
movq (%rcx,%rax,8), %rbp
movq %r14, %rdi
movl %r12d, %esi
callq 0x525a
testq %rbx, %rbp
movzbl %r15b, %ecx
cmovnel %r13d, %ecx
cmpq %r12, 0x30(%rsp)
movl %r13d, %r15d
cmovel %ecx, %r15d
testq %rax, 0x28(%rsp)
cmovel %ecx, %r15d
decq %r12
jmp 0x3abd
testb $0x1, %r15b
je 0x3b14
movq 0x8(%rsp), %r12
jmp 0x3b35
movq 0x10(%rsp), %rax
shll $0x6, %eax
movq 0x18(%rsp), %rcx
addl %ecx, %eax
addl $0x8000, %eax           # imm = 0x8000
movq 0x8(%rsp), %r12
movl %eax, (%r12)
addq $0x8, %r12
movq %r12, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq