// File: 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
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
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, -0x240(%rbp)
leaq -0x1d0(%rbp), %rdi
xorl %esi, %esi
movl $0x190, %edx            # imm = 0x190
callq 0x51f0
movl -0x34(%rbp), %eax
movl %eax, -0x1d4(%rbp)
movl $0x0, -0x1d8(%rbp)
movl -0x1d8(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x11eb4
movq -0x30(%rbp), %rdi
movslq -0x1d8(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
leaq -0xc(%rbp), %rsi
callq 0xadc0
testb $0x1, %al
jne 0x11bdd
jmp 0x11bf8
movslq -0x1d8(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %ecx
addl $0x9c40, %ecx           # imm = 0x9C40
movl %ecx, -0x1d0(%rbp,%rax,4)
movq -0x240(%rbp), %rcx
movq %rcx, %rax
addq $0x178, %rax            # imm = 0x178
movslq 0x170(%rcx), %rcx
shlq $0xe, %rcx
addq %rcx, %rax
movq %rax, -0x260(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x1d8(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14df0
movl %eax, %ecx
movq -0x260(%rbp), %rax
movslq %ecx, %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movq %rax, -0x258(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x1d8(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14dd0
movq -0x240(%rbp), %rdi
movl %eax, %ecx
movq -0x258(%rbp), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %ecx
movslq -0x1d8(%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, -0x250(%rbp)
movq -0x30(%rbp), %rdi
movslq -0x1d8(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14dd0
movl %eax, %edi
callq 0x6bd0
movq -0x240(%rbp), %rdi
movq %rax, %rcx
movq -0x250(%rbp), %rax
andq %rcx, %rax
movq %rax, -0x248(%rbp)
movl $0x5, %esi
callq 0x14e50
movq %rax, %rcx
movq -0x248(%rbp), %rax
xorq $-0x1, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
jne 0x11d7e
movq -0x30(%rbp), %rdi
movslq -0x1d8(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14df0
movq -0x240(%rbp), %rdi
movl %eax, %esi
callq 0x14db0
movl %eax, %edi
callq 0x8d20
cmpl $0x0, %eax
jne 0x11e9e
movq -0x30(%rbp), %rdi
movslq -0x1d8(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0x14dd0
movq -0x240(%rbp), %rdi
movl %eax, -0x264(%rbp)
addq $0x158, %rdi            # imm = 0x158
callq 0xbce0
movq %rax, %rcx
movl -0x264(%rbp), %eax
cmpl 0xc(%rcx), %eax
jne 0x11e9e
movq -0x240(%rbp), %rdi
movq -0x30(%rbp), %rax
movslq -0x1d8(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x1e8(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x1e0(%rbp)
movq -0x1e8(%rbp), %rax
movq %rax, -0x1f8(%rbp)
movl -0x1e0(%rbp), %eax
movl %eax, -0x1f0(%rbp)
movq -0x1f8(%rbp), %rsi
movl -0x1f0(%rbp), %edx
callq 0x11900
movl %eax, -0x1dc(%rbp)
cmpl $0x0, -0x1dc(%rbp)
jge 0x11e2d
movq -0x30(%rbp), %rax
movslq -0x1d8(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
leaq 0x2ae8a(%rip), %rcx     # 0x3cc88
movq (%rcx), %rdx
movq %rdx, (%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%rax)
movslq -0x1d8(%rbp), %rax
movl $0x80000000, -0x1d0(%rbp,%rax,4) # imm = 0x80000000
movl -0x1d4(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1d4(%rbp)
jmp 0x11e9c
movq -0x240(%rbp), %rdi
movq -0x30(%rbp), %rax
movslq -0x1d8(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x204(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x1fc(%rbp)
movq -0x204(%rbp), %rax
movq %rax, -0x214(%rbp)
movl -0x1fc(%rbp), %eax
movl %eax, -0x20c(%rbp)
movq -0x214(%rbp), %rsi
movl -0x20c(%rbp), %edx
callq 0x11900
movl %eax, %ecx
movslq -0x1d8(%rbp), %rax
addl -0x1d0(%rbp,%rax,4), %ecx
movl %ecx, -0x1d0(%rbp,%rax,4)
jmp 0x11e9e
jmp 0x11ea0
movl -0x1d8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1d8(%rbp)
jmp 0x11bad
movl $0x0, -0x218(%rbp)
movl -0x218(%rbp), %eax
cmpl -0x1d4(%rbp), %eax
jge 0x12056
movslq -0x218(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
movl %eax, -0x21c(%rbp)
movl -0x218(%rbp), %eax
movl %eax, -0x220(%rbp)
movl -0x218(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x224(%rbp)
movl -0x224(%rbp), %eax
cmpl -0x34(%rbp), %eax
jge 0x11f70
movq -0x30(%rbp), %rdi
movslq -0x224(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rdi
callq 0xacf0
testb $0x1, %al
jne 0x11f27
jmp 0x11f5f
movslq -0x224(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
cmpl -0x21c(%rbp), %eax
jle 0x11f5d
movl -0x224(%rbp), %eax
movl %eax, -0x220(%rbp)
movslq -0x224(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
movl %eax, -0x21c(%rbp)
jmp 0x11f5f
movl -0x224(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x224(%rbp)
jmp 0x11eff
movl -0x220(%rbp), %eax
cmpl -0x218(%rbp), %eax
je 0x12040
movslq -0x220(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %eax
movl %eax, -0x228(%rbp)
movslq -0x218(%rbp), %rax
movl -0x1d0(%rbp,%rax,4), %ecx
movslq -0x220(%rbp), %rax
movl %ecx, -0x1d0(%rbp,%rax,4)
movl -0x228(%rbp), %ecx
movslq -0x218(%rbp), %rax
movl %ecx, -0x1d0(%rbp,%rax,4)
movq -0x30(%rbp), %rax
movslq -0x220(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0x234(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x22c(%rbp)
movq -0x30(%rbp), %rcx
movslq -0x218(%rbp), %rax
imulq $0xc, %rax, %rax
addq %rax, %rcx
movq -0x30(%rbp), %rax
movslq -0x220(%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 -0x218(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq -0x234(%rbp), %rcx
movq %rcx, (%rax)
movl -0x22c(%rbp), %ecx
movl %ecx, 0x8(%rax)
jmp 0x12042
movl -0x218(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x218(%rbp)
jmp 0x11ebe
movl -0x1d4(%rbp), %eax
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
nopw %cs:(%rax,%rax)