// File: 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
subq $0x80, %rsp
movq %rdi, %rax
leaq 0x10(%rbp), %rdi
movq %rdi, -0x78(%rbp)
movq %rsi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
xorl %edx, %edx
movl %edx, %esi
callq 0x76f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x4bc0
movq -0x78(%rbp), %rdi
movl $0x1, %esi
callq 0x7840
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rcx
movq %rcx, %rax
shrq %rax
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rax
subq %rax, %rcx
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
movq %rcx, %rax
andq %rdx, %rax
shrq $0x2, %rcx
andq %rdx, %rcx
addq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
addq %rcx, %rax
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rax
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rax
shrq $0x38, %rax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xc961
leaq -0x18(%rbp), %rdi
callq 0xc970
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
jne 0xc7e4
jmp 0xc953
movl -0x34(%rbp), %edi
xorl %edx, %edx
movl %edx, %esi
callq 0x46e0
movq %rax, %rsi
andq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x4bc0
movl -0x34(%rbp), %edi
xorl %edx, %edx
movl %edx, %esi
callq 0x46e0
movq %rax, %rsi
movq -0x78(%rbp), %rax
andq 0x60(%rax), %rsi
leaq -0x48(%rbp), %rdi
callq 0x4bc0
cmpq $0x0, -0x48(%rbp)
je 0xc853
leaq -0x48(%rbp), %rdi
callq 0xc970
movl %eax, -0x4c(%rbp)
movl -0x34(%rbp), %edi
movl -0x4c(%rbp), %esi
movl $0x2, %edx
callq 0xab00
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0xcbd0
movq -0x40(%rbp), %rsi
andq 0x2302(%rip), %rsi      # 0xeb60
leaq -0x58(%rbp), %rdi
callq 0x4bc0
cmpq $0x0, -0x58(%rbp)
je 0xc888
movl -0x34(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
xorl %edx, %edx
callq 0xc9c0
movq 0x22d1(%rip), %rax      # 0xeb60
xorq $-0x1, %rax
andq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xc951
movq -0x40(%rbp), %rcx
movq %rcx, %rax
shrq %rax
movabsq $0x5555555555555555, %rdx # imm = 0x5555555555555555
andq %rdx, %rax
subq %rax, %rcx
movabsq $0x3333333333333333, %rdx # imm = 0x3333333333333333
movq %rcx, %rax
andq %rdx, %rax
shrq $0x2, %rcx
andq %rdx, %rcx
addq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
addq %rcx, %rax
movabsq $0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F
andq %rcx, %rax
movabsq $0x101010101010101, %rcx # imm = 0x101010101010101
imulq %rcx, %rax
shrq $0x38, %rax
movl %eax, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x64(%rbp), %eax
jge 0xc94f
leaq -0x40(%rbp), %rdi
callq 0xc970
movl %eax, -0x6c(%rbp)
cmpl $-0x1, -0x6c(%rbp)
jne 0xc929
jmp 0xc944
movl -0x34(%rbp), %edi
movl -0x6c(%rbp), %esi
callq 0xaab0
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xcbd0
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0xc90d
jmp 0xc951
jmp 0xc953
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xc7c1
addq $0x80, %rsp
popq %rbp
retq
nopw (%rax,%rax)