// File: 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
subq $0x60, %rsp
movq %rdi, %rax
leaq 0x10(%rbp), %rdi
movq %rdi, -0x58(%rbp)
movq %rax, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x76f0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rcx
movq %rcx, %rax
shrq %rax
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rax
subq %rax, %rcx
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
movq %rcx, %rax
andq %rdx, %rax
shrq $0x2, %rcx
andq %rdx, %rcx
addq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
addq %rcx, %rax
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rax
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rax
shrq $0x38, %rax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0xbef2
leaq -0x18(%rbp), %rdi
callq 0xc970
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0xbde6
jmp 0xbee4
movl -0x24(%rbp), %edi
movl -0xc(%rbp), %esi
movl -0x10(%rbp), %edx
callq 0x46e0
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
callq 0x4bc0
movq -0x58(%rbp), %rdi
movq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0xc(%rbp), %esi
callq 0x7840
movq -0x60(%rbp), %rsi
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
xorq $-0x1, %rax
andq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0x4bc0
movq -0x30(%rbp), %rcx
movq %rcx, %rax
shrq %rax
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rax
subq %rax, %rcx
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
movq %rcx, %rax
andq %rdx, %rax
shrq $0x2, %rcx
andq %rdx, %rcx
addq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
addq %rcx, %rax
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rax
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rax
shrq $0x38, %rax
movl %eax, -0x44(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xbee2
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0xbee0
leaq -0x38(%rbp), %rdi
callq 0xc970
movl %eax, -0x4c(%rbp)
cmpl $-0x1, -0x4c(%rbp)
jne 0xbeba
jmp 0xbed5
movl -0x24(%rbp), %edi
movl -0x4c(%rbp), %esi
callq 0xaab0
movl %eax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0xcbd0
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0xbe9e
jmp 0xbee2
jmp 0xbee4
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xbdc3
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)