// File: /workspace/github_cpp_makefile_star2_O2/codeandkey[P]neocortex/src/position.cpp
// Function: neocortex::Position::pseudolegal_moves_quiescence(neocortex::Move*)
int Position::pseudolegal_moves_quiescence(Move* out) {
	if (check()) {
		return pseudolegal_moves_evasions(out);
	}

	int count = 0;

	bitboard ctm = board.get_color_occ(color_to_move);
	bitboard opp = board.get_color_occ(!color_to_move);
	
	int oppking = bb::getlsb(opp & board.get_piece_occ(piece::KING));

	bitboard bchecks = attacks::bishop(oppking, board.get_global_occ()) & ~ctm;
	bitboard rchecks = attacks::rook(oppking, board.get_global_occ()) & ~ctm;
	bitboard nchecks = attacks::knight(oppking) & ~ctm;
	bitboard pchecks = attacks::pawn(!color_to_move, oppking);

	bitboard ep_mask = 0;
	int ep_square = ply.back().en_passant_square;

	if (square::is_valid(ep_square)) {
		ep_mask = bb::mask(ep_square);
	}

	/* Pawn moves */
	bitboard pawns = ctm & board.get_piece_occ(piece::PAWN);
	bitboard promoting_rank = (color_to_move == piece::WHITE) ? RANK_7 : RANK_2;

	int adv_dir = (color_to_move == piece::WHITE) ? NORTH : SOUTH;
	int left_dir = (color_to_move == piece::WHITE) ? NORTHWEST : SOUTHWEST;
	int right_dir = (color_to_move == piece::WHITE) ? NORTHEAST : SOUTHEAST;

	bitboard promoting_pawns = pawns & promoting_rank;

	/* Promoting left captures */
	bitboard promoting_left_cap = bb::shift(promoting_pawns & ~FILE_A, left_dir) & opp;

	while (promoting_left_cap) {
		int dst = bb::poplsb(promoting_left_cap);
		out[count++] = Move(dst - left_dir, dst, piece::QUEEN);
		out[count++] = Move(dst - left_dir, dst, piece::KNIGHT);
	}

	/* Promoting right captures */
	bitboard promoting_right_cap = bb::shift(promoting_pawns & ~FILE_H, right_dir) & opp;

	while (promoting_right_cap) {
		int dst = bb::poplsb(promoting_right_cap);
		out[count++] = Move(dst - right_dir, dst, piece::QUEEN);
		out[count++] = Move(dst - right_dir, dst, piece::KNIGHT);
	}

	/* Promoting advances */
	bitboard promoting_advances = bb::shift(promoting_pawns, adv_dir) & ~board.get_global_occ();

	while (promoting_advances) {
		int dst = bb::poplsb(promoting_advances);
		out[count++] = Move(dst - adv_dir, dst, piece::QUEEN);
		out[count++] = Move(dst - adv_dir, dst, piece::KNIGHT);
	}

	/* Nonpromoting pawn moves */
	bitboard npm_pawns = pawns & ~promoting_pawns;

	/* Left captures */
	bitboard npm_left_cap = bb::shift(npm_pawns & ~FILE_A, left_dir) & (opp | ep_mask);

	while (npm_left_cap) {
		int dst = bb::poplsb(npm_left_cap);
		out[count++] = Move(dst - left_dir, dst);
	}

	/* Right captures */
	bitboard npm_right_cap = bb::shift(npm_pawns & ~FILE_H, right_dir) & (opp | ep_mask);

	while (npm_right_cap) {
		int dst = bb::poplsb(npm_right_cap);
		out[count++] = Move(dst - right_dir, dst);
	}

	/* Pawn checks */
	bitboard npm_checks = bb::shift(npm_pawns, adv_dir) & ~board.get_global_occ() & pchecks;

	while (npm_checks) {
		int dst = bb::poplsb(npm_checks);
		out[count++] = Move(dst - adv_dir, dst);
	}

	/* Queen captures / checks */
	bitboard queens = ctm & board.get_piece_occ(piece::QUEEN);

	while (queens) {
		int src = bb::poplsb(queens);
		bitboard moves = attacks::queen(src, board.get_global_occ()) & (opp | bchecks | rchecks);

		while (moves) {
			int dst = bb::poplsb(moves);
			out[count++] = Move(src, dst);
		}
	}

	/* Rook captures / checks */
	bitboard rooks = ctm & board.get_piece_occ(piece::ROOK);

	while (rooks) {
		int src = bb::poplsb(rooks);
		bitboard moves = attacks::rook(src, board.get_global_occ()) & (opp | rchecks);

		while (moves) {
			int dst = bb::poplsb(moves);
			out[count++] = Move(src, dst);
		}
	}

	/* Knight captures / checks */
	bitboard knights = ctm & board.get_piece_occ(piece::KNIGHT);

	while (knights) {
		int src = bb::poplsb(knights);
		bitboard moves = attacks::knight(src) & (opp | nchecks);

		while (moves) {
			int dst = bb::poplsb(moves);
			out[count++] = Move(src, dst);
		}
	}

	/* Bishop captures / checks */
	bitboard bishops = ctm & board.get_piece_occ(piece::BISHOP);

	while (bishops) {
		int src = bb::poplsb(bishops);
		bitboard moves = attacks::bishop(src, board.get_global_occ()) & (opp | bchecks);

		while (moves) {
			int dst = bb::poplsb(moves);
			out[count++] = Move(src, dst);
		}
	}

	/* King captures */
	bitboard kings = ctm & board.get_piece_occ(piece::KING);

	while (kings) {
		int src = bb::poplsb(kings);
		bitboard moves = attacks::king(src) & opp;

		while (moves) {
			int dst = bb::poplsb(moves);
			out[count++] = Move(src, dst);
		}
	}

	assert(count <= MAX_PL_MOVES);
	return count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %r15
movq %rdi, %r12
movq 0x160(%rdi), %rax
cmpl $0x0, -0x14(%rax)
je 0x9f0a
movq %r12, %rdi
movq %r15, %rsi
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x94d0
movslq 0x170(%r12), %rax
xorl %ecx, %ecx
testq %rax, %rax
sete %cl
movq 0x8(%r12,%rax,8), %r14
movq 0x8(%r12,%rcx,8), %rax
movq 0x40(%r12), %rdi
movq %rax, 0x18(%rsp)
andq %rax, %rdi
callq 0x6515
movl %eax, %ebx
movq (%r12), %rsi
movl %eax, %edi
callq 0x6440
movq %rax, 0x70(%rsp)
movq %r14, 0x28(%rsp)
notq %r14
movq (%r12), %rsi
movl %ebx, %edi
callq 0x649e
movq %rax, 0x30(%rsp)
movslq %ebx, %rax
leaq 0x13856(%rip), %rcx     # 0x1d7c0
movq (%rcx,%rax,8), %rcx
andq %r14, %rcx
movq %rcx, 0x78(%rsp)
movl 0x170(%r12), %esi
xorl %ecx, %ecx
testl %esi, %esi
sete %cl
shll $0x9, %ecx
addq 0x13001(%rip), %rcx     # 0x1cf90
movq (%rcx,%rax,8), %rax
movq %rax, 0xb8(%rsp)
movq 0x160(%r12), %rax
movl -0x2c(%rax), %edi
cmpl $0x3f, %edi
ja 0x9fbf
callq 0x5103
movq %rax, 0x68(%rsp)
movl 0x170(%r12), %esi
jmp 0x9fc8
movq $0x0, 0x68(%rsp)
andq %r14, 0x70(%rsp)
andq %r14, 0x30(%rsp)
movq %r12, 0x10(%rsp)
movq 0x18(%r12), %rdi
andq 0x28(%rsp), %rdi
xorl %ecx, %ecx
testl %esi, %esi
sete %cl
movabsq $0xff000000000000, %rax # imm = 0xFF000000000000
movl $0xff00, %edx           # imm = 0xFF00
cmoveq %rax, %rdx
shll $0x4, %ecx
addl $-0x9, %ecx
movq %rdi, 0xb0(%rsp)
andq %rdi, %rdx
movabsq $0xfe00000000fe00, %rax # imm = 0xFE00000000FE00
movq %rdx, 0x20(%rsp)
andq %rdx, %rax
movq %rax, %rdx
movq %rcx, 0x38(%rsp)
shlq %cl, %rdx
shrq $0x9, %rax
movl %esi, 0xc(%rsp)
testl %esi, %esi
cmoveq %rdx, %rax
andq 0x18(%rsp), %rax
leaq 0xa8(%rsp), %rcx
movq %rax, (%rcx)
xorl %r12d, %r12d
movq %rsp, %rbx
pushq $0x2
popq %r13
movq %r15, 0x40(%rsp)
testq %rax, %rax
je 0xa0c1
leaq 0xa8(%rsp), %rdi
callq 0x64fc
movl %eax, %r14d
movl %eax, %ebp
subl 0x38(%rsp), %ebp
movq %rbx, %rdi
movl %ebp, %esi
movl %eax, %edx
pushq $0x4
popq %rcx
callq 0x6d4e
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, (%r15)
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
movl %r13d, %ecx
callq 0x6d4e
addq $0x2, %r12
movl 0x8(%rsp), %eax
movl %eax, 0x14(%r15)
movq (%rsp), %rax
movq %rax, 0xc(%r15)
movq 0xa8(%rsp), %rax
addq $0x18, %r15
jmp 0xa056
xorl %ecx, %ecx
movl 0xc(%rsp), %esi
testl %esi, %esi
sete %cl
shll $0x4, %ecx
addl $-0x7, %ecx
movabsq $0x7f000000007f00, %rax # imm = 0x7F000000007F00
andq 0x20(%rsp), %rax
movq %rax, %rdx
movq %rcx, 0x80(%rsp)
shlq %cl, %rdx
shrq $0x7, %rax
testl %esi, %esi
cmoveq %rdx, %rax
andq 0x18(%rsp), %rax
leaq 0xa0(%rsp), %rcx
movq %rax, (%rcx)
movq %rsp, %r13
pushq $0x2
popq %r14
testq %rax, %rax
je 0xa17c
leaq 0xa0(%rsp), %rdi
callq 0x64fc
movl %eax, %ebx
movl %eax, %ebp
subl 0x80(%rsp), %ebp
movq %r13, %rdi
movl %ebp, %esi
movl %eax, %edx
pushq $0x4
popq %rcx
callq 0x6d4e
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, (%r15)
movq %r13, %rdi
movl %ebp, %esi
movl %ebx, %edx
movl %r14d, %ecx
callq 0x6d4e
addq $0x2, %r12
movl 0x8(%rsp), %eax
movl %eax, 0x14(%r15)
movq (%rsp), %rax
movq %rax, 0xc(%r15)
movq 0xa0(%rsp), %rax
addq $0x18, %r15
jmp 0xa110
xorl %r13d, %r13d
movl 0xc(%rsp), %edx
testl %edx, %edx
sete %r13b
shll $0x4, %r13d
addl $-0x8, %r13d
movq 0x20(%rsp), %rsi
movq %rsi, %rax
movl %r13d, %ecx
shlq %cl, %rax
movq %rsi, %rcx
shrq $0x8, %rcx
testl %edx, %edx
cmoveq %rax, %rcx
movq 0x10(%rsp), %rax
movq (%rax), %rax
notq %rax
andq %rcx, %rax
leaq 0x98(%rsp), %rcx
movq %rax, (%rcx)
movq %rsp, %rbx
testq %rax, %rax
je 0xa232
leaq 0x98(%rsp), %rdi
callq 0x64fc
movl %eax, %r14d
movl %eax, %ebp
subl %r13d, %ebp
movq %rbx, %rdi
movl %ebp, %esi
movl %eax, %edx
pushq $0x4
popq %rcx
callq 0x6d4e
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, (%r15)
movq %rbx, %rdi
movl %ebp, %esi
movl %r14d, %edx
pushq $0x2
popq %rcx
callq 0x6d4e
addq $0x2, %r12
movl 0x8(%rsp), %eax
movl %eax, 0x14(%r15)
movq (%rsp), %rax
movq %rax, 0xc(%r15)
movq 0x98(%rsp), %rax
addq $0x18, %r15
jmp 0xa1c8
movq 0x20(%rsp), %rcx
xorq 0xb0(%rsp), %rcx
movabsq $-0x101010101010102, %rax # imm = 0xFEFEFEFEFEFEFEFE
movq %rcx, 0x20(%rsp)
andq %rcx, %rax
movq %rax, %rdx
movq 0x38(%rsp), %rcx
shlq %cl, %rdx
shrq $0x9, %rax
cmpl $0x0, 0xc(%rsp)
cmoveq %rdx, %rax
movq 0x68(%rsp), %rcx
orq 0x18(%rsp), %rcx
movq %rcx, 0x68(%rsp)
andq %rcx, %rax
leaq 0xd0(%rsp), %rbx
movq %rax, (%rbx)
movq %rsp, %r14
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0xa2cb
movq %rbx, %rdi
callq 0x64fc
movl %eax, %esi
subl 0x38(%rsp), %esi
movq %r14, %rdi
movl %eax, %edx
movl %ebp, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, (%r15)
movq 0xd0(%rsp), %rax
addq $0xc, %r15
jmp 0xa28c
movabsq $0x7f7f7f7f7f7f7f7f, %rax # imm = 0x7F7F7F7F7F7F7F7F
andq 0x20(%rsp), %rax
movq %rax, %rdx
movq 0x80(%rsp), %rcx
shlq %cl, %rdx
shrq $0x7, %rax
cmpl $0x0, 0xc(%rsp)
cmoveq %rdx, %rax
andq 0x68(%rsp), %rax
leaq 0xc8(%rsp), %rbx
movq %rax, (%rbx)
movq %rsp, %r14
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0xa34d
movq %rbx, %rdi
callq 0x64fc
movl %eax, %esi
subl 0x80(%rsp), %esi
movq %r14, %rdi
movl %eax, %edx
movl %ebp, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, (%r15)
movq 0xc8(%rsp), %rax
addq $0xc, %r15
jmp 0xa30b
movq 0x20(%rsp), %rdx
movq %rdx, %rax
movl %r13d, %ecx
shlq %cl, %rax
shrq $0x8, %rdx
cmpl $0x0, 0xc(%rsp)
cmoveq %rax, %rdx
movq 0x10(%rsp), %rax
movq (%rax), %rax
notq %rax
andq 0xb8(%rsp), %rdx
andq %rax, %rdx
leaq 0xc0(%rsp), %rbx
movq %rdx, (%rbx)
movq %rsp, %r14
pushq $-0x1
popq %rbp
testq %rdx, %rdx
je 0xa3cd
movq %rbx, %rdi
callq 0x64fc
movl %eax, %esi
subl %r13d, %esi
movq %r14, %rdi
movl %eax, %edx
movl %ebp, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, (%r15)
movq 0xc0(%rsp), %rdx
addq $0xc, %r15
jmp 0xa38f
movq 0x10(%rsp), %rax
movq 0x38(%rax), %rax
andq 0x28(%rsp), %rax
leaq 0x90(%rsp), %rcx
movq %rax, (%rcx)
movq 0x70(%rsp), %rcx
orq 0x18(%rsp), %rcx
movq %rcx, 0x70(%rsp)
orq 0x30(%rsp), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x60(%rsp), %r13
movq %rsp, %rbp
pushq $-0x1
popq %r14
testq %rax, %rax
je 0xa489
leaq 0x90(%rsp), %rdi
callq 0x64fc
movl %eax, %r15d
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movl %r15d, %edi
callq 0xcf4b
andq 0x38(%rsp), %rax
movq %rax, 0x60(%rsp)
movslq %r12d, %r12
imulq $0xc, %r12, %rbx
addq 0x40(%rsp), %rbx
testq %rax, %rax
je 0xa47f
movq %r13, %rdi
callq 0x64fc
movq %rbp, %rdi
movl %r15d, %esi
movl %eax, %edx
movl %r14d, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
movq (%rsp), %rax
movq %rax, (%rbx)
movq 0x60(%rsp), %rax
addq $0xc, %rbx
jmp 0xa446
movq 0x90(%rsp), %rax
jmp 0xa40b
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rax
andq 0x28(%rsp), %rax
leaq 0x60(%rsp), %rcx
movq %rax, (%rcx)
movq 0x30(%rsp), %rcx
orq 0x18(%rsp), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x58(%rsp), %r13
movq %rsp, %rbp
pushq $-0x1
popq %r14
testq %rax, %rax
je 0xa532
leaq 0x60(%rsp), %rdi
callq 0x64fc
movl %eax, %r15d
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movl %r15d, %edi
callq 0x649e
andq 0x30(%rsp), %rax
movq %rax, 0x58(%rsp)
movslq %r12d, %r12
imulq $0xc, %r12, %rbx
addq 0x40(%rsp), %rbx
testq %rax, %rax
je 0xa52b
movq %r13, %rdi
callq 0x64fc
movq %rbp, %rdi
movl %r15d, %esi
movl %eax, %edx
movl %r14d, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
movq (%rsp), %rax
movq %rax, (%rbx)
movq 0x58(%rsp), %rax
addq $0xc, %rbx
jmp 0xa4f2
movq 0x60(%rsp), %rax
jmp 0xa4ba
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
andq 0x28(%rsp), %rax
leaq 0x58(%rsp), %rcx
movq %rax, (%rcx)
movq 0x78(%rsp), %rcx
orq 0x18(%rsp), %rcx
movq %rcx, 0x78(%rsp)
leaq 0x50(%rsp), %r14
movq %rsp, %r13
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0xa5d5
leaq 0x58(%rsp), %rdi
callq 0x64fc
movl %eax, %ebx
cltq
leaq 0x13244(%rip), %rcx     # 0x1d7c0
movq (%rcx,%rax,8), %rax
andq 0x78(%rsp), %rax
movq %rax, 0x50(%rsp)
movslq %r12d, %r12
imulq $0xc, %r12, %r15
addq 0x40(%rsp), %r15
testq %rax, %rax
je 0xa5ce
movq %r14, %rdi
callq 0x64fc
movq %r13, %rdi
movl %ebx, %esi
movl %eax, %edx
movl %ebp, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r15)
movq (%rsp), %rax
movq %rax, (%r15)
movq 0x50(%rsp), %rax
addq $0xc, %r15
jmp 0xa596
movq 0x58(%rsp), %rax
jmp 0xa562
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
andq 0x28(%rsp), %rax
leaq 0x50(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x48(%rsp), %r13
movq %rsp, %rbp
pushq $-0x1
popq %r14
testq %rax, %rax
je 0xa66f
leaq 0x50(%rsp), %rdi
callq 0x64fc
movl %eax, %r15d
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movl %r15d, %edi
callq 0x6440
andq 0x70(%rsp), %rax
movq %rax, 0x48(%rsp)
movslq %r12d, %r12
imulq $0xc, %r12, %rbx
addq 0x40(%rsp), %rbx
testq %rax, %rax
je 0xa668
movq %r13, %rdi
callq 0x64fc
movq %rbp, %rdi
movl %r15d, %esi
movl %eax, %edx
movl %r14d, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rbx)
movq (%rsp), %rax
movq %rax, (%rbx)
movq 0x48(%rsp), %rax
addq $0xc, %rbx
jmp 0xa62f
movq 0x50(%rsp), %rax
jmp 0xa5f7
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
andq 0x40(%rcx), %rax
leaq 0x48(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x88(%rsp), %r15
movq %rsp, %r13
pushq $-0x1
popq %rbp
testq %rax, %rax
je 0xa70c
leaq 0x48(%rsp), %rdi
callq 0x64fc
movl %eax, %ebx
cltq
leaq 0x12f13(%rip), %rcx     # 0x1d5c0
movq (%rcx,%rax,8), %rax
andq 0x18(%rsp), %rax
movq %rax, 0x88(%rsp)
movslq %r12d, %r12
imulq $0xc, %r12, %r14
addq 0x40(%rsp), %r14
testq %rax, %rax
je 0xa705
movq %r15, %rdi
callq 0x64fc
movq %r13, %rdi
movl %ebx, %esi
movl %eax, %edx
movl %ebp, %ecx
callq 0x6d4e
incq %r12
movl 0x8(%rsp), %eax
movl %eax, 0x8(%r14)
movq (%rsp), %rax
movq %rax, (%r14)
movq 0x88(%rsp), %rax
addq $0xc, %r14
jmp 0xa6ca
movq 0x48(%rsp), %rax
jmp 0xa693
cmpl $0x65, %r12d
jge 0xa727
movl %r12d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa775(%rip), %rdi      # 0x14ea3
leaq 0xa5e0(%rip), %rsi      # 0x14d15
leaq 0xa7f4(%rip), %rcx      # 0x14f30
movl $0x36c, %edx            # imm = 0x36C
callq 0x41f0