// File: /workspace/github_cpp_makefile_star2_O2/niekh1234[P]nnchesslib/movegen.cpp
// Function: nnchesslib::genWhiteSinglePawnMoves(nnchesslib::ChessBoard, std::vector<unsigned int, std::allocator<unsigned int>>&, nnchesslib::BitBoard)
void nnchesslib::genWhiteSinglePawnMoves(ChessBoard board, MoveList& moveList, BitBoard blockers)
{
    BitBoard pawns = board.getBoard(WHITE, 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_8];
    // 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, WHITE, singlePawnBoard);
    }

    pawnsMoved.board &= ~rank_bb[RANK_8];

    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, %r15
movq %rdi, %rbx
leaq 0x10(%rbp), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x4388
shlq $0x8, %rax
notq %r15
andq %rax, %r15
leaq -0x30(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x3036
movzbl 0x7(%r12), %esi
shlq $0x38, %rsi
leaq -0x40(%rbp), %r15
movq %r15, %rdi
callq 0x3036
movq (%r15), %r13
movq %r13, %rax
shrq %rax
movabsq $0x5555555555555555, %rcx # imm = 0x5555555555555555
andq %rax, %rcx
movq %r13, %rax
subq %rcx, %rax
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
movq %rax, %rcx
andq %rdx, %rcx
shrq $0x2, %rax
andq %rdx, %rax
addq %rcx, %rax
movq %rax, %r14
shrq $0x4, %r14
addq %rax, %r14
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %r14
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %r14
shrq $0x38, %r14
leaq -0x38(%rbp), %r15
subl $0x1, %r14d
jb 0x61e2
bsfq %r13, %r12
movq %r13, %rax
subq $0x1, %rax
pushq $-0x1
popq %rcx
cmovbl %ecx, %r12d
andq %rax, %r13
movq %r15, %rdi
callq 0x302c
movq %r15, %rdi
movl %r12d, %esi
pushq $0x1
popq %rdx
callq 0x3062
addl $-0x8, %r12d
movq -0x38(%rbp), %rcx
movl %r12d, %edi
movq %rbx, %rsi
callq 0x7092
jmp 0x619c
movq -0x30(%rbp), %rax
movabsq $0xffffffffffffff, %rcx # imm = 0xFFFFFFFFFFFFFF
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
shrq %rax
movabsq $0x55555555555555, %rdx # imm = 0x55555555555555
andq %rax, %rdx
subq %rdx, %rcx
movq %rcx, %rax
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
andq %rdx, %rax
shrq $0x2, %rcx
andq %rdx, %rcx
addq %rax, %rcx
movq %rcx, %r15
shrq $0x4, %r15
addq %rcx, %r15
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %r15
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %r15
shrq $0x38, %r15
leaq -0x38(%rbp), %r14
subl $0x1, %r15d
jb 0x6288
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
cmpq $0x1, %rax
jb 0x6251
bsfq %rax, %rsi
leal -0x8(%rsi), %edi
callq 0x5c0a
movl %eax, -0x38(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x71b0
jmp 0x6251
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq