// File: /workspace/github_cpp_makefile_star2_O2/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, %r15d
movl %r8d, %ebp
movl %ecx, %r13d
movl %edx, %r12d
movq %rsi, %r14
movq %rdi, 0x28(%rsp)
leaq 0x4c(%rsp), %rdi
callq 0x700c
movq %r14, %rdi
callq 0xc994
xorl %ebx, %ebx
cmpl $0x2, %eax
jg 0xe723
movq %r14, %rdi
callq 0xc9d6
cmpl $0x31, %eax
jg 0xe723
movq 0xb40(%rsp), %rbx
movq %r14, %rdi
xorl %esi, %esi
callq 0xb5f4
testl %r12d, %r12d
je 0xe547
movl %eax, %ecx
movq 0x160(%r14), %rax
cmpl $0x0, -0x14(%rax)
je 0xe566
movl %ecx, 0x10(%rsp)
jmp 0xe581
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movl %r13d, %edx
movl %ebp, %ecx
movl %r15d, %r8d
movq %rbx, %r9
callq 0xe73e
movl %eax, %ebx
jmp 0xe723
cmpl %ebp, %ecx
movq 0xb40(%rsp), %rax
jge 0xe737
cmpl %r13d, %ecx
movl %ecx, 0x10(%rsp)
cmovgl %ecx, %r13d
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x650, %rdi            # imm = 0x650
callq 0x6d44
addq $0xc, %rbx
cmpq $0x4b0, %rbx            # imm = 0x4B0
jne 0xe583
leaq 0x650(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9eca
leaq 0x1860e(%rip), %rdx     # 0x26bc8
movq (%rdx), %rcx
movl 0x8(%rdx), %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0xb1c0
decl %r12d
movl %r12d, 0x24(%rsp)
movl %ebp, 0x14(%rsp)
negl %ebp
movl %ebp, 0x20(%rsp)
movq %r15, 0x30(%rsp)
leal 0x1(%r15), %ecx
movl %ecx, 0x1c(%rsp)
movq 0xb40(%rsp), %rcx
leaq 0x4(%rcx), %rdx
movq %rdx, 0x40(%rsp)
addq $0x10, %rcx
movq %rcx, 0x38(%rsp)
xorl %r15d, %r15d
testl %eax, %eax
cmovlel %r15d, %eax
imulq $0xc, %rax, %r12
movl $0x0, 0x18(%rsp)
cmpq %r15, %r12
je 0xe6e9
leaq (%rsp,%r15), %rbx
addq $0x650, %rbx            # imm = 0x650
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r14, %rdi
callq 0x8258
testb %al, %al
je 0xe6cc
movl %r13d, %r8d
negl %r8d
leaq 0x4c(%rsp), %rax
movq %rax, (%rsp)
movq 0x28(%rsp), %rdi
movq %r14, %rsi
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %ecx
movl 0x1c(%rsp), %r9d
callq 0xe4c8
movl %eax, %ebp
negl %ebp
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r14, %rdi
callq 0x8824
cmpl 0x14(%rsp), %ebp
jge 0xe71b
incl 0x18(%rsp)
cmpl %ebp, %r13d
jge 0xe6da
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 0xb40(%rsp), %rdx
movl %ecx, (%rdx)
imulq $0xc, %rax, %rdx
movq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4290
jmp 0xe6dd
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r14, %rdi
callq 0x8824
movl %r13d, %ebp
addq $0xc, %r15
movl %ebp, %r13d
jmp 0xe61c
cmpl $0x0, 0x18(%rsp)
jne 0xe720
movq 0x160(%r14), %rax
movq 0x30(%rsp), %rcx
addl $0xffc2f700, %ecx       # imm = 0xFFC2F700
cmpl $0x0, -0x14(%rax)
cmovel 0x10(%rsp), %ecx
movq 0xb40(%rsp), %rax
andl $0x0, (%rax)
movl %ecx, %r13d
jmp 0xe720
movl 0x14(%rsp), %r13d
movl %r13d, %ebx
movl %ebx, %eax
addq $0xb08, %rsp            # imm = 0xB08
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, (%rax)
movl %ebp, %ebx
jmp 0xe723