// File: /workspace/github_cpp_makefile_star2_O2/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, %ebx
movq %rsi, %r13
movq %rdi, %r15
leaq 0x40(%rsp), %rax
movq %rcx, (%rax)
movl %r8d, 0x8(%rax)
leaq 0x50(%rsp), %rdi
xorl %ebp, %ebp
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x41a0
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x10(%rsp)
leaq 0x178(%r15), %rax
movq %rax, 0x8(%rsp)
movq %r13, 0x20(%rsp)
movl %ebx, 0x4(%rsp)
cmpq %rbp, 0x10(%rsp)
je 0xb304
movq %r13, %rdi
leaq 0x40(%rsp), %rsi
callq 0x6fee
movl 0x50(%rsp,%rbp,4), %ecx
leal 0x9c40(%rcx), %r12d
testb %al, %al
cmovel %ecx, %r12d
movslq 0x170(%r15), %rax
xorl %ecx, %ecx
testq %rax, %rax
sete %cl
movslq (%r13), %rdx
movslq 0x4(%r13), %rdi
shlq $0xe, %rax
addq 0x8(%rsp), %rax
shlq $0x8, %rdx
addq %rax, %rdx
addl (%rdx,%rdi,4), %r12d
movl %r12d, 0x50(%rsp,%rbp,4)
movq 0x8(%r15,%rcx,8), %r14
callq 0x5103
andq %r14, %rax
movq 0x40(%r15), %rcx
notq %rcx
testq %rcx, %rax
je 0xb2b3
movq (%r13), %rsi
movq %r15, %rdi
callq 0xb06a
testl %eax, %eax
js 0xb2d7
movq (%r13), %rsi
movq %r15, %rdi
callq 0xb06a
addl %r12d, %eax
movl %eax, 0x50(%rsp,%rbp,4)
jmp 0xb2f8
movslq (%r13), %rax
movl 0x48(%r15,%rax,4), %edi
callq 0x63ef
testl %eax, %eax
jne 0xb2f8
movl 0x4(%r13), %eax
movq 0x160(%r15), %rcx
cmpl -0x2c(%rcx), %eax
je 0xb28e
jmp 0xb2f8
leaq 0x1b8ea(%rip), %rcx     # 0x26bc8
movl 0x8(%rcx), %eax
movl %eax, 0x8(%r13)
movq (%rcx), %rax
movq %rax, (%r13)
movl $0x80000000, 0x50(%rsp,%rbp,4) # imm = 0x80000000
decl 0x4(%rsp)
incq %rbp
addq $0xc, %r13
jmp 0xb21c
xorl %r12d, %r12d
movl 0x4(%rsp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, 0x38(%rsp)
movq 0x20(%rsp), %rax
leaq 0xc(%rax), %r15
pushq $0x1
popq %r13
cmpq 0x38(%rsp), %r12
je 0xb3e6
movl 0x50(%rsp,%r12,4), %r14d
movq %r15, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movl %r14d, 0x1c(%rsp)
movl %r12d, %ebp
cmpl %ebx, %r13d
jge 0xb373
movq %r15, %rdi
callq 0x6f7c
testb %al, %al
je 0xb36a
movl 0x50(%rsp,%r13,4), %eax
cmpl %r14d, %eax
cmovgl %eax, %r14d
cmovgl %r13d, %ebp
incq %r13
addq $0xc, %r15
jmp 0xb349
movl %ebp, %eax
cmpq %rax, %r12
je 0xb3cd
movslq %ebp, %rax
movl 0x50(%rsp,%rax,4), %ecx
movl 0x1c(%rsp), %edx
movl %edx, 0x50(%rsp,%rax,4)
movl %ecx, 0x50(%rsp,%r12,4)
imulq $0xc, %rax, %rax
movq 0x20(%rsp), %rsi
movl 0x8(%rsi,%rax), %ecx
movl %ecx, 0x30(%rsp)
movq (%rsi,%rax), %rcx
movq %rcx, 0x28(%rsp)
imulq $0xc, %r12, %rcx
movl 0x8(%rsi,%rcx), %edx
movl %edx, 0x8(%rsi,%rax)
movq (%rsi,%rcx), %rdx
movq %rdx, (%rsi,%rax)
movl 0x30(%rsp), %eax
movl %eax, 0x8(%rsi,%rcx)
movq 0x28(%rsp), %rax
movq %rax, (%rsi,%rcx)
incq %r12
movq 0x8(%rsp), %r13
incq %r13
movq 0x10(%rsp), %r15
addq $0xc, %r15
jmp 0xb327
movl 0x4(%rsp), %eax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq