// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %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, 0x30(%rsp)
movl 0x8(%rcx), %ebp
je 0x3ac8
shrl $0x2, %ebp
movl $0x38, %eax
movq %rax, 0x18(%rsp)
movl $0x39, %eax
movq %rax, 0x28(%rsp)
movl $0x3f, %r13d
movl $0x158, %eax            # imm = 0x158
jmp 0x3ae6
movq $0x0, 0x18(%rsp)
movl $0x1, %eax
movq %rax, 0x28(%rsp)
movl $0x7, %r13d
movl $0x128, %eax            # imm = 0x128
movq 0x170(%r14), %rcx
movq %rcx, 0x40(%rsp)
bsfq (%r14,%rax), %r12
movq %r14, %rdi
movl %r12d, %esi
callq 0x578c
andq 0x30(%rsp), %rax
cmpq $0x1, %rax
sete %bl
testb $0x1, %bpl
je 0x3bc2
cmpq $0x1, %rax
je 0x3bc2
movl %ebp, 0x20(%rsp)
movq %r15, 0x8(%rsp)
movq %r12, 0x10(%rsp)
leal 0x1(%r12), %eax
movq %r13, 0x38(%rsp)
cmpl %r13d, %eax
jae 0x3b88
movq 0x10(%rsp), %r12
incq %r12
movl 0x38(%rsp), %ebp
movl $0x1, %r15d
leaq 0x8288(%rip), %rax      # 0xbde0
movq (%rax,%r12,8), %r13
andq 0x40(%rsp), %r13
movq %r14, %rdi
movl %r12d, %esi
callq 0x578c
andq 0x30(%rsp), %rax
orq %r13, %rax
movzbl %bl, %ebx
cmovnel %r15d, %ebx
testb $0x1, %bl
jne 0x3b88
incq %r12
cmpq %rbp, %r12
jb 0x3b51
testb $0x1, %bl
je 0x3b9b
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movq 0x10(%rsp), %r12
jmp 0x3bbe
movq 0x38(%rsp), %rax
shll $0x6, %eax
movq 0x10(%rsp), %r12
addl %r12d, %eax
addl $0x8000, %eax           # imm = 0x8000
movq 0x8(%rsp), %r15
movl %eax, (%r15)
addq $0x8, %r15
xorl %ebx, %ebx
movl 0x20(%rsp), %ebp
testb $0x2, %bpl
je 0x3c84
testb %bl, %bl
jne 0x3c84
movq %r15, 0x8(%rsp)
movq %r12, 0x10(%rsp)
jne 0x3c58
leal -0x1(%r12), %ebp
cmpl 0x18(%rsp), %ebp
jle 0x3c58
movl %ebp, %r12d
movl 0x18(%rsp), %eax
movq %rax, 0x20(%rsp)
decq %r12
negq 0x28(%rsp)
movq 0x28(%rsp), %rax
leaq (%rax,%r12), %r15
leaq 0x81d1(%rip), %rax      # 0xbde0
movq 0x8(%rax,%r12,8), %r13
movq %r14, %rdi
movl %ebp, %esi
callq 0x578c
testq %r13, 0x40(%rsp)
movzbl %bl, %ecx
movl $0x1, %edx
cmovnel %edx, %ecx
cmpq $-0x1, %r15
movl $0x1, %ebx
cmovel %ecx, %ebx
testq %rax, 0x30(%rsp)
cmovel %ecx, %ebx
testb $0x1, %bl
jne 0x3c58
decl %ebp
leaq -0x1(%r12), %rax
cmpq 0x20(%rsp), %r12
movq %rax, %r12
jg 0x3bff
testb $0x1, %bl
je 0x3c64
movq 0x8(%rsp), %r15
jmp 0x3c84
movq 0x18(%rsp), %rax
shll $0x6, %eax
movq 0x10(%rsp), %rcx
addl %ecx, %eax
addl $0x8000, %eax           # imm = 0x8000
movq 0x8(%rsp), %r15
movl %eax, (%r15)
addq $0x8, %r15
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq