// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1c8, %rsp            # imm = 0x1C8
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
movq %rcx, 0x20(%rsp)
movl %r8d, 0x28(%rsp)
leaq 0x30(%rsp), %rdi
movl $0x190, %edx            # imm = 0x190
xorl %esi, %esi
callq 0x4150
testl %ebx, %ebx
jle 0xccdb
leaq 0x178(%r15), %rax
movq %rax, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movl %ebx, %ebp
xorl %r12d, %r12d
movq %r14, %r13
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x7b06
movl 0x30(%rsp,%r12,4), %ecx
leal 0x9c40(%rcx), %ebx
testb %al, %al
cmovel %ecx, %ebx
movslq 0x170(%r15), %rcx
movslq (%r13), %rdx
movslq 0x4(%r13), %rax
movq %rcx, %rsi
shlq $0xe, %rsi
addq 0x18(%rsp), %rsi
shlq $0x8, %rdx
addq %rsi, %rdx
addl (%rdx,%rax,4), %ebx
movl %ebx, 0x30(%rsp,%r12,4)
cmpq $0x40, %rax
jae 0xcd0c
xorl %edx, %edx
testl %ecx, %ecx
sete %dl
movq 0x8(%r15,%rdx,8), %rcx
btq %rax, %rcx
jae 0xcbed
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcd2c
addl %eax, %ebx
movl %ebx, 0x30(%rsp,%r12,4)
movslq (%r13), %rax
movl 0x48(%r15,%rax,4), %eax
cmpl $0xc, %eax
jae 0xcced
cmpl $0x1, %eax
ja 0xcc27
movl 0x4(%r13), %eax
movq 0x160(%r15), %rcx
cmpl -0x2c(%rcx), %eax
jne 0xcc27
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcd2c
addl %eax, %ebx
movl %ebx, 0x30(%rsp,%r12,4)
incq %r12
addq $0xc, %r13
cmpq %r12, %rbp
jne 0xcb77
movq 0x10(%rsp), %r11
cmpl $0x1, %r11d
je 0xccdb
decl %r11d
movl $0x1, %eax
xorl %ecx, %ecx
movq %rcx, %rdx
movl 0x30(%rsp,%rcx,4), %esi
incq %rcx
movq %rax, %r8
movl %edx, %edi
movl %esi, %r9d
movl 0x30(%rsp,%r8,4), %r10d
cmpl %r9d, %r10d
cmovgl %r10d, %r9d
cmovgl %r8d, %edi
incq %r8
cmpq %r8, %rbp
jne 0xcc62
movl %edi, %r8d
cmpq %r8, %rdx
je 0xcccf
movslq %edi, %rdi
movl 0x30(%rsp,%rdi,4), %r8d
movl %esi, 0x30(%rsp,%rdi,4)
movl %r8d, 0x30(%rsp,%rdx,4)
leaq (%rdi,%rdi,2), %rsi
movl 0x8(%r14,%rsi,4), %edi
movl %edi, 0x8(%rsp)
movq (%r14,%rsi,4), %rdi
movq %rdi, (%rsp)
leaq (%rdx,%rdx,2), %rdx
movl 0x8(%r14,%rdx,4), %edi
movl %edi, 0x8(%r14,%rsi,4)
movq (%r14,%rdx,4), %rdi
movq %rdi, (%r14,%rsi,4)
movl 0x8(%rsp), %esi
movl %esi, 0x8(%r14,%rdx,4)
movq (%rsp), %rsi
movq %rsi, (%r14,%rdx,4)
incq %rax
cmpq %r11, %rcx
jne 0xcc50
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc02a(%rip), %rdi      # 0x18d1e
leaq 0xc033(%rip), %rsi      # 0x18d2e
leaq 0xc038(%rip), %rcx      # 0x18d3a
movl $0x45, %edx
callq 0x4190
leaq 0xbf05(%rip), %rdi      # 0x18c18
leaq 0xd59c(%rip), %rsi      # 0x1a2b6
leaq 0xd5a4(%rip), %rcx      # 0x1a2c5
movl $0x91, %edx
callq 0x4190
nop