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

	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()];

		// SEE on basic captures
		if (board.get_color_occ(!color_to_move) & bb::mask(moves[i].dst())) scores[i] += see_capture(moves[i]);

		// SEE on EP captures
		if (piece::type(board.get_piece(moves[i].src())) == piece::PAWN && moves[i].dst() == ply.back().en_passant_square) scores[i] += see_capture(moves[i]);
	}

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

		for (int j = i + 1; j < num_moves; ++j) {
			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;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movl %edx, %ebx
movq %rsi, %r13
movq %rdi, %r15
leaq 0x30(%rsp), %rax
movq %rcx, (%rax)
movl %r8d, 0x8(%rax)
leaq 0x40(%rsp), %rdi
xorl %ebp, %ebp
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x41a0
leaq 0x178(%r15), %rax
movq %rax, 0x20(%rsp)
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x28(%rsp)
movq %r13, 0x18(%rsp)
cmpq %rbp, 0x28(%rsp)
je 0xafb2
movq %r13, %rdi
leaq 0x30(%rsp), %rsi
callq 0x6fee
movl 0x40(%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 0x20(%rsp), %rax
shlq $0x8, %rdx
addq %rax, %rdx
addl (%rdx,%rdi,4), %r12d
movl %r12d, 0x40(%rsp,%rbp,4)
movq 0x8(%r15,%rcx,8), %r14
callq 0x5103
testq %r14, %rax
je 0xaf70
movq (%r13), %rsi
movq %r15, %rdi
callq 0xb06a
addl %eax, %r12d
movl %r12d, 0x40(%rsp,%rbp,4)
movslq (%r13), %rax
movl 0x48(%r15,%rax,4), %edi
callq 0x63ef
testl %eax, %eax
jne 0xafa6
movl 0x4(%r13), %eax
movq 0x160(%r15), %rcx
cmpl -0x2c(%rcx), %eax
jne 0xafa6
movq (%r13), %rsi
movq %r15, %rdi
callq 0xb06a
addl %eax, %r12d
movl %r12d, 0x40(%rsp,%rbp,4)
incq %rbp
addq $0xc, %r13
jmp 0xaef4
cmpl $0x2, %ebx
pushq $0x1
popq %rax
cmovgel %ebx, %eax
decl %eax
pushq $0x1
popq %rcx
xorl %edx, %edx
movq 0x18(%rsp), %r11
cmpq %rax, %rdx
je 0xb058
movl 0x40(%rsp,%rdx,4), %esi
movq %rcx, %r8
movl %esi, %r9d
movl %edx, %edi
cmpl %ebx, %r8d
jge 0xaff6
movl 0x40(%rsp,%r8,4), %r10d
cmpl %r9d, %r10d
cmovgl %r10d, %r9d
cmovgl %r8d, %edi
incq %r8
jmp 0xafdc
movl %edi, %r8d
cmpq %r8, %rdx
je 0xb04d
movslq %edi, %rdi
movl 0x40(%rsp,%rdi,4), %r8d
movl %esi, 0x40(%rsp,%rdi,4)
movl %r8d, 0x40(%rsp,%rdx,4)
imulq $0xc, %rdi, %rsi
movl 0x8(%r11,%rsi), %edi
movl %edi, 0x10(%rsp)
movq (%r11,%rsi), %rdi
movq %rdi, 0x8(%rsp)
imulq $0xc, %rdx, %rdi
movl 0x8(%r11,%rdi), %r8d
movl %r8d, 0x8(%r11,%rsi)
movq (%r11,%rdi), %r8
movq %r8, (%r11,%rsi)
movl 0x10(%rsp), %esi
movl %esi, 0x8(%r11,%rdi)
movq 0x8(%rsp), %rsi
movq %rsi, (%r11,%rdi)
incq %rdx
incq %rcx
jmp 0xafc7
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq