// File: niekh1234[P]nnchesslib/board.cpp
// Function: nnchesslib::ChessBoard::updateCastlingRights()
void ChessBoard::updateCastlingRights()
{
    // if the rooks have moved:
    bool whiteQSRook = BitBoard(boardinfo.whitePieces.board & boardinfo.rooks.board).get(A1);
    bool whiteKSRook = BitBoard(boardinfo.whitePieces.board & boardinfo.rooks.board).get(H1);
    bool blackQSRook = BitBoard(boardinfo.blackPieces.board & boardinfo.rooks.board).get(A8);
    bool blackKSRook = BitBoard(boardinfo.blackPieces.board & boardinfo.rooks.board).get(H8);
    // castling rights can only go from true to false. If you move a rook back into proper position you cannot castle anymore.
    if(!whiteQSRook && boardinfo.whiteCastleLong) boardinfo.whiteCastleLong = false;
    if(!whiteKSRook && boardinfo.whiteCastleShort) boardinfo.whiteCastleShort = false;
    if(!blackQSRook && boardinfo.blackCastleLong) boardinfo.blackCastleLong = false;
    if(!blackKSRook && boardinfo.blackCastleShort) boardinfo.blackCastleShort = false;

    // if the kings have moved:
    bool whiteKing = BitBoard(boardinfo.whitePieces.board & boardinfo.kings.board).get(E1);
    bool blackKing = BitBoard(boardinfo.blackPieces.board & boardinfo.kings.board).get(E8);
    if(!whiteKing && (boardinfo.whiteCastleLong || boardinfo.whiteCastleShort))
    {
        boardinfo.whiteCastleLong = false;
        boardinfo.whiteCastleShort = false;
    } else if (!blackKing && (boardinfo.blackCastleLong || boardinfo.blackCastleShort))
    {
        boardinfo.blackCastleLong = false;
        boardinfo.blackCastleShort = false;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq (%rax), %rsi
andq 0x28(%rax), %rsi
leaq -0x18(%rbp), %rdi
callq 0x4bc0
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x4ce0
movl %eax, %ecx
movq -0x70(%rbp), %rax
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, -0x9(%rbp)
movq (%rax), %rsi
andq 0x28(%rax), %rsi
leaq -0x28(%rbp), %rdi
callq 0x4bc0
leaq -0x28(%rbp), %rdi
movl $0x7, %esi
callq 0x4ce0
movl %eax, %ecx
movq -0x70(%rbp), %rax
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, -0x19(%rbp)
movq 0x8(%rax), %rsi
andq 0x28(%rax), %rsi
leaq -0x38(%rbp), %rdi
callq 0x4bc0
leaq -0x38(%rbp), %rdi
movl $0x38, %esi
callq 0x4ce0
movl %eax, %ecx
movq -0x70(%rbp), %rax
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, -0x29(%rbp)
movq 0x8(%rax), %rsi
andq 0x28(%rax), %rsi
leaq -0x48(%rbp), %rdi
callq 0x4bc0
leaq -0x48(%rbp), %rdi
movl $0x3f, %esi
callq 0x4ce0
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x39(%rbp)
testb $0x1, -0x9(%rbp)
jne 0x7fe4
movq -0x70(%rbp), %rax
testb $0x1, 0x42(%rax)
je 0x7fe4
movq -0x70(%rbp), %rax
movb $0x0, 0x42(%rax)
testb $0x1, -0x19(%rbp)
jne 0x7ffc
movq -0x70(%rbp), %rax
testb $0x1, 0x41(%rax)
je 0x7ffc
movq -0x70(%rbp), %rax
movb $0x0, 0x41(%rax)
testb $0x1, -0x29(%rbp)
jne 0x8014
movq -0x70(%rbp), %rax
testb $0x1, 0x44(%rax)
je 0x8014
movq -0x70(%rbp), %rax
movb $0x0, 0x44(%rax)
testb $0x1, -0x39(%rbp)
jne 0x802c
movq -0x70(%rbp), %rax
testb $0x1, 0x43(%rax)
je 0x802c
movq -0x70(%rbp), %rax
movb $0x0, 0x43(%rax)
movq -0x70(%rbp), %rax
movq (%rax), %rsi
andq 0x38(%rax), %rsi
leaq -0x58(%rbp), %rdi
callq 0x4bc0
leaq -0x58(%rbp), %rdi
movl $0x4, %esi
callq 0x4ce0
movl %eax, %ecx
movq -0x70(%rbp), %rax
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, -0x49(%rbp)
movq 0x8(%rax), %rsi
andq 0x38(%rax), %rsi
leaq -0x68(%rbp), %rdi
callq 0x4bc0
leaq -0x68(%rbp), %rdi
movl $0x3c, %esi
callq 0x4ce0
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x59(%rbp)
testb $0x1, -0x49(%rbp)
jne 0x80b2
movq -0x70(%rbp), %rax
testb $0x1, 0x42(%rax)
jne 0x80a4
movq -0x70(%rbp), %rax
testb $0x1, 0x41(%rax)
je 0x80b2
movq -0x70(%rbp), %rax
movb $0x0, 0x42(%rax)
movb $0x0, 0x41(%rax)
jmp 0x80da
testb $0x1, -0x59(%rbp)
jne 0x80d8
movq -0x70(%rbp), %rax
testb $0x1, 0x44(%rax)
jne 0x80cc
movq -0x70(%rbp), %rax
testb $0x1, 0x43(%rax)
je 0x80d8
movq -0x70(%rbp), %rax
movb $0x0, 0x44(%rax)
movb $0x0, 0x43(%rax)
jmp 0x80da
addq $0x70, %rsp
popq %rbp
retq