// File: /workspace/github_cpp_makefile_star2_O2/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, -0x34(%rbp)
movl %edx, -0x38(%rbp)
callq 0x4388
movq %rax, %rdx
shrq %rdx
movabsq $0x5555555555555555, %rcx # imm = 0x5555555555555555
andq %rcx, %rdx
movq %rax, %rcx
subq %rdx, %rcx
movabsq $0x3333333333333333, %rsi # imm = 0x3333333333333333
movq %rcx, %rdx
andq %rsi, %rdx
shrq $0x2, %rcx
andq %rsi, %rcx
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x4, %rdx
addq %rcx, %rdx
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rdx
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rdx
shrq $0x38, %rdx
xorl %ecx, %ecx
pushq $-0x1
popq %rsi
movq %rdx, -0x50(%rbp)
cmpl %edx, %ecx
je 0x6780
bsfq %rax, %rdi
movq %rax, %r13
movl %edi, %ebx
subq $0x1, %r13
cmovbl %esi, %ebx
jb 0x6776
movl %ecx, -0x3c(%rbp)
movq %rax, -0x58(%rbp)
movl -0x34(%rbp), %r12d
movl %r12d, %esi
movl -0x38(%rbp), %edx
callq 0x2e6e
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x3036
movq -0x48(%rbp), %r14
leaq 0x10(%rbp), %rdi
movl %r12d, %esi
leaq -0x40(%rbp), %r12
callq 0x440e
notq %rax
andq %rax, %r14
leaq -0x30(%rbp), %rdi
movq %r14, %rsi
callq 0x3036
cmpq $0x0, -0x30(%rbp)
je 0x6768
movq -0x48(%rbp), %rax
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, %r14
shrq $0x4, %r14
addq %rax, %r14
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %r14
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %r14
shrq $0x38, %r14
subl $0x1, %r14d
jb 0x6768
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
cmpq $0x1, %rax
jb 0x6732
bsfq %rax, %rsi
movl %ebx, %edi
callq 0x5c0a
movl %eax, -0x40(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x71b0
jmp 0x6732
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rdx
movl -0x3c(%rbp), %ecx
pushq $-0x1
popq %rsi
andq %r13, %rax
incl %ecx
jmp 0x6664
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq