// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1f8, %rsp            # imm = 0x1F8
movl %edx, %ebx
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
movq %rcx, 0x50(%rsp)
movl %r8d, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x4150
movl %ebx, %ebp
movl %ebx, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
testl %ebx, %ebx
jle 0xcf74
leaq 0x178(%r15), %r14
xorl %r12d, %r12d
movq 0x18(%rsp), %r13
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rsp)
movq %r13, %rdi
leaq 0x50(%rsp), %rsi
callq 0x79fa
testb %al, %al
je 0xcea1
addl $0x9c40, 0x60(%rsp,%r12,4) # imm = 0x9C40
movslq 0x170(%r15), %rdx
movslq (%r13), %rax
movslq 0x4(%r13), %rcx
movq %rdx, %rsi
shlq $0xe, %rsi
addq %r14, %rsi
movq %rax, %rdi
shlq $0x8, %rdi
addq %rsi, %rdi
movl 0x60(%rsp,%r12,4), %ebx
addl (%rdi,%rcx,4), %ebx
movl %ebx, 0x60(%rsp,%r12,4)
cmpq $0x40, %rcx
jae 0xd092
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 0xcf21
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcc82
testl %eax, %eax
js 0xcf42
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcc82
addl %ebx, %eax
movl %eax, 0x60(%rsp,%r12,4)
jmp 0xcf64
movl 0x48(%r15,%rax,4), %eax
cmpl $0xc, %eax
jae 0xd0b1
cmpl $0x1, %eax
ja 0xcf64
movq 0x160(%r15), %rax
cmpl -0x2c(%rax), %ecx
je 0xcefc
jmp 0xcf64
leaq 0x1dc4f(%rip), %rcx     # 0x2ab98
movl 0x8(%rcx), %eax
movl %eax, 0x8(%r13)
movq (%rcx), %rax
movq %rax, (%r13)
movl $0x80000000, 0x60(%rsp,%r12,4) # imm = 0x80000000
decl 0x8(%rsp)
incq %r12
addq $0xc, %r13
cmpq %r12, %rbp
jne 0xce87
cmpl $0x0, 0x8(%rsp)
jle 0xd07c
movslq 0x10(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x8(%rsp), %eax
movq %rax, 0x40(%rsp)
movq 0x18(%rsp), %rax
addq $0xc, %rax
movq %rax, 0x28(%rsp)
movl $0x1, %eax
movq %rax, 0x20(%rsp)
xorl %r12d, %r12d
movl 0x60(%rsp,%r12,4), %eax
movl %eax, 0xc(%rsp)
leaq 0x1(%r12), %rax
movl %r12d, %ebx
movq %rax, 0x10(%rsp)
cmpq 0x48(%rsp), %rax
jge 0xd004
movq 0x28(%rsp), %r14
movq 0x20(%rsp), %r15
movl %r12d, %ebx
movl 0xc(%rsp), %r13d
movq %r14, %rdi
callq 0x794e
testb %al, %al
je 0xcff8
movl 0x60(%rsp,%r15,4), %eax
cmpl %r13d, %eax
cmovgl %eax, %r13d
cmovgl %r15d, %ebx
incq %r15
addq $0xc, %r14
cmpq %r15, %rbp
jne 0xcfdc
movl %ebx, %eax
cmpq %rax, %r12
je 0xd05e
movslq %ebx, %rax
movl 0x60(%rsp,%rax,4), %ecx
movl 0xc(%rsp), %edx
movl %edx, 0x60(%rsp,%rax,4)
movl %ecx, 0x60(%rsp,%r12,4)
leaq (%rax,%rax,2), %rax
movq 0x18(%rsp), %rsi
movl 0x8(%rsi,%rax,4), %ecx
movl %ecx, 0x38(%rsp)
movq (%rsi,%rax,4), %rcx
movq %rcx, 0x30(%rsp)
leaq (%r12,%r12,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 0x38(%rsp), %eax
movl %eax, 0x8(%rsi,%rcx,4)
movq 0x30(%rsp), %rax
movq %rax, (%rsi,%rcx,4)
incq 0x20(%rsp)
addq $0xc, 0x28(%rsp)
movq 0x10(%rsp), %rax
movq %rax, %r12
cmpq 0x40(%rsp), %rax
jne 0xcfad
movl 0x8(%rsp), %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa9ea(%rip), %rdi      # 0x17a83
leaq 0xa9f8(%rip), %rsi      # 0x17a98
leaq 0xaa00(%rip), %rcx      # 0x17aa7
movl $0x91, %edx
callq 0x4190
leaq 0xac2d(%rip), %rdi      # 0x17ce5
leaq 0xac36(%rip), %rsi      # 0x17cf5
leaq 0xac3b(%rip), %rcx      # 0x17d01
movl $0x45, %edx
callq 0x4190