// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb28, %rsp            # imm = 0xB28
movl %r9d, %r15d
movl %r8d, 0x10(%rsp)
movl %ecx, %r12d
movl %edx, 0x1c(%rsp)
movq %rsi, %r14
movq %rdi, %rbp
leaq 0x6c(%rsp), %rdi
callq 0x7a1c
movq %r14, %rdi
callq 0xedba
xorl %ebx, %ebx
cmpl $0x2, %eax
jg 0x11258
movq %r14, %rdi
callq 0xee0c
cmpl $0x31, %eax
jg 0x11258
movq 0xb60(%rsp), %r13
movq %r14, %rdi
xorl %esi, %esi
callq 0xd30c
cmpl $0x0, 0x1c(%rsp)
je 0x11201
movq 0x160(%r14), %rcx
cmpl $0x0, -0x14(%rcx)
jne 0x11031
movl 0x10(%rsp), %ebx
cmpl %ebx, %eax
jge 0x1126c
cmpl %r12d, %eax
cmovgl %eax, %r12d
movq %rbp, 0x58(%rsp)
movl %eax, 0x2c(%rsp)
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x670, %rdi            # imm = 0x670
callq 0x7648
addq $0xc, %rbx
cmpq $0x4b0, %rbx            # imm = 0x4B0
jne 0x1103c
leaq 0x670(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xb606
leaq 0x19b25(%rip), %rdx     # 0x2ab98
movq (%rdx), %rcx
movl 0x8(%rdx), %r8d
movq %r14, %rdi
movq %rbx, %rsi
movl %eax, %edx
callq 0xce28
xorl %r13d, %r13d
testl %eax, %eax
setg %bpl
jle 0x1121d
movl %r12d, 0x14(%rsp)
decl 0x1c(%rsp)
movl 0x10(%rsp), %ecx
negl %ecx
movl %ecx, 0x34(%rsp)
movq %r15, 0x38(%rsp)
leal 0x1(%r15), %ecx
movl %ecx, 0x30(%rsp)
movq 0xb60(%rsp), %rcx
leaq 0x4(%rcx), %rdx
movq %rdx, 0x48(%rsp)
addq $0x10, %rcx
movq %rcx, 0x40(%rsp)
movl %eax, %r15d
movq %r15, %rax
negq %rax
movq %rax, 0x60(%rsp)
movl $0x1, %r12d
movq %r14, 0x20(%rsp)
movq %r15, 0x50(%rsp)
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r14, %rdi
callq 0x947c
testb %al, %al
je 0x111b9
movl %r13d, 0x18(%rsp)
movl 0x14(%rsp), %r13d
movl %r13d, %r8d
negl %r8d
leaq 0x6c(%rsp), %rax
movq %rax, (%rsp)
movq 0x58(%rsp), %rdi
movq %r14, %rsi
movl 0x1c(%rsp), %edx
movl 0x34(%rsp), %ecx
movl 0x30(%rsp), %r9d
callq 0x10fa0
movl %eax, %r15d
negl %r15d
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r14, %rdi
callq 0x9a5e
movl 0x10(%rsp), %eax
cmpl %eax, %r15d
jge 0x1119c
cmpl %r15d, %r13d
jge 0x1119c
movl 0x8(%rbx), %eax
movq 0x48(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rbx), %rax
movq %rax, (%rcx)
movslq 0x6c(%rsp), %rax
leal 0x1(%rax), %ecx
movq 0xb60(%rsp), %rdx
movl %ecx, (%rdx)
shlq $0x2, %rax
leaq (%rax,%rax,2), %rdx
movq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4230
movl 0x10(%rsp), %eax
movl %r15d, 0x14(%rsp)
movq 0x20(%rsp), %r14
movl 0x18(%rsp), %r13d
incl %r13d
movl %eax, 0x10(%rsp)
cmpl %eax, %r15d
movq 0x50(%rsp), %r15
jl 0x111c7
jmp 0x111ea
movq (%rbx), %rsi
movl 0x8(%rbx), %edx
movq %r14, %rdi
callq 0x9a5e
cmpq %r15, %r12
setb %bpl
addq $0xc, %rbx
movq 0x60(%rsp), %rax
addq %r12, %rax
incq %rax
incq %r12
cmpq $0x1, %rax
jne 0x110ee
testl %r13d, %r13d
setne %r13b
movq 0x38(%rsp), %r15
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %r12d
jmp 0x1121d
movq %rbp, %rdi
movq %r14, %rsi
movl %r12d, %edx
movl 0x10(%rsp), %ecx
movl %r15d, %r8d
movq %r13, %r9
callq 0x11276
movl %eax, %ebx
jmp 0x11258
testb $0x1, %bpl
cmovnel %eax, %r12d
jne 0x11255
testb %r13b, %r13b
jne 0x11255
movq 0x160(%r14), %rax
addl $0xffc2f700, %r15d      # imm = 0xFFC2F700
cmpl $0x0, -0x14(%rax)
movq 0xb60(%rsp), %rax
movl $0x0, (%rax)
cmovel 0x2c(%rsp), %r15d
movl %r15d, %r12d
movl %r12d, %ebx
movl %ebx, %eax
addq $0xb28, %rsp            # imm = 0xB28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, (%r13)
jmp 0x11258