// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb08, %rsp            # imm = 0xB08
movq %r9, %rbx
movl %r8d, %r14d
movl %ecx, 0x4(%rsp)
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r13
leaq 0x4c(%rsp), %rdi
callq 0x7b28
movq %r12, %rdi
callq 0xf14e
xorl %ebp, %ebp
cmpl $0x2, %eax
jg 0x117ab
movq %r12, %rdi
callq 0xf1a0
cmpl $0x31, %eax
jg 0x117ab
movq %r12, %rdi
xorl %esi, %esi
callq 0xd36e
movq 0x160(%r12), %rcx
cmpl $0x0, -0x14(%rcx)
movq %r12, %rbp
je 0x115a3
movl %eax, 0x8(%rsp)
jmp 0x115b8
cmpl 0x4(%rsp), %eax
jge 0x1176a
cmpl %r15d, %eax
movl %eax, 0x8(%rsp)
cmovgl %eax, %r15d
movl %r15d, (%rsp)
movq %rbx, 0x10(%rsp)
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x650, %rdi            # imm = 0x650
callq 0x7758
addq $0xc, %rbx
cmpq $0x4b0, %rbx            # imm = 0x4B0
jne 0x115c3
leaq 0x650(%rsp), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0xc1f0
leaq 0x1a59e(%rip), %rdx     # 0x2bb98
movq (%rdx), %rcx
movl 0x8(%rdx), %r8d
movq %rbp, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0xcec8
testl %eax, %eax
jle 0x11741
movl 0x4(%rsp), %ecx
negl %ecx
movl %ecx, 0x1c(%rsp)
movq %r14, 0x30(%rsp)
leal 0x1(%r14), %ecx
movl %ecx, 0x18(%rsp)
movq 0x10(%rsp), %rcx
leaq 0x4(%rcx), %rdx
movq %rdx, 0x40(%rsp)
addq $0x10, %rcx
movq %rcx, 0x38(%rsp)
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %r12
xorl %r15d, %r15d
movl $0x0, 0xc(%rsp)
movq %r12, 0x28(%rsp)
movq %r13, 0x20(%rsp)
leaq (%rsp,%r15), %rbx
addq $0x650, %rbx            # imm = 0x650
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %rbp, %rdi
callq 0x953e
testb %al, %al
je 0x1171a
movl (%rsp), %ecx
movl %ecx, %r12d
negl %ecx
movq %r13, %rdi
movq %rbp, %rsi
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %r8d
leaq 0x4c(%rsp), %r9
callq 0x11530
movl %eax, %r13d
negl %r13d
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %rbp, %r14
movq %rbp, %rdi
callq 0x9b20
cmpl 0x4(%rsp), %r13d
jge 0x11789
incl 0xc(%rsp)
cmpl %r13d, %r12d
jge 0x1170b
movl 0x8(%rbx), %eax
movq 0x40(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rbx), %rax
movq %rax, (%rcx)
movslq 0x4c(%rsp), %rax
leal 0x1(%rax), %ecx
movq 0x10(%rsp), %rdx
movl %ecx, (%rdx)
shlq $0x2, %rax
leaq (%rax,%rax,2), %rdx
movq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4230
movl %r13d, (%rsp)
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %r12
movq %r14, %rbp
jmp 0x11728
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %rbp, %rdi
callq 0x9b20
addq $0xc, %r15
cmpq %r15, %r12
jne 0x11663
cmpl $0x0, 0xc(%rsp)
movq 0x30(%rsp), %r14
jne 0x117a8
movq 0x160(%rbp), %rax
cmpl $0x0, -0x14(%rax)
movq 0x10(%rsp), %rax
movl $0x0, (%rax)
movl 0x8(%rsp), %ecx
je 0x11792
addl $0xffc2f700, %r14d      # imm = 0xFFC2F700
movl %r14d, (%rsp)
jmp 0x117a8
movl $0x0, (%rbx)
movl 0x81e4(%r13), %eax
cmpl %r14d, %eax
jge 0x11783
xchgl %r14d, 0x81e4(%r13)
movl 0x4(%rsp), %ebp
jmp 0x117ab
movl 0x4(%rsp), %eax
movl %eax, (%rsp)
jmp 0x117a8
movl 0x81e4(%r13), %eax
cmpl %r14d, %eax
jge 0x117a5
xchgl %r14d, 0x81e4(%r13)
movl %ecx, (%rsp)
movl (%rsp), %ebp
movl %ebp, %eax
addq $0xb08, %rsp            # imm = 0xB08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop