// File: /workspace/github_cpp_makefile_star2_O3/codeandkey[P]neocortex/src/position.cpp
// Function: neocortex::Position::order_moves_quiescence(neocortex::Move*, int, neocortex::Move)
int Position::order_moves_quiescence(Move* moves, int num_moves, Move pv_move) {
	int scores[MAX_PL_MOVES] = { 0 };
	int new_num_moves = num_moves;

	for (int i = 0; i < num_moves; ++i) {
		/* Assign move scores */

		// PV move bonus
		if (moves[i] == pv_move) scores[i] += eval::ORDER_PV_MOVE;

		// History bonus
		scores[i] += history[color_to_move][moves[i].src()][moves[i].dst()];

		if (
			(board.get_color_occ(!color_to_move) & bb::mask(moves[i].dst()) & ~board.get_piece_occ(piece::KING)) || // Normal captures
			(piece::type(board.get_piece(moves[i].src())) == piece::PAWN && moves[i].dst() == ply.back().en_passant_square) // EP captures
			) {
			int see_val = see_capture(moves[i]);

			if (see_val < 0) {
				moves[i] = Move::null; /* Drop moves with negative SEE */
				scores[i] = INT_MIN;
				--new_num_moves;
			}
			else {
				scores[i] += see_capture(moves[i]);
			}
		}
	}

	/* Perform selection sort */
	for (int i = 0; i < new_num_moves; ++i) {
		int m = scores[i];
		int sind = i;

		for (int j = i + 1; j < num_moves; ++j) {
			if (!moves[j].is_valid()) continue;
			if (scores[j] > m) {
				sind = j;
				m = scores[j];
			}
		}

		if (sind != i) {
			int tmp = scores[sind];
			scores[sind] = scores[i];
			scores[i] = tmp;

			Move tmove = moves[sind];
			moves[sind] = moves[i];
			moves[i] = tmove;
		}
	}

	return new_num_moves;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movl %edx, %r14d
movq %rsi, %r13
movq %rdi, %r15
movq %rcx, 0x40(%rsp)
movl %r8d, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x4150
testl %r14d, %r14d
jle 0xd0ff
leaq 0x178(%r15), %rbx
movl %r14d, (%rsp)
movl %r14d, %ebp
xorl %r12d, %r12d
movq %r13, 0x8(%rsp)
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
callq 0x7b06
movl 0x50(%rsp,%r12,4), %ecx
leal 0x9c40(%rcx), %r14d
testb %al, %al
cmovel %ecx, %r14d
movslq 0x170(%r15), %rdx
movslq (%r13), %rax
movslq 0x4(%r13), %rcx
movq %rdx, %rsi
shlq $0xe, %rsi
addq %rbx, %rsi
movq %rax, %rdi
shlq $0x8, %rdi
addq %rsi, %rdi
addl (%rdi,%rcx,4), %r14d
movl %r14d, 0x50(%rsp,%r12,4)
cmpq $0x40, %rcx
jae 0xd114
xorl %esi, %esi
testl %edx, %edx
sete %sil
movl $0x1, %edx
shlq %cl, %rdx
andq 0x8(%r15,%rsi,8), %rdx
movq 0x40(%r15), %rsi
notq %rsi
testq %rsi, %rdx
je 0xcfb8
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcd2c
testl %eax, %eax
js 0xcfd9
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcd2c
addl %r14d, %eax
movl %eax, 0x50(%rsp,%r12,4)
jmp 0xcffa
movl 0x48(%r15,%rax,4), %eax
cmpl $0xc, %eax
jae 0xd133
cmpl $0x1, %eax
ja 0xcffa
movq 0x160(%r15), %rax
cmpl -0x2c(%rax), %ecx
je 0xcf92
jmp 0xcffa
leaq 0x1ebb8(%rip), %rcx     # 0x2bb98
movl 0x8(%rcx), %eax
movl %eax, 0x8(%r13)
movq (%rcx), %rax
movq %rax, (%r13)
movl $0x80000000, 0x50(%rsp,%r12,4) # imm = 0x80000000
decl (%rsp)
incq %r12
addq $0xc, %r13
cmpq %r12, %rbp
jne 0xcf1c
movl (%rsp), %r14d
testl %r14d, %r14d
jle 0xd0ff
movl %r14d, %eax
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %rax
leaq 0xc(%rax), %r15
movl $0x1, %r13d
xorl %ebx, %ebx
movl 0x50(%rsp,%rbx,4), %r12d
leaq 0x1(%rbx), %rax
movl %ebx, %r14d
movq %r15, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movl %r12d, 0x4(%rsp)
movq %rax, 0x28(%rsp)
cmpq %rbp, %rax
jae 0xd07d
movq %r15, %rdi
callq 0x7a5a
testb %al, %al
je 0xd071
movl 0x50(%rsp,%r13,4), %eax
cmpl %r12d, %eax
cmovgl %eax, %r12d
cmovgl %r13d, %r14d
incq %r13
addq $0xc, %r15
cmpq %r13, %rbp
jne 0xd055
movl %r14d, %eax
cmpq %rax, %rbx
je 0xd0d7
movslq %r14d, %rax
movl 0x50(%rsp,%rax,4), %ecx
movl 0x4(%rsp), %edx
movl %edx, 0x50(%rsp,%rax,4)
movl %ecx, 0x50(%rsp,%rbx,4)
leaq (%rax,%rax,2), %rax
movq 0x8(%rsp), %rsi
movl 0x8(%rsi,%rax,4), %ecx
movl %ecx, 0x18(%rsp)
movq (%rsi,%rax,4), %rcx
movq %rcx, 0x10(%rsp)
leaq (%rbx,%rbx,2), %rcx
movl 0x8(%rsi,%rcx,4), %edx
movl %edx, 0x8(%rsi,%rax,4)
movq (%rsi,%rcx,4), %rdx
movq %rdx, (%rsi,%rax,4)
movl 0x18(%rsp), %eax
movl %eax, 0x8(%rsi,%rcx,4)
movq 0x10(%rsp), %rax
movq %rax, (%rsi,%rcx,4)
movq 0x30(%rsp), %r13
incq %r13
movq 0x38(%rsp), %r15
addq $0xc, %r15
movq 0x28(%rsp), %rax
movq %rax, %rbx
cmpq 0x20(%rsp), %rax
jne 0xd030
movl (%rsp), %r14d
movl %r14d, %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbafd(%rip), %rdi      # 0x18c18
leaq 0xd194(%rip), %rsi      # 0x1a2b6
leaq 0xd19c(%rip), %rcx      # 0x1a2c5
movl $0x91, %edx
callq 0x4190
leaq 0xbbe4(%rip), %rdi      # 0x18d1e
leaq 0xbbed(%rip), %rsi      # 0x18d2e
leaq 0xbbf2(%rip), %rcx      # 0x18d3a
movl $0x45, %edx
callq 0x4190