// File: /workspace/github_cpp_makefile_star2_O3/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)
movl %ecx, 0xc(%rsp)
jne 0x7c45
testb %dl, %dl
je 0x7b8e
movl 0x13c(%rbx), %eax
cmpq $0x40, %rax
je 0x7a27
leaq 0xf2a8d(%rip), %rcx     # 0xfa4b0
orq (%rcx,%rax,8), %rdi
movq 0x158(%rbx), %rax
leaq 0xf29fb(%rip), %rcx     # 0xfa430
movq (%rcx), %r12
notq %r12
andq %rax, %r12
shrq $0x9, %r12
andq %rdi, %r12
je 0x7abc
movq %rdi, 0x10(%rsp)
movq %r12, %rdi
callq 0x3c31
movl %eax, %r13d
movl $0x1, %ebp
cmpl 0x13c(%rbx), %eax
je 0x7a68
movl (%rbx,%r13,4), %ebp
movl %r13d, %edi
callq 0xcd33
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r13d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x84dc
movq %rax, %r14
leaq 0xf2a19(%rip), %rax     # 0xfa4b0
movq (%rax,%r13,8), %rax
movq %rax, %rcx
xorq %r12, %rcx
cmpq %r12, %rax
movq %rcx, %r12
jne 0x7a4c
movq 0x158(%rbx), %rax
movq 0x10(%rsp), %rdi
leaq 0xf2974(%rip), %rcx     # 0xfa430
movq 0x38(%rcx), %r12
notq %r12
andq %rax, %r12
shrq $0x7, %r12
andq %rdi, %r12
je 0x7b33
movq %r12, %rdi
callq 0x3c31
movl %eax, %r13d
movl $0x1, %ebp
cmpl 0x13c(%rbx), %eax
je 0x7aeb
movl (%rbx,%r13,4), %ebp
movl %r13d, %edi
callq 0xcd7a
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r13d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r13d, %edx
movl %ebp, %ecx
callq 0x84dc
movq %rax, %r14
leaq 0xf2996(%rip), %rax     # 0xfa4b0
movq (%rax,%r13,8), %rax
movq %rax, %rcx
xorq %r12, %rcx
cmpq %r12, %rax
movq %rcx, %r12
jne 0x7acf
movq 0x158(%rbx), %rax
leaq 0xf2936(%rip), %rcx     # 0xfa470
andq 0x8(%rcx), %rax
shrq $0x8, %rax
movq %r15, %r12
notq %r12
andq %rax, %r12
je 0x7b8e
leaq 0xf295c(%rip), %r13     # 0xfa4b0
movq %r12, %rdi
callq 0x3c31
movl %eax, %ebp
movl %eax, %edi
callq 0xcc3e
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x84dc
movq %rax, %r14
movl %ebp, %eax
movq (%r13,%rax,8), %rax
movq %rax, %rcx
xorq %r12, %rcx
cmpq %r12, %rax
movq %rcx, %r12
jne 0x7b54
cmpb $0x0, 0xc(%rsp)
je 0x7e77
leaq 0xf28d0(%rip), %rax     # 0xfa470
movq 0x8(%rax), %rax
notq %rax
andq 0x158(%rbx), %rax
shrq $0x8, %rax
notq %r15
andq %rax, %r15
je 0x7e77
leaq 0xf28eb(%rip), %r13     # 0xfa4b0
movq %r15, %rdi
callq 0x3c31
movl %eax, %ebp
movl %eax, %edi
callq 0xcc3e
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x84dc
movq %rax, %r14
movl %ebp, %edi
callq 0xca69
cmpl $0x5, %eax
jne 0x7c2b
movl %ebp, %edi
callq 0xcc05
movl %eax, %eax
cmpl $0x0, (%rbx,%rax,4)
jne 0x7c2b
movl %ebp, %edi
callq 0xcc3e
movl %eax, %r12d
movl %ebp, %edi
callq 0xcc05
movq %r14, %rdi
movl %r12d, %esi
movl %eax, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x84dc
movq %rax, %r14
movl %ebp, %eax
movq (%r13,%rax,8), %rax
movq %rax, %rcx
xorq %r15, %rcx
cmpq %r15, %rax
movq %rcx, %r15
jne 0x7bc5
jmp 0x7e77
notq %r15
testb %dl, %dl
je 0x7dc8
movl 0x13c(%rbx), %eax
cmpq $0x40, %rax
je 0x7c67
leaq 0xf284d(%rip), %rcx     # 0xfa4b0
orq (%rcx,%rax,8), %rsi
movq 0x160(%rbx), %r12
leaq 0xf27bb(%rip), %rax     # 0xfa430
movq (%rax), %r13
notq %r13
andq %r12, %r13
shlq $0x7, %r13
andq %rsi, %r13
je 0x7cfc
movq %rsi, 0x10(%rsp)
movq %r13, %rdi
callq 0x3b67
movl %eax, %r12d
movl $0x1, %ebp
cmpl 0x13c(%rbx), %eax
je 0x7ca8
movl (%rbx,%r12,4), %ebp
movl %r12d, %edi
callq 0xccf0
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r12d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r12d, %edx
movl %ebp, %ecx
callq 0x84dc
movq %rax, %r14
leaq 0xf27d9(%rip), %rax     # 0xfa4b0
movq (%rax,%r12,8), %rax
movq %rax, %rcx
xorq %r13, %rcx
cmpq %r13, %rax
movq %rcx, %r13
jne 0x7c8c
movq 0x160(%rbx), %r12
movq 0x10(%rsp), %rsi
leaq 0xf2734(%rip), %rax     # 0xfa430
movq 0x38(%rax), %r13
notq %r13
andq %r12, %r13
shlq $0x9, %r13
andq %rsi, %r13
je 0x7d73
movq %r13, %rdi
callq 0x3b67
movl %eax, %r12d
movl $0x1, %ebp
cmpl 0x13c(%rbx), %eax
je 0x7d2b
movl (%rbx,%r12,4), %ebp
movl %r12d, %edi
callq 0xcdba
xorl %r8d, %r8d
cmpl 0x13c(%rbx), %r12d
sete %r8b
movq %r14, %rdi
movl %eax, %esi
movl %r12d, %edx
movl %ebp, %ecx
callq 0x84dc
movq %rax, %r14
leaq 0xf2756(%rip), %rax     # 0xfa4b0
movq (%rax,%r12,8), %rax
movq %rax, %rcx
xorq %r13, %rcx
cmpq %r13, %rax
movq %rcx, %r13
jne 0x7d0f
movq 0x160(%rbx), %r12
leaq 0xf26f6(%rip), %rax     # 0xfa470
andq 0x30(%rax), %r12
shlq $0x8, %r12
andq %r15, %r12
je 0x7dc8
leaq 0xf2722(%rip), %r13     # 0xfa4b0
movq %r12, %rdi
callq 0x3b67
movl %eax, %ebp
movl %eax, %edi
callq 0xcc05
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x84dc
movq %rax, %r14
movl %ebp, %eax
movq (%r13,%rax,8), %rax
movq %rax, %rcx
xorq %r12, %rcx
cmpq %r12, %rax
movq %rcx, %r12
jne 0x7d8e
cmpb $0x0, 0xc(%rsp)
je 0x7e77
leaq 0xf2696(%rip), %rax     # 0xfa470
movq 0x30(%rax), %r12
notq %r12
andq 0x160(%rbx), %r12
shlq $0x8, %r12
andq %r15, %r12
je 0x7e77
leaq 0xf26b4(%rip), %r13     # 0xfa4b0
movq %r12, %rdi
callq 0x3b67
movl %eax, %ebp
movl %eax, %edi
callq 0xcc05
movq %r14, %rdi
movl %eax, %esi
movl %ebp, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x84dc
movq %rax, %r14
movl %ebp, %edi
callq 0xca69
cmpl $0x2, %eax
jne 0x7e62
movl %ebp, %edi
callq 0xcc3e
movl %eax, %eax
cmpl $0x0, (%rbx,%rax,4)
jne 0x7e62
movl %ebp, %edi
callq 0xcc05
movl %eax, %r15d
movl %ebp, %edi
callq 0xcc3e
movq %r14, %rdi
movl %r15d, %esi
movl %eax, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x84dc
movq %rax, %r14
movl %ebp, %eax
movq (%r13,%rax,8), %rax
movq %rax, %rcx
xorq %r12, %rcx
cmpq %r12, %rax
movq %rcx, %r12
jne 0x7dfc
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq