// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb18, %rsp            # imm = 0xB18
movq %r9, %rbp
movl %r8d, %r13d
movl %ecx, (%rsp)
movl %edx, %r15d
movq %rsi, %r12
movq %rdi, %r14
leaq 0x5c(%rsp), %rdi
callq 0x7a1c
movq %r12, %rdi
callq 0xedba
xorl %ebx, %ebx
cmpl $0x2, %eax
jg 0x1150c
movq %r12, %rdi
callq 0xee0c
cmpl $0x31, %eax
jg 0x1150c
movq %r12, %rdi
xorl %esi, %esi
callq 0xd30c
movq 0x160(%r12), %rcx
cmpl $0x0, -0x14(%rcx)
je 0x112e5
movl %eax, 0x4(%rsp)
jmp 0x112f9
cmpl (%rsp), %eax
jge 0x114d4
cmpl %r15d, %eax
movl %eax, 0x4(%rsp)
cmovgl %eax, %r15d
movl %r15d, 0x8(%rsp)
movq %r14, 0x28(%rsp)
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x660, %rdi            # imm = 0x660
callq 0x7648
addq $0xc, %rbx
cmpq $0x4b0, %rbx            # imm = 0x4B0
jne 0x11305
leaq 0x660(%rsp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0xc142
leaq 0x1985c(%rip), %rdx     # 0x2ab98
movq (%rdx), %rcx
movl 0x8(%rdx), %r8d
movq %r12, %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0xce28
xorl %r15d, %r15d
testl %eax, %eax
setg %r14b
movq %r13, 0x30(%rsp)
jle 0x1149d
movl (%rsp), %ecx
negl %ecx
movl %ecx, 0x1c(%rsp)
leal 0x1(%r13), %ecx
movl %ecx, 0x18(%rsp)
leaq 0x4(%rbp), %rcx
movq %rcx, 0x40(%rsp)
movq %rbp, 0x20(%rsp)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x38(%rsp)
movl %eax, %r13d
movq %r13, %rax
negq %rax
movq %rax, 0x50(%rsp)
movl $0x1, %ebp
movq %r12, 0x10(%rsp)
movq %r13, 0x48(%rsp)
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r12, %rdi
callq 0x947c
testb %al, %al
je 0x1145e
movl %r15d, 0xc(%rsp)
movl 0x8(%rsp), %r15d
movl %r15d, %ecx
negl %ecx
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %r8d
leaq 0x5c(%rsp), %r9
callq 0x11276
movl %eax, %r13d
negl %r13d
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r12, %rdi
callq 0x9a5e
cmpl (%rsp), %r13d
jge 0x11444
cmpl %r13d, %r15d
jge 0x11444
movl 0x8(%rbx), %eax
movq 0x40(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rbx), %rax
movq %rax, (%rcx)
movslq 0x5c(%rsp), %rax
leal 0x1(%rax), %ecx
movq 0x20(%rsp), %rdx
movl %ecx, (%rdx)
shlq $0x2, %rax
leaq (%rax,%rax,2), %rdx
movq 0x38(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x4230
movl %r13d, 0x8(%rsp)
movq 0x10(%rsp), %r12
movl 0xc(%rsp), %r15d
incl %r15d
cmpl (%rsp), %r13d
movq 0x48(%rsp), %r13
jl 0x1146c
jmp 0x1148f
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r12, %rdi
callq 0x9a5e
cmpq %r13, %rbp
setb %r14b
addq $0xc, %rbx
movq 0x50(%rsp), %rax
addq %rbp, %rax
incq %rax
incq %rbp
cmpq $0x1, %rax
jne 0x113a9
testl %r15d, %r15d
setne %r15b
movq 0x20(%rsp), %rbp
jmp 0x1149d
movl 0x8(%rsp), %ebx
testb $0x1, %r14b
cmovnel (%rsp), %ebx
jne 0x1150c
testb %r15b, %r15b
movq 0x30(%rsp), %rcx
jne 0x1150c
movq 0x160(%r12), %rax
cmpl $0x0, -0x14(%rax)
movl $0x0, (%rbp)
je 0x114f3
addl $0xffc2f700, %ecx       # imm = 0xFFC2F700
movl %ecx, %ebx
jmp 0x1150c
movl $0x0, (%rbp)
movl 0x81e4(%r14), %eax
cmpl %r13d, %eax
jge 0x114ee
xchgl %r13d, 0x81e4(%r14)
movl (%rsp), %ebx
jmp 0x1150c
movq 0x28(%rsp), %rdx
movl 0x81e4(%rdx), %eax
cmpl %ecx, %eax
jge 0x11508
xchgl %ecx, 0x81e4(%rdx)
movl 0x4(%rsp), %ebx
movl %ebx, %eax
addq $0xb18, %rsp            # imm = 0xB18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq