// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x10(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4d8a
shrq $0x8, %rax
notq %r14
andq %rax, %r14
leaq -0x30(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x334a
movzbl (%r15), %esi
leaq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x334a
movq (%r14), %r13
testq %r13, %r13
je 0x7e1a
movq %r13, %rax
shrq %rax
movabsq $0x5555555555555555, %rcx # imm = 0x5555555555555555
andq %rcx, %rax
movq %r13, %rcx
subq %rax, %rcx
movq %rcx, %rax
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
andq %rdx, %rax
shrq $0x2, %rcx
andq %rdx, %rcx
addq %rax, %rcx
movq %rcx, %r12
shrq $0x4, %r12
addq %rcx, %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
leaq -0x38(%rbp), %r14
bsfq %r13, %r15
movq %r13, %rax
subq $0x1, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmovbl %ecx, %r15d
andq %rax, %r13
movq %r14, %rdi
callq 0x333c
movq %r14, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x3376
addl $0x8, %r15d
movq -0x38(%rbp), %rcx
movl %r15d, %edi
movq %rbx, %rsi
callq 0x82b7
decl %r12d
jne 0x7dd3
movq $-0x100, %rax
andq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
je 0x7ee3
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, %rcx
shrq $0x4, %rcx
addq %rax, %rcx
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %rcx
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %rcx
shrq $0x38, %rcx
cmpl $0x2, %ecx
movl $0x1, %r15d
cmovael %ecx, %r15d
leaq -0x38(%rbp), %r14
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
cmpq $0x1, %rax
jb 0x7ede
bsfq %rax, %rsi
leal 0x8(%rsi), %edi
callq 0x6a8a
movl %eax, -0x38(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x7ed3
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x7ede
movq %rbx, %rdi
movq %r14, %rdx
callq 0x845c
decl %r15d
jne 0x7e99
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq