// File: /workspace/github_cpp_makefile_star2_O1/niekh1234[P]nnchesslib/movegen.cpp
// Function: nnchesslib::genBlackPawnCaptures(nnchesslib::ChessBoard, std::vector<unsigned int, std::allocator<unsigned int>>&, nnchesslib::BitBoard)
void nnchesslib::genBlackPawnCaptures(ChessBoard board, MoveList& moveList, BitBoard blockers)
{
    // getting board for black pawns.
    BitBoard pawns = board.getBoard(BLACK, PAWN).board;
    BitBoard white = board.getBoard(WHITE);
    // 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 white pieces.
        BitBoard pawnAttacks = Attacks::getNonSlidingAttacks(index, BLACK, PAWN) & white.board;
        // checking whether there is an attack on the eighth rank.

        // getting en passant move by looking at if a white pawn is attacking a predefined en passant square ('above' the pawn, actually it is above the pawn because it is a white pawn)
        BitBoard enPassant = Attacks::getNonSlidingAttacks(index, BLACK, PAWN) & board.boardinfo.blackEnPassantTarget.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_1];
        // if there is an attack generate promotion moves.
        if(promotedPawns.board)
            genPromotions(index, moveList, BLACK, promotedPawns);

        // removing attacks on the eighth rank to prevent non promotions moves from being added.
        pawnAttacks.board &= ~rank_bb[RANK_1];
        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
xorl %r14d, %r14d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4d8a
leaq -0x50(%rbp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0x334a
movq %r15, %rdi
movl $0x1, %esi
callq 0x4e20
movq %rax, -0x58(%rbp)
movq (%r12), %rax
testq %rax, %rax
je 0x8286
movq %rax, %rcx
shrq %rcx
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rcx
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, %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
cmpl $0x2, %r15d
movl $0x1, %eax
cmovbl %eax, %r15d
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq -0x50(%rbp), %rax
bsfq %rax, %r13
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x50(%rbp)
cmpq $0x1, %rax
cmovbl %edx, %r13d
jb 0x827a
movl %r13d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x31a9
andq -0x58(%rbp), %rax
leaq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x334a
movl %r13d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x31a9
leaq 0x10(%rbp), %rcx
andq 0x60(%rcx), %rax
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x334a
movq -0x48(%rbp), %rax
testq %rax, %rax
je 0x818b
bsfq %rax, %rsi
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x48(%rbp)
movl %r13d, %edi
movl $0x2, %edx
callq 0x6aac
movl %eax, -0x38(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x817f
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x818b
movq %rbx, %rdi
leaq -0x38(%rbp), %rdx
callq 0x845c
movzbl -0x30(%rbp), %esi
leaq -0x38(%rbp), %rdi
callq 0x334a
movq -0x38(%rbp), %rcx
testq %rcx, %rcx
je 0x81ac
movl %r13d, %edi
movq %rbx, %rsi
callq 0x82b7
movq -0x30(%rbp), %rax
movq $-0x100, %rcx
andq %rcx, %rax
movq %rax, -0x30(%rbp)
je 0x8275
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, %r12
shrq $0x4, %r12
addq %rax, %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
movq -0x30(%rbp), %rax
leaq -0x1(%rax), %rcx
andq %rax, %rcx
movq %rcx, -0x30(%rbp)
cmpq $0x1, %rax
jb 0x8270
bsfq %rax, %rsi
movl %r13d, %edi
callq 0x6a8a
movl %eax, -0x3c(%rbp)
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x8264
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x8(%rbx)
jmp 0x8270
movq %rbx, %rdi
leaq -0x3c(%rbp), %rdx
callq 0x845c
decl %r12d
jne 0x822a
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
incl %r14d
cmpl %r15d, %r14d
jne 0x80e4
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq