// File: /workspace/github_cpp_makefile_star2_O1/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
movl %ebx, %ebp
movq %rbx, 0x10(%rsp)
testl %ebx, %ebx
jle 0xcb8d
leaq 0x178(%r15), %rax
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
movq %r14, %r13
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
callq 0x79fa
testb %al, %al
je 0xcae7
addl $0x9c40, 0x30(%rsp,%rbx,4) # imm = 0x9C40
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
movl 0x30(%rsp,%rbx,4), %r12d
addl (%rdx,%rax,4), %r12d
movl %r12d, 0x30(%rsp,%rbx,4)
cmpq $0x40, %rax
jae 0xcc62
xorl %edx, %edx
testl %ecx, %ecx
sete %dl
movq 0x8(%r15,%rdx,8), %rcx
btq %rax, %rcx
jae 0xcb46
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcc82
addl %r12d, %eax
movl %eax, 0x30(%rsp,%rbx,4)
movslq (%r13), %rax
movl 0x48(%r15,%rax,4), %eax
cmpl $0xc, %eax
jae 0xcc43
cmpl $0x1, %eax
ja 0xcb7d
movl 0x4(%r13), %eax
movq 0x160(%r15), %rcx
cmpl -0x2c(%rcx), %eax
jne 0xcb7d
movq (%r13), %rsi
movq %r15, %rdi
callq 0xcc82
addl %eax, 0x30(%rsp,%rbx,4)
incq %rbx
addq $0xc, %r13
cmpq %rbx, %rbp
jne 0xcace
movq 0x10(%rsp), %r11
cmpl $0x2, %r11d
jl 0xcc31
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 0xcbb8
movl %edi, %r8d
cmpq %r8, %rdx
je 0xcc25
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 0xcba6
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb09b(%rip), %rdi      # 0x17ce5
leaq 0xb0a4(%rip), %rsi      # 0x17cf5
leaq 0xb0a9(%rip), %rcx      # 0x17d01
movl $0x45, %edx
callq 0x4190
leaq 0xae1a(%rip), %rdi      # 0x17a83
leaq 0xae28(%rip), %rsi      # 0x17a98
leaq 0xae30(%rip), %rcx      # 0x17aa7
movl $0x91, %edx
callq 0x4190
nop