// File: 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
subq $0x80, %rsp
movq %rdi, %rax
leaq 0x10(%rbp), %rdi
movq %rdi, -0x78(%rbp)
movq %rsi, -0x8(%rbp)
movq %rax, -0x10(%rbp)
movl $0x1, %esi
xorl %edx, %edx
callq 0x76f0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x18(%rbp), %rdi
callq 0x4bc0
movq -0x78(%rbp), %rdi
xorl %esi, %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 0xbd1a
leaq -0x18(%rbp), %rdi
callq 0xc970
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
jne 0xbb94
jmp 0xbd0c
movl -0x34(%rbp), %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x46e0
movq %rax, %rsi
andq -0x28(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x4bc0
movl -0x34(%rbp), %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x46e0
movq %rax, %rsi
movq -0x78(%rbp), %rax
andq 0x58(%rax), %rsi
leaq -0x48(%rbp), %rdi
callq 0x4bc0
cmpq $0x0, -0x48(%rbp)
je 0xbc09
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 0x2f84(%rip), %rsi      # 0xeb98
leaq -0x58(%rbp), %rdi
callq 0x4bc0
cmpq $0x0, -0x58(%rbp)
je 0xbc41
movl -0x34(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rcx
movl $0x1, %edx
callq 0xc9c0
movq 0x2f50(%rip), %rax      # 0xeb98
xorq $-0x1, %rax
andq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0xbd0a
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 0xbd08
leaq -0x40(%rbp), %rdi
callq 0xc970
movl %eax, -0x6c(%rbp)
cmpl $-0x1, -0x6c(%rbp)
jne 0xbce2
jmp 0xbcfd
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 0xbcc6
jmp 0xbd0a
jmp 0xbd0c
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xbb71
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)