// File: /workspace/github_cpp_makefile_star2_O2/jswaff[P]prophet3/src/movegen.cpp
// Function: gen_pawn_moves(unsigned long long*, position*, bool, bool)
move* gen_pawn_moves(move* m,position* p,bool caps,bool noncaps) {

    uint64 all_pieces = p->white_pieces | p->black_pieces;
    uint64 pmap;

    // captures
    if (p->player==WHITE) {
        if (caps) {
            uint64 targets = p->black_pieces;
            if (p->ep_sq != NO_SQUARE) targets |= bb_squares[p->ep_sq];

            // attacks west
            pmap = ((p->white_pawns & ~bb_files[FILE_A]) >> 9) & targets;
            while (pmap) {
                square_t sq = (square_t)get_msb(pmap);
                piece_t captured = sq==p->ep_sq ? PAWN : (piece_t)p->piece[sq];
                m = add_pawn_move(m,southeast(sq),sq,captured,sq==p->ep_sq);
                pmap ^= bb_squares[sq];
            }

            // attacks east
            pmap = ((p->white_pawns & ~bb_files[FILE_H]) >> 7) & targets;
            while (pmap) {
                square_t sq = (square_t)get_msb(pmap);
                piece_t captured = sq==p->ep_sq ? PAWN : (piece_t)p->piece[sq];
                m = add_pawn_move(m,southwest(sq),sq,captured,sq==p->ep_sq);
                pmap ^= bb_squares[sq];
            }

            // push promotions
            pmap = ((p->white_pawns & bb_ranks[RANK_7]) >> 8) & ~all_pieces;
            while (pmap) {
                square_t sq = (square_t)get_msb(pmap);
                m = add_pawn_move(m,south(sq),sq,NO_PIECE,false);
                pmap ^= bb_squares[sq];
            }
        }

        // pawn pushes less promotions
        if (noncaps) {
            pmap = ((p->white_pawns & ~bb_ranks[RANK_7]) >> 8) & ~all_pieces;
            while (pmap) {
                square_t sq = (square_t)get_msb(pmap);
                m = add_pawn_move(m,south(sq),sq,NO_PIECE,false);
                if (get_rank(sq)==RANK_3 && p->piece[north(sq)]==NO_PIECE) {
                    m = add_pawn_move(m,south(sq),north(sq),NO_PIECE,false);
                }
                pmap ^= bb_squares[sq];
            }
        }
    } else {
        if (caps) {
            uint64 targets = p->white_pieces;
            if (p->ep_sq != NO_SQUARE) targets |= bb_squares[p->ep_sq];

            // attacks west
            pmap = ((p->black_pawns & ~bb_files[FILE_A]) << 7) & targets;
            while (pmap) {
                square_t sq = (square_t)get_lsb(pmap);
                piece_t captured = sq==p->ep_sq ? PAWN : (piece_t)p->piece[sq];
                m = add_pawn_move(m,northeast(sq),sq,captured,sq==p->ep_sq);
                pmap ^= bb_squares[sq];
            }

            // attacks west
            pmap = ((p->black_pawns & ~bb_files[FILE_H]) << 9) & targets;
            while (pmap) {
                square_t sq = (square_t)get_lsb(pmap);
                piece_t captured = sq==p->ep_sq ? PAWN : (piece_t)p->piece[sq];
                m = add_pawn_move(m,northwest(sq),sq,captured,sq==p->ep_sq);
                pmap ^= bb_squares[sq];
            }

            // push promotions
            pmap = ((p->black_pawns & bb_ranks[RANK_2]) << 8) & ~all_pieces;
            while (pmap) {
                square_t sq = (square_t)get_lsb(pmap);
                m = add_pawn_move(m,north(sq),sq,NO_PIECE,false);
                pmap ^= bb_squares[sq];
            }
        }

        // pawn pushes less promotions
        if (noncaps) {
            pmap = ((p->black_pawns & ~bb_ranks[RANK_2]) << 8) & ~all_pieces;
            while (pmap) {
                square_t sq = (square_t)get_lsb(pmap);
                m = add_pawn_move(m,north(sq),sq,NO_PIECE,false);
                if (get_rank(sq)==RANK_6 && p->piece[south(sq)]==NO_PIECE) {
                    m = add_pawn_move(m,north(sq),south(sq),NO_PIECE,false);
                }
                pmap ^= bb_squares[sq];
            }
        }
    }

    return m;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq 0x1a8(%rsi), %rsi
movq 0x1b0(%rbx), %rdi
movq %rdi, %r15
orq %rsi, %r15
cmpl $0x1, 0x138(%rbx)
jne 0x75b1
testb %dl, %dl
je 0x77f1
movl %ecx, 0xc(%rsp)
movl 0x13c(%rbx), %eax
cmpq $0x40, %rax
je 0x7539
leaq 0xf5f7b(%rip), %rcx     # 0xfd4b0
orq (%rcx,%rax,8), %rdi
leaq 0xf5ef0(%rip), %rax     # 0xfd430
movq (%rax), %r12
notq %r12
andq 0x158(%rbx), %r12
shrq $0x9, %r12
movq %rdi, 0x10(%rsp)
andq %rdi, %r12
testq %r12, %r12
je 0x764f
movq %r12, %rdi
callq 0x3a44
movl %eax, %r13d
pushq $0x1
popq %rbp
cmpl 0x13c(%rbx), %eax
je 0x757c
movl (%rbx,%r13,4), %ebp
movl %r13d, %edi
callq 0xc2ff
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r13d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x7edd
movq %rax, %r14
leaq 0xf5f05(%rip), %rax     # 0xfd4b0
xorq (%rax,%r13,8), %r12
jmp 0x7559
notq %r15
testb %dl, %dl
je 0x789b
movl %ecx, 0xc(%rsp)
movl 0x13c(%rbx), %eax
cmpq $0x40, %rax
je 0x75d7
leaq 0xf5edd(%rip), %rcx     # 0xfd4b0
orq (%rcx,%rax,8), %rsi
leaq 0xf5e52(%rip), %rax     # 0xfd430
movq (%rax), %r12
notq %r12
andq 0x160(%rbx), %r12
shlq $0x7, %r12
movq %rsi, 0x10(%rsp)
andq %rsi, %r12
testq %r12, %r12
je 0x76c5
movq %r12, %rdi
callq 0x397d
movl %eax, %r13d
pushq $0x1
popq %rbp
cmpl 0x13c(%rbx), %eax
je 0x761a
movl (%rbx,%r13,4), %ebp
movl %r13d, %edi
callq 0xc2a9
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r13d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x7edd
movq %rax, %r14
leaq 0xf5e67(%rip), %rax     # 0xfd4b0
xorq (%rax,%r13,8), %r12
jmp 0x75f7
leaq 0xf5dda(%rip), %rax     # 0xfd430
movq 0x38(%rax), %r12
notq %r12
andq 0x158(%rbx), %r12
shrq $0x7, %r12
andq 0x10(%rsp), %r12
testq %r12, %r12
je 0x773b
movq %r12, %rdi
callq 0x3a44
movl %eax, %r13d
pushq $0x1
popq %rbp
cmpl 0x13c(%rbx), %eax
je 0x7690
movl (%rbx,%r13,4), %ebp
movl %r13d, %edi
callq 0xc356
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r13d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x7edd
movq %rax, %r14
leaq 0xf5df1(%rip), %rax     # 0xfd4b0
xorq (%rax,%r13,8), %r12
jmp 0x766d
leaq 0xf5d64(%rip), %rax     # 0xfd430
movq 0x38(%rax), %r12
notq %r12
andq 0x160(%rbx), %r12
shlq $0x9, %r12
andq 0x10(%rsp), %r12
testq %r12, %r12
je 0x7797
movq %r12, %rdi
callq 0x397d
movl %eax, %r13d
pushq $0x1
popq %rbp
cmpl 0x13c(%rbx), %eax
je 0x7706
movl (%rbx,%r13,4), %ebp
movl %r13d, %edi
callq 0xc3ac
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r13d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x7edd
movq %rax, %r14
leaq 0xf5d7b(%rip), %rax     # 0xfd4b0
xorq (%rax,%r13,8), %r12
jmp 0x76e3
leaq 0xf5d2e(%rip), %rax     # 0xfd470
movq 0x8(%rax), %rax
andq 0x158(%rbx), %rax
shrq $0x8, %rax
movq %r15, %r12
notq %r12
andq %rax, %r12
testq %r12, %r12
je 0x77ed
movq %r12, %rdi
callq 0x3a44
movl %eax, %ebp
movl %eax, %edi
callq 0xc1e0
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7edd
movq %rax, %r14
movl %ebp, %eax
leaq 0xf5d1f(%rip), %rcx     # 0xfd4b0
xorq (%rcx,%rax,8), %r12
jmp 0x775a
leaq 0xf5cd2(%rip), %rax     # 0xfd470
movq 0x30(%rax), %r12
andq 0x160(%rbx), %r12
shlq $0x8, %r12
andq %r15, %r12
testq %r12, %r12
je 0x7897
movq %r12, %rdi
callq 0x397d
movl %eax, %ebp
movl %eax, %edi
callq 0xc19d
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7edd
movq %rax, %r14
movl %ebp, %eax
leaq 0xf5cc9(%rip), %rcx     # 0xfd4b0
xorq (%rcx,%rax,8), %r12
jmp 0x77b0
movl 0xc(%rsp), %ecx
testb %cl, %cl
je 0x793a
leaq 0xf5c70(%rip), %rax     # 0xfd470
movq 0x8(%rax), %rax
notq %rax
andq 0x158(%rbx), %rax
shrq $0x8, %rax
notq %r15
andq %rax, %r15
leaq 0xf5c91(%rip), %r13     # 0xfd4b0
testq %r15, %r15
je 0x793a
movq %r15, %rdi
callq 0x3a44
movl %eax, %ebp
movl %eax, %edi
callq 0xc1e0
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7edd
movq %rax, %r14
movl %ebp, %edi
callq 0xc001
cmpl $0x5, %eax
jne 0x788e
movl %ebp, %edi
callq 0xc19d
movl %eax, %eax
cmpl $0x0, (%rbx,%rax,4)
jne 0x788e
movl %ebp, %edi
callq 0xc1e0
movl %eax, %r12d
movl %ebp, %edi
callq 0xc19d
movq %r14, %rdi
movl %r12d, %esi
movl %eax, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7edd
movq %rax, %r14
movl %ebp, %eax
xorq (%r13,%rax,8), %r15
jmp 0x781f
movl 0xc(%rsp), %ecx
testb %cl, %cl
je 0x793a
leaq 0xf5bc6(%rip), %rax     # 0xfd470
movq 0x30(%rax), %r12
notq %r12
andq 0x160(%rbx), %r12
shlq $0x8, %r12
andq %r15, %r12
leaq 0xf5bea(%rip), %r13     # 0xfd4b0
testq %r12, %r12
je 0x793a
movq %r12, %rdi
callq 0x397d
movl %eax, %ebp
movl %eax, %edi
callq 0xc19d
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7edd
movq %rax, %r14
movl %ebp, %edi
callq 0xc001
cmpl $0x2, %eax
jne 0x7931
movl %ebp, %edi
callq 0xc1e0
movl %eax, %eax
cmpl $0x0, (%rbx,%rax,4)
jne 0x7931
movl %ebp, %edi
callq 0xc19d
movl %eax, %r15d
movl %ebp, %edi
callq 0xc1e0
movq %r14, %rdi
movl %r15d, %esi
movl %eax, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x7edd
movq %rax, %r14
movl %ebp, %eax
xorq (%r13,%rax,8), %r12
jmp 0x78c6
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq