// File: /workspace/github_cpp_makefile_star2_O3/codeandkey[P]neocortex/src/search.cpp
// Function: neocortex::search::Search::quiescence(neocortex::Position&, int, int, int, int, neocortex::PV*)
int search::Search::quiescence(Position& root, int depth, 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();

	if (!depth) {
		return quiescence_captures(root, alpha, beta, ply_dist, pv_line);
	}

	/* Perform standing pat */

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

			//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(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(root, depth - 1, -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;

			return cur_score;
		}
	}

	return alpha;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb08, %rsp            # imm = 0xB08
movl %r9d, %r12d
movl %r8d, %r13d
movl %ecx, %ebx
movl %edx, 0xc(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0x4c(%rsp), %rdi
callq 0x7b28
movq %r14, %rdi
callq 0xf14e
xorl %ebp, %ebp
cmpl $0x2, %eax
jg 0x1151c
movq %r14, %rdi
callq 0xf1a0
cmpl $0x31, %eax
jg 0x1151c
movq 0xb40(%rsp), %rbp
movq %r14, %rdi
xorl %esi, %esi
callq 0xd36e
cmpl $0x0, 0xc(%rsp)
je 0x1132a
movl %eax, 0x10(%rsp)
movq 0x160(%r14), %rax
cmpl $0x0, -0x14(%rax)
je 0x11347
movq %r15, 0x20(%rsp)
jmp 0x1135e
movq %r15, %rdi
movq %r14, %rsi
movl %ebx, %edx
movl %r13d, %ecx
movl %r12d, %r8d
movq %rbp, %r9
callq 0x11530
movl %eax, %ebp
jmp 0x1151c
movl 0x10(%rsp), %eax
cmpl %r13d, %eax
jge 0x1150a
movq %r15, 0x20(%rsp)
cmpl %ebx, %eax
cmovgl %eax, %ebx
xorl %r15d, %r15d
leaq (%rsp,%r15), %rdi
addq $0x650, %rdi            # imm = 0x650
callq 0x7758
addq $0xc, %r15
cmpq $0x4b0, %r15            # imm = 0x4B0
jne 0x11361
leaq 0x650(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0xb6ba
leaq 0x1a800(%rip), %rdx     # 0x2bb98
movq (%rdx), %rcx
movl 0x8(%rdx), %r8d
movq %r14, %rdi
movq %rbp, %rsi
movl %eax, %edx
callq 0xcec8
testl %eax, %eax
movq 0xb40(%rsp), %rbp
jle 0x114e6
decl 0xc(%rsp)
movl %r13d, 0x14(%rsp)
negl %r13d
movl %r13d, 0x1c(%rsp)
movq %r12, 0x28(%rsp)
leal 0x1(%r12), %ecx
movl %ecx, 0x18(%rsp)
leaq 0x4(%rbp), %rcx
movq %rcx, 0x38(%rsp)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x30(%rsp)
movl %eax, %eax
shlq $0x2, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x40(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
leaq (%rsp,%r13), %r15
addq $0x650, %r15            # imm = 0x650
movq (%r15), %rsi
movl 0x8(%r15), %edx
movq %r14, %rdi
callq 0x953e
testb %al, %al
je 0x114b4
movl %ebx, %r8d
negl %r8d
leaq 0x4c(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq %r14, %rsi
movl 0xc(%rsp), %edx
movl 0x1c(%rsp), %ecx
movl 0x18(%rsp), %r9d
callq 0x112a8
movl %eax, %ebp
negl %ebp
movq (%r15), %rsi
movl 0x8(%r15), %edx
movq %r14, %rdi
callq 0x9b20
cmpl 0x14(%rsp), %ebp
jge 0x11516
incl %r12d
cmpl %ebp, %ebx
jge 0x114c5
movl 0x8(%r15), %eax
movq 0x38(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%r15), %rax
movq %rax, (%rcx)
movslq 0x4c(%rsp), %rax
leal 0x1(%rax), %ecx
movq 0xb40(%rsp), %r15
movl %ecx, (%r15)
shlq $0x2, %rax
leaq (%rax,%rax,2), %rdx
movq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4230
movl %ebp, %ebx
movq %r15, %rbp
jmp 0x114cd
movq (%r15), %rsi
movl 0x8(%r15), %edx
movq %r14, %rdi
callq 0x9b20
jmp 0x114cd
movq 0xb40(%rsp), %rbp
addq $0xc, %r13
cmpq %r13, 0x40(%rsp)
jne 0x11402
testl %r12d, %r12d
movq 0x28(%rsp), %r12
jne 0x1151a
movq 0x160(%r14), %rax
addl $0xffc2f700, %r12d      # imm = 0xFFC2F700
cmpl $0x0, -0x14(%rax)
movl $0x0, (%rbp)
cmovel 0x10(%rsp), %r12d
movl %r12d, %ebx
jmp 0x1151a
movl $0x0, (%rbp)
movl %r13d, %ebp
jmp 0x1151c
movl 0x14(%rsp), %ebx
movl %ebx, %ebp
movl %ebp, %eax
addq $0xb08, %rsp            # imm = 0xB08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq