// File: /workspace/github_cpp_makefile_star2_O3/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 0x4d9e
shrq $0x8, %rax
notq %r14
andq %rax, %r14
leaq -0x30(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x3386
movzbl (%r15), %esi
leaq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x3386
movq (%r14), %r13
testq %r13, %r13
je 0x7d9f
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
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 0x3378
movq %r14, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x33b2
addl $0x8, %r15d
movq -0x38(%rbp), %rcx
movl %r15d, %edi
movq %rbx, %rsi
callq 0x8205
decl %r12d
jne 0x7d58
movq $-0x100, %rax
andq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
je 0x7e5b
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, %r15
shrq $0x4, %r15
addq %rax, %r15
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %r15
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %r15
shrq $0x38, %r15
leaq -0x38(%rbp), %r14
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
cmpq $0x1, %rax
jb 0x7e56
bsfq %rax, %rsi
leal 0x8(%rsi), %edi
callq 0x6a8e
movl %eax, -0x38(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x7e4b
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x7e56
movq %rbx, %rdi
movq %r14, %rdx
callq 0x83a8
decl %r15d
jne 0x7e11
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq