// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movl %r8d, %r14d
movl %ecx, 0xc(%rsp)
movl %edx, %ebx
movq %rsi, %r12
movq %rdi, %r15
leaq 0x4c(%rsp), %rdi
callq 0x700c
movq %r12, %rdi
callq 0xc994
xorl %ebp, %ebp
cmpl $0x2, %eax
jg 0xe994
movq %r12, %rdi
callq 0xc9d6
cmpl $0x31, %eax
jg 0xe994
movq %r12, %rdi
xorl %esi, %esi
callq 0xb5f4
movq 0x160(%r12), %rcx
cmpl $0x0, -0x14(%rcx)
je 0xe7b4
movl %eax, 0x10(%rsp)
movq %r14, 0x18(%rsp)
jmp 0xe7cc
cmpl 0xc(%rsp), %eax
jge 0xe953
movq %r14, 0x18(%rsp)
cmpl %ebx, %eax
movl %eax, 0x10(%rsp)
cmovgl %eax, %ebx
movq %r15, 0x30(%rsp)
xorl %r14d, %r14d
leaq (%rsp,%r14), %rdi
addq $0x650, %rdi            # imm = 0x650
callq 0x6d44
addq $0xc, %r14
cmpq $0x4b0, %r14            # imm = 0x4B0
jne 0xe7d4
leaq 0x650(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0xa746
leaq 0x183bd(%rip), %rdx     # 0x26bc8
movq (%rdx), %rcx
movl 0x8(%rdx), %r8d
movq %r12, %rdi
movq %r15, %rsi
movl %eax, %edx
callq 0xb1c0
movl 0xc(%rsp), %ecx
negl %ecx
movl %ecx, 0x2c(%rsp)
movq 0x18(%rsp), %rcx
incl %ecx
movl %ecx, 0x28(%rsp)
movq 0x20(%rsp), %rcx
leaq 0x4(%rcx), %rdx
movq %rdx, 0x40(%rsp)
addq $0x10, %rcx
movq %rcx, 0x38(%rsp)
xorl %r14d, %r14d
testl %eax, %eax
cmovlel %r14d, %eax
imulq $0xc, %rax, %r15
movl $0x0, 0x14(%rsp)
cmpq %r14, %r15
je 0xe921
leaq (%rsp,%r14), %r13
addq $0x650, %r13            # imm = 0x650
movq (%r13), %rsi
movl 0x8(%r13), %edx
movq %r12, %rdi
callq 0x8258
testb %al, %al
je 0xe904
movl %ebx, %ecx
negl %ecx
movq 0x30(%rsp), %rdi
movq %r12, %rsi
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %r8d
leaq 0x4c(%rsp), %r9
callq 0xe73e
movl %eax, %ebp
negl %ebp
movq (%r13), %rsi
movl 0x8(%r13), %edx
movq %r12, %rdi
callq 0x8824
cmpl 0xc(%rsp), %ebp
jge 0xe94d
incl 0x14(%rsp)
cmpl %ebp, %ebx
jge 0xe914
movl 0x8(%r13), %eax
movq 0x40(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%r13), %rax
movq %rax, (%rcx)
movslq 0x4c(%rsp), %rax
leal 0x1(%rax), %ecx
movq 0x20(%rsp), %rdx
movl %ecx, (%rdx)
imulq $0xc, %rax, %rdx
movq 0x38(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4290
jmp 0xe916
movq (%r13), %rsi
movl 0x8(%r13), %edx
movq %r12, %rdi
callq 0x8824
movl %ebx, %ebp
addq $0xc, %r14
movl %ebp, %ebx
jmp 0xe860
cmpl $0x0, 0x14(%rsp)
jne 0xe992
movq 0x160(%r12), %rax
movl -0x14(%rax), %eax
movq 0x20(%rsp), %rcx
andl $0x0, (%rcx)
testl %eax, %eax
je 0xe974
movq 0x18(%rsp), %rax
addl $0xffc2f700, %eax       # imm = 0xFFC2F700
movl %eax, %ebx
jmp 0xe992
movl 0xc(%rsp), %ebx
jmp 0xe992
movq 0x20(%rsp), %rax
andl $0x0, (%rax)
movl 0x81e4(%r15), %eax
cmpl %r14d, %eax
jge 0xe96e
xchgl %r14d, 0x81e4(%r15)
movl 0xc(%rsp), %ebp
jmp 0xe994
movq 0x30(%rsp), %rdx
movl 0x81e4(%rdx), %eax
movq 0x18(%rsp), %rcx
cmpl %ecx, %eax
jge 0xe98e
xchgl %ecx, 0x81e4(%rdx)
movl 0x10(%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