// File: 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
movq %rsp, %rbp
subq $0x260, %rsp            # imm = 0x260
movq %rcx, -0x1c(%rbp)
movl %r8d, -0x14(%rbp)
movq -0x1c(%rbp), %rax
movq %rax, -0xc(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movl %edx, -0x34(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x238(%rbp)
leaq -0x1d0(%rbp), %rdi
xorl %esi, %esi
movl $0x190, %edx            # imm = 0x190
callq 0x51f0
movl $0x0, -0x1d4(%rbp)
movl -0x1d4(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1176c
movq -0x30(%rbp), %rdi
movslq -0x1d4(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
leaq -0xc(%rbp), %rsi
callq 0xadc0
testb $0x1, %al
jne 0x11504
jmp 0x1151f
movslq -0x1d4(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %ecx
addl $0x9c40, %ecx           # imm = 0x9C40
movl %ecx, -0x1d0(%rbp,%rax,4)
movq -0x238(%rbp), %rcx
movq %rcx, %rax
addq $0x178, %rax            # imm = 0x178
movslq 0x170(%rcx), %rcx
shlq $0xe, %rcx
addq %rcx, %rax
movq %rax, -0x250(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x1d4(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14df0
movl %eax, %ecx
movq -0x250(%rbp), %rax
movslq %ecx, %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movq %rax, -0x248(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x1d4(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14dd0
movq -0x238(%rbp), %rdi
movl %eax, %ecx
movq -0x248(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %ecx
movslq -0x1d4(%rbp), %rax
addl -0x1d0(%rbp,%rax,4), %ecx
movl %ecx, -0x1d0(%rbp,%rax,4)
cmpl $0x0, 0x170(%rdi)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %esi
callq 0x14e30
movq %rax, -0x240(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x1d4(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14dd0
movl %eax, %edi
callq 0x6bd0
movq %rax, %rcx
movq -0x240(%rbp), %rax
andq %rcx, %rax
cmpq $0x0, %rax
je 0x11674
movq -0x238(%rbp), %rdi
movq -0x30(%rbp), %rax
movslq -0x1d4(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x1e0(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x1d8(%rbp)
movq -0x1e0(%rbp), %rax
movq %rax, -0x1f0(%rbp)
movl -0x1d8(%rbp), %eax
movl %eax, -0x1e8(%rbp)
movq -0x1f0(%rbp), %rsi
movl -0x1e8(%rbp), %edx
callq 0x11900
movl %eax, %ecx
movslq -0x1d4(%rbp), %rax
addl -0x1d0(%rbp,%rax,4), %ecx
movl %ecx, -0x1d0(%rbp,%rax,4)
movq -0x30(%rbp), %rdi
movslq -0x1d4(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14df0
movq -0x238(%rbp), %rdi
movl %eax, %esi
callq 0x14db0
movl %eax, %edi
callq 0x8d20
cmpl $0x0, %eax
jne 0x11756
movq -0x30(%rbp), %rdi
movslq -0x1d4(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14dd0
movq -0x238(%rbp), %rdi
movl %eax, -0x254(%rbp)
addq $0x158, %rdi            # imm = 0x158
callq 0xbce0
movq %rax, %rcx
movl -0x254(%rbp), %eax
cmpl 0xc(%rcx), %eax
jne 0x11756
movq -0x238(%rbp), %rdi
movq -0x30(%rbp), %rax
movslq -0x1d4(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x1fc(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x1f4(%rbp)
movq -0x1fc(%rbp), %rax
movq %rax, -0x20c(%rbp)
movl -0x1f4(%rbp), %eax
movl %eax, -0x204(%rbp)
movq -0x20c(%rbp), %rsi
movl -0x204(%rbp), %edx
callq 0x11900
movl %eax, %ecx
movslq -0x1d4(%rbp), %rax
addl -0x1d0(%rbp,%rax,4), %ecx
movl %ecx, -0x1d0(%rbp,%rax,4)
jmp 0x11758
movl -0x1d4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x114d4
movl $0x0, -0x210(%rbp)
movl -0x210(%rbp), %eax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x118f3
movslq -0x210(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
movl %eax, -0x214(%rbp)
movl -0x210(%rbp), %eax
movl %eax, -0x218(%rbp)
movl -0x210(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x21c(%rbp)
movl -0x21c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x1180d
movslq -0x21c(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
cmpl -0x214(%rbp), %eax
jle 0x117fa
movl -0x21c(%rbp), %eax
movl %eax, -0x218(%rbp)
movslq -0x21c(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
movl %eax, -0x214(%rbp)
jmp 0x117fc
movl -0x21c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x21c(%rbp)
jmp 0x117b9
movl -0x218(%rbp), %eax
cmpl -0x210(%rbp), %eax
je 0x118dd
movslq -0x218(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
movl %eax, -0x220(%rbp)
movslq -0x210(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %ecx
movslq -0x218(%rbp), %rax
movl %ecx, -0x1d0(%rbp,%rax,4)
movl -0x220(%rbp), %ecx
movslq -0x210(%rbp), %rax
movl %ecx, -0x1d0(%rbp,%rax,4)
movq -0x30(%rbp), %rax
movslq -0x218(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x22c(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x224(%rbp)
movq -0x30(%rbp), %rcx
movslq -0x210(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rcx
movq -0x30(%rbp), %rax
movslq -0x218(%rbp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
movq -0x30(%rbp), %rax
movslq -0x210(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq -0x22c(%rbp), %rcx
movq %rcx, (%rax)
movl -0x224(%rbp), %ecx
movl %ecx, 0x8(%rax)
jmp 0x118df
movl -0x210(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x210(%rbp)
jmp 0x11776
addq $0x260, %rsp            # imm = 0x260
popq %rbp
retq
nopl (%rax)