// File: /workspace/github_cpp_makefile_star2_O2/niekh1234[P]nnchesslib/movegen.cpp
// Function: nnchesslib::genBlackSinglePawnMoves(nnchesslib::ChessBoard, std::vector<unsigned int, std::allocator<unsigned int>>&, nnchesslib::BitBoard)
void nnchesslib::genBlackSinglePawnMoves(ChessBoard board, MoveList& moveList, BitBoard blockers)
{
    BitBoard pawns = board.getBoard(BLACK, PAWN);
    // making a move and checking if pawns have been blocked
    BitBoard pawnsMoved = (pawns.board >> 8) & ~blockers.board;

    BitBoard promotedPawns = pawnsMoved.board & rank_bb[RANK_1];
    // make a copy because we still want to use promotedpawns bitboard after lsb has been popped.
    BitBoard promotedPawnsCopy = promotedPawns;
    int promotedPawnCount = __builtin_popcountll(promotedPawns.board);

    for(int _ =  0; _ < promotedPawnCount; _++)
    {
        int index = popLsb(promotedPawnsCopy.board);
        // genPromotions function only works for a single index in the bitboard.
        BitBoard singlePawnBoard;
        singlePawnBoard.set(index, true);

        genPromotions(index + 8, moveList, BLACK, singlePawnBoard);
    }

    pawnsMoved.board &= ~rank_bb[RANK_1];

    int validPawnCount = __builtin_popcountll(pawnsMoved.board);

    for(int _ = 0; _ < validPawnCount; _++)
    {
        // finding current index of pawn.
        int index = popLsb(pawnsMoved.board);

        if (index == -1)
            continue;
        // adding to pseudo legal movelist. 
        Move move = createMove(index + 8, index);
        moveList.push_back(move);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, -0x40(%rbp)
leaq 0x10(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4388
shrq $0x8, %rax
notq %r14
andq %rax, %r14
leaq -0x30(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3036
movzbl (%r15), %esi
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x3036
movq (%r14), %r13
movq %r13, %rax
shrq %rax
movabsq $0x5555555555555555, %rcx # imm = 0x5555555555555555
andq %rax, %rcx
movq %r13, %rax
subq %rcx, %rax
movabsq $0x3333333333333333, %rcx # imm = 0x3333333333333333
movq %rax, %rdx
andq %rcx, %rdx
shrq $0x2, %rax
andq %rcx, %rax
addq %rdx, %rax
movq %rax, %rbx
shrq $0x4, %rbx
addq %rax, %rbx
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %rbx
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %rbx
shrq $0x38, %rbx
leaq -0x38(%rbp), %r14
pushq $0x1
popq %r15
subl $0x1, %ebx
jb 0x6c82
bsfq %r13, %r12
movq %r13, %rax
subq $0x1, %rax
pushq $-0x1
popq %rcx
cmovbl %ecx, %r12d
andq %rax, %r13
movq %r14, %rdi
callq 0x302c
movq %r14, %rdi
movl %r12d, %esi
movl %r15d, %edx
callq 0x3062
addl $0x8, %r12d
movq -0x38(%rbp), %rcx
movl %r12d, %edi
movq -0x40(%rbp), %rsi
callq 0x7092
jmp 0x6c3c
movq -0x30(%rbp), %rax
movq %rax, %rcx
andq $-0x100, %rcx
movq %rcx, -0x30(%rbp)
shrq %rax
movabsq $0x5555555555555500, %rdx # imm = 0x5555555555555500
andq %rax, %rdx
subq %rdx, %rcx
movabsq $0x3333333333333300, %rax # imm = 0x3333333333333300
movq %rcx, %rdx
andq %rax, %rdx
shrq $0x2, %rcx
andq %rax, %rcx
addq %rdx, %rcx
movq %rcx, %rbx
shrq $0x4, %rbx
addq %rcx, %rbx
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %rbx
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %rbx
shrq $0x38, %rbx
leaq -0x38(%rbp), %r14
subl $0x1, %ebx
jb 0x6d25
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
cmpq $0x1, %rax
jb 0x6cee
bsfq %rax, %rsi
leal 0x8(%rsi), %edi
callq 0x5c0a
movl %eax, -0x38(%rbp)
movq -0x40(%rbp), %rdi
movq %r14, %rsi
callq 0x71b0
jmp 0x6cee
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq