// File: /workspace/github_cpp_makefile_star2_O1/niekh1234[P]nnchesslib/movegen.cpp
// Function: nnchesslib::genNonSlidingMoves(nnchesslib::ChessBoard, std::vector<unsigned int, std::allocator<unsigned int>>&, nnchesslib::Color, nnchesslib::PieceType)
void nnchesslib::genNonSlidingMoves(ChessBoard board, MoveList& moveList, Color color, PieceType piece)
{
    // getting target pieces on the board for a specific color.
    BitBoard targets = board.getBoard(color, piece);

    int count = __builtin_popcountll(targets.board);

    for(int _ = 0; _ < count; _++)
    {
        int index = popLsb(targets.board);

        if (index == -1)
            continue;

        BitBoard targetMoves = Attacks::getNonSlidingAttacks(index, color, piece);
        BitBoard targetAttacks = targetMoves.board & ~board.getBoard(color).board;

        int attackCount = __builtin_popcountll(targetMoves.board);

        if(targetAttacks.board)
        {
            for(int num = 0; num < attackCount; num++)
            {
                // getting the index of a square that our piece can move to/capture.
                int attackIndex = popLsb(targetAttacks.board);

                if (attackIndex == -1)
                    continue;
                // generating a move with our piece index and the potential square it can move to. 
                Move move = createMove(index, attackIndex);       
                moveList.push_back(move);
            }
        }
    }    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r15
leaq 0x10(%rbp), %rdi
movl %esi, -0x2c(%rbp)
movl %edx, -0x3c(%rbp)
callq 0x4d8a
testq %rax, %rax
je 0x77fe
movq %rax, %r13
shrq %rax
movabsq $0x5555555555555555, %rcx # imm = 0x5555555555555555
andq %rcx, %rax
movq %r13, %rcx
subq %rax, %rcx
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
movq %rcx, %rax
andq %rdx, %rax
shrq $0x2, %rcx
andq %rdx, %rcx
addq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
addq %rcx, %rax
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rax
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rax
shrq $0x38, %rax
cmpl $0x2, %eax
movl $0x1, %ecx
cmovbl %ecx, %eax
xorl %r14d, %r14d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rax, -0x50(%rbp)
bsfq %r13, %rbx
movq %r13, %rdx
subq $0x1, %rdx
cmovbl %ecx, %ebx
jb 0x77ef
movq %rdx, -0x58(%rbp)
movl %ebx, %edi
movl -0x2c(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x31a9
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x334a
movq -0x48(%rbp), %r12
leaq 0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x4e20
notq %rax
andq %rax, %r12
leaq -0x38(%rbp), %rdi
movq %r12, %rsi
callq 0x334a
movq -0x48(%rbp), %rax
cmpq $0x0, -0x38(%rbp)
setne %cl
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x77e2
movq %rax, %rcx
shrq %rcx
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rcx
subq %rcx, %rax
movq %rax, %rcx
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
andq %rdx, %rcx
shrq $0x2, %rax
andq %rdx, %rax
addq %rcx, %rax
movq %rax, %r12
shrq $0x4, %r12
addq %rax, %r12
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %r12
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %r12
shrq $0x38, %r12
cmpl $0x2, %r12d
movl $0x1, %eax
cmovbl %eax, %r12d
movq -0x38(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x38(%rbp)
cmpq $0x1, %rax
jb 0x77dd
bsfq %rax, %rsi
movl %ebx, %edi
callq 0x6a8a
movl %eax, -0x40(%rbp)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x77d1
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r15)
jmp 0x77dd
movq %r15, %rdi
leaq -0x40(%rbp), %rdx
callq 0x845c
decl %r12d
jne 0x7798
movq -0x50(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq -0x58(%rbp), %rdx
andq %rdx, %r13
incl %r14d
cmpl %eax, %r14d
jne 0x76c2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq