// File: /workspace/github_cpp_makefile_star2_O2/niekh1234[P]nnchesslib/movegen.cpp
// Function: nnchesslib::genWhitePawnCaptures(nnchesslib::ChessBoard, std::vector<unsigned int, std::allocator<unsigned int>>&, nnchesslib::BitBoard)
void nnchesslib::genWhitePawnCaptures(ChessBoard board, MoveList& moveList, BitBoard blockers)
{
    // getting board for white pawns.
    BitBoard pawns = board.getBoard(WHITE, PAWN).board;
    BitBoard black = board.getBoard(BLACK);
    // counting the amount of white pawns.
    int pawnCount = __builtin_popcountll(pawns.board);

    for(int _ = 0; _ < pawnCount; _++)
    {
        // getting the index of a specific pawn and removing it from the board.
        int index = popLsb(pawns.board);

        if (index == -1)
            continue;
        
        // getting attacks and comparing to black pieces.
        BitBoard pawnAttacks = Attacks::getNonSlidingAttacks(index, WHITE, PAWN) & black.board;

        // getting en passant move by looking at if a white pawn is attacking a predefined en passant square ('under' the pawn, actually it is above the pawn because it is a black pawn)
        BitBoard enPassant = Attacks::getNonSlidingAttacks(index, WHITE, PAWN) & board.boardinfo.whiteEnPassantTarget.board;
        if(enPassant.board)
        {
            int epIndex = popLsb(enPassant.board);

            Move enPassantMove = createMove(index, epIndex, ENPASSANT);
            moveList.push_back(enPassantMove);
        }

        BitBoard promotedPawns = pawnAttacks.board & rank_bb[RANK_8];
        // if there is an attack generate promotion moves.
        if(promotedPawns.board)
            genPromotions(index, moveList, WHITE, promotedPawns);

        // removing attacks on the eighth rank to prevent non promotions moves from being added.
        pawnAttacks.board &= ~rank_bb[RANK_8];
        if(pawnAttacks.board){
            // getting the amount of pieces a pawn is attacking.
            int attackCount = __builtin_popcountll(pawnAttacks.board);
            
            for(int num = 0; num < attackCount; num++)
            {
                // getting the index of the attacked pawn/piece from black.
                int attackIndex = popLsb(pawnAttacks.board);

                if (attackIndex == -1)
                    continue;
                // generating a move with our pawn index and the index of blacks attacked pawn/piece.
                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, %rbx
leaq 0x10(%rbp), %r15
pushq $0x1
popq %rsi
xorl %r12d, %r12d
movq %r15, %rdi
xorl %edx, %edx
callq 0x4388
leaq -0x50(%rbp), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x3036
movq %r15, %rdi
xorl %esi, %esi
callq 0x440e
movq %rax, -0x58(%rbp)
movq (%r14), %rax
movq %rax, %rcx
shrq %rcx
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rcx, %rdx
subq %rdx, %rax
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
movq %rax, %rcx
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
movq %rcx, -0x60(%rbp)
leaq -0x38(%rbp), %r14
cmpl -0x60(%rbp), %r12d
je 0x65cd
movq -0x50(%rbp), %rax
bsfq %rax, %r13
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x50(%rbp)
movl %r13d, %r15d
cmpq $0x1, %rax
pushq $-0x1
popq %rax
cmovbl %eax, %r15d
jb 0x65c5
movl %r12d, -0x34(%rbp)
movl %r13d, %edi
pushq $0x1
popq %r12
movl %r12d, %esi
xorl %edx, %edx
callq 0x2e6e
andq -0x58(%rbp), %rax
leaq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x3036
movl %r13d, %edi
movl %r12d, %esi
xorl %edx, %edx
callq 0x2e6e
leaq 0x10(%rbp), %rcx
andq 0x58(%rcx), %rax
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x3036
movq -0x48(%rbp), %rax
testq %rax, %rax
leaq -0x40(%rbp), %r12
je 0x64f2
bsfq %rax, %rsi
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x48(%rbp)
movl %r13d, %edi
pushq $0x2
popq %rdx
callq 0x5c2c
movl %eax, -0x40(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x71b0
movzbl -0x29(%rbp), %esi
shlq $0x38, %rsi
movq %r12, %rdi
callq 0x3036
movq -0x40(%rbp), %rcx
testq %rcx, %rcx
je 0x6516
movl %r13d, %edi
movq %rbx, %rsi
callq 0x7092
movq -0x30(%rbp), %rax
movabsq $0xffffffffffffff, %rcx # imm = 0xFFFFFFFFFFFFFF
andq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %r12d
je 0x65c5
movq %rax, %rcx
shrq %rcx
movabsq $0x55555555555555, %rdx # imm = 0x55555555555555
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, %r13
shrq $0x4, %r13
addq %rax, %r13
movabsq $0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F
andq %rax, %r13
movabsq $0x101010101010101, %rax # imm = 0x101010101010101
imulq %rax, %r13
shrq $0x38, %r13
subl $0x1, %r13d
jb 0x65c5
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
cmpq $0x1, %rax
jb 0x658e
bsfq %rax, %rsi
movl %r15d, %edi
callq 0x5c0a
movl %eax, -0x38(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x71b0
jmp 0x658e
incl %r12d
jmp 0x6446
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq