// File: codeandkey[P]neocortex/src/search.cpp
// Function: neocortex::search::Search::quiescence_captures(neocortex::Position&, int, int, int, neocortex::PV*)
int search::Search::quiescence_captures(Position& root, int alpha, int beta, int ply_dist, PV* pv_line) {
	PV local_pv;

	/* Check for threefold repetition or 50-move-rule */
	if (root.num_repetitions() >= 3 || root.halfmove_clock() >= 50) {
		return eval::CONTEMPT;
	}

	int cur_score = root.evaluate();

	/* Perform standing pat */

	if (!root.check()) {
		if (cur_score >= beta) {
			pv_line->len = 0;

			/* Set longest line */
			if (ply_dist > max_ply_searched) {
				max_ply_searched = ply_dist;
			}

			//neocortex_debug("Returning standing pat score %d\n", cur_score);
			return beta;
		}

		if (alpha < cur_score) {
			//neocortex_debug("Setting alpha to standing pat %d\n", cur_score);
			alpha = cur_score;
		}
	}

	Move pl_moves[MAX_PL_MOVES];
	int num_pl_moves = root.pseudolegal_moves_quiescence_captures(pl_moves);
	int num_moves = 0; /* num of legal moves */

	num_pl_moves = root.order_moves_quiescence(pl_moves, num_pl_moves);

	for (int i = 0; i < num_pl_moves; ++i) {
		if (root.make_move(pl_moves[i])) {
			num_moves++;

			int value = -quiescence_captures(root, -beta, -alpha, ply_dist + 1, &local_pv);

			root.unmake_move(pl_moves[i]);

			if (value == score::INCOMPLETE) {
				return value;
			}

			if (value >= beta) return beta;

			if (value > alpha) {
				alpha = value;

				pv_line->moves[0] = pl_moves[i];
				pv_line->len = local_pv.len + 1;

				memcpy(pv_line->moves + 1, local_pv.moves, local_pv.len * sizeof local_pv.moves[0]);
			}
		}
		else {
			root.unmake_move(pl_moves[i]);
		}
	}

	if (!num_moves) {
		if (root.check()) {
			pv_line->len = 0;
			return score::CHECKMATED + ply_dist;
		}
		else {
			/* In qsearch if there are no Q moves, we can't assume the game is a draw, but we cannot search further.
			 * Here we just return the static eval of the node. */
			pv_line->len = 0;

			/* Set longest line */
			if (ply_dist > max_ply_searched) {
				max_ply_searched = ply_dist;
			}

			return cur_score;
		}
	}

	return alpha;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xba0, %rsp            # imm = 0xBA0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb78(%rbp)
leaq -0x634(%rbp), %rdi
callq 0xae50
movq -0x18(%rbp), %rdi
callq 0x13fd0
cmpl $0x3, %eax
jge 0x180a4
movq -0x18(%rbp), %rdi
callq 0x14080
cmpl $0x32, %eax
jl 0x180b0
movl $0x0, -0x4(%rbp)
jmp 0x184c3
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x12400
movl %eax, -0x638(%rbp)
movq -0x18(%rbp), %rdi
callq 0xbc10
testb $0x1, %al
jne 0x18144
movl -0x638(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0x1812e
movq -0xb78(%rbp), %rdi
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movl -0x24(%rbp), %eax
movl %eax, -0xb7c(%rbp)
addq $0x81e4, %rdi           # imm = 0x81E4
callq 0x19380
movl %eax, %ecx
movl -0xb7c(%rbp), %eax
cmpl %ecx, %eax
jle 0x18123
movq -0xb78(%rbp), %rdi
movl -0x24(%rbp), %esi
addq $0x81e4, %rdi           # imm = 0x81E4
callq 0x190e0
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x184c3
movl -0x1c(%rbp), %eax
cmpl -0x638(%rbp), %eax
jge 0x18142
movl -0x638(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x18144
leaq -0xaf0(%rbp), %rax
movq %rax, %rcx
addq $0x4b0, %rcx            # imm = 0x4B0
movq %rcx, -0xb90(%rbp)
movq %rax, -0xb88(%rbp)
movq -0xb88(%rbp), %rdi
movq %rdi, -0xb98(%rbp)
callq 0xa870
movq -0xb98(%rbp), %rax
movq -0xb90(%rbp), %rcx
addq $0xc, %rax
cmpq %rcx, %rax
movq %rax, -0xb88(%rbp)
jne 0x18163
movq -0x18(%rbp), %rdi
leaq -0xaf0(%rbp), %rsi
callq 0x10aa0
movl %eax, -0xaf4(%rbp)
movl $0x0, -0xaf8(%rbp)
movq -0x18(%rbp), %rdi
leaq -0xaf0(%rbp), %rsi
movl -0xaf4(%rbp), %edx
leaq 0x24abc(%rip), %rax     # 0x3cc88
movq (%rax), %rcx
movq %rcx, -0xb04(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xafc(%rbp)
movq -0xb04(%rbp), %rax
movq %rax, -0xb14(%rbp)
movl -0xafc(%rbp), %eax
movl %eax, -0xb0c(%rbp)
movq -0xb14(%rbp), %rcx
movl -0xb0c(%rbp), %r8d
callq 0x11b50
movl %eax, -0xaf4(%rbp)
movl $0x0, -0xb18(%rbp)
movl -0xb18(%rbp), %eax
cmpl -0xaf4(%rbp), %eax
jge 0x1843b
movq -0x18(%rbp), %rdi
movslq -0xb18(%rbp), %rcx
leaq -0xaf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0xb24(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xb1c(%rbp)
movq -0xb24(%rbp), %rax
movq %rax, -0xb34(%rbp)
movl -0xb1c(%rbp), %eax
movl %eax, -0xb2c(%rbp)
movq -0xb34(%rbp), %rsi
movl -0xb2c(%rbp), %edx
callq 0xd190
testb $0x1, %al
jne 0x1828f
jmp 0x183cd
movq -0xb78(%rbp), %rdi
movl -0xaf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xaf8(%rbp)
movq -0x18(%rbp), %rsi
xorl %edx, %edx
subl -0x20(%rbp), %edx
xorl %ecx, %ecx
subl -0x1c(%rbp), %ecx
movl -0x24(%rbp), %r8d
addl $0x1, %r8d
leaq -0x634(%rbp), %r9
callq 0x18050
movl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0xb38(%rbp)
movq -0x18(%rbp), %rdi
movslq -0xb18(%rbp), %rcx
leaq -0xaf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0xb44(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xb3c(%rbp)
movq -0xb44(%rbp), %rax
movq %rax, -0xb54(%rbp)
movl -0xb3c(%rbp), %eax
movl %eax, -0xb4c(%rbp)
movq -0xb54(%rbp), %rsi
movl -0xb4c(%rbp), %edx
callq 0xdd20
cmpl $0x80000000, -0xb38(%rbp) # imm = 0x80000000
jne 0x18345
movl -0xb38(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x184c3
movl -0xb38(%rbp), %eax
cmpl -0x20(%rbp), %eax
jl 0x1835b
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x184c3
movl -0xb38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jle 0x183cb
movl -0xb38(%rbp), %eax
movl %eax, -0x1c(%rbp)
movslq -0xb18(%rbp), %rax
leaq -0xaf0(%rbp), %rcx
imulq $0xc, %rax, %rax
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, 0x4(%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0xc(%rax)
movl -0x634(%rbp), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rdi
addq $0x4, %rdi
addq $0xc, %rdi
leaq -0x634(%rbp), %rsi
addq $0x4, %rsi
movslq -0x634(%rbp), %rax
imulq $0xc, %rax, %rdx
callq 0x5360
jmp 0x18425
movq -0x18(%rbp), %rdi
movslq -0xb18(%rbp), %rcx
leaq -0xaf0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0xb60(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xb58(%rbp)
movq -0xb60(%rbp), %rax
movq %rax, -0xb70(%rbp)
movl -0xb58(%rbp), %eax
movl %eax, -0xb68(%rbp)
movq -0xb70(%rbp), %rsi
movl -0xb68(%rbp), %edx
callq 0xdd20
jmp 0x18427
movl -0xb18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb18(%rbp)
jmp 0x1821c
cmpl $0x0, -0xaf8(%rbp)
jne 0x184bd
movq -0x18(%rbp), %rdi
callq 0xbc10
testb $0x1, %al
jne 0x18453
jmp 0x1846a
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movl -0x24(%rbp), %eax
addl $0xffc2f700, %eax       # imm = 0xFFC2F700
movl %eax, -0x4(%rbp)
jmp 0x184c3
movq -0xb78(%rbp), %rdi
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
movl -0x24(%rbp), %eax
movl %eax, -0xb9c(%rbp)
addq $0x81e4, %rdi           # imm = 0x81E4
callq 0x19380
movl %eax, %ecx
movl -0xb9c(%rbp), %eax
cmpl %ecx, %eax
jle 0x184b2
movq -0xb78(%rbp), %rdi
movl -0x24(%rbp), %esi
addq $0x81e4, %rdi           # imm = 0x81E4
callq 0x190e0
movl -0x638(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x184c3
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xba0, %rsp            # imm = 0xBA0
popq %rbp
retq
nop