// File: /workspace/github_cpp_makefile_star2_O3/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 0x4d9e
testq %rax, %rax
je 0x77a8
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
xorl %r14d, %r14d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %rax, -0x50(%rbp)
bsfq %r13, %rdi
movq %r13, %rdx
movl %edi, %ebx
subq $0x1, %rdx
cmovbl %ecx, %ebx
jb 0x7799
movq %rdx, -0x58(%rbp)
movl -0x2c(%rbp), %esi
movl -0x3c(%rbp), %edx
callq 0x31e5
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x3386
movq -0x48(%rbp), %r12
leaq 0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x4e34
notq %rax
andq %rax, %r12
leaq -0x38(%rbp), %rdi
movq %r12, %rsi
callq 0x3386
movq -0x48(%rbp), %rax
cmpq $0x0, -0x38(%rbp)
setne %cl
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x778c
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
movq -0x38(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x38(%rbp)
cmpq $0x1, %rax
jb 0x7787
bsfq %rax, %rsi
movl %ebx, %edi
callq 0x6a8e
movl %eax, -0x40(%rbp)
movq 0x8(%r15), %rsi
cmpq 0x10(%r15), %rsi
je 0x777b
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%r15)
jmp 0x7787
movq %r15, %rdi
leaq -0x40(%rbp), %rdx
callq 0x83a8
decl %r12d
jne 0x7742
movq -0x50(%rbp), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq -0x58(%rbp), %rdx
andq %rdx, %r13
incl %r14d
cmpl %eax, %r14d
jne 0x7679
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq