// File: 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
movq %rsp, %rbp
subq $0xb90, %rsp            # imm = 0xB90
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xb68(%rbp)
leaq -0x62c(%rbp), %rdi
callq 0xae50
movq -0x18(%rbp), %rdi
callq 0x13fd0
cmpl $0x3, %eax
jge 0x17c68
movq -0x18(%rbp), %rdi
callq 0x14080
cmpl $0x32, %eax
jl 0x17c74
movl $0x0, -0x4(%rbp)
jmp 0x18043
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x12400
movl %eax, -0x630(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x17cb3
movq -0xb68(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x20(%rbp), %edx
movl -0x24(%rbp), %ecx
movl -0x28(%rbp), %r8d
movq 0x10(%rbp), %r9
callq 0x18050
movl %eax, -0x4(%rbp)
jmp 0x18043
movq -0x18(%rbp), %rdi
callq 0xbc10
testb $0x1, %al
jne 0x17cf6
movl -0x630(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x17ce0
movq 0x10(%rbp), %rax
movl $0x0, (%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x18043
movl -0x20(%rbp), %eax
cmpl -0x630(%rbp), %eax
jge 0x17cf4
movl -0x630(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x17cf6
leaq -0xae0(%rbp), %rax
movq %rax, %rcx
addq $0x4b0, %rcx            # imm = 0x4B0
movq %rcx, -0xb78(%rbp)
movq %rax, -0xb70(%rbp)
movq -0xb70(%rbp), %rdi
movq %rdi, -0xb80(%rbp)
callq 0xa870
movq -0xb80(%rbp), %rax
movq -0xb78(%rbp), %rcx
addq $0xc, %rax
cmpq %rcx, %rax
movq %rax, -0xb70(%rbp)
jne 0x17d15
movq -0x18(%rbp), %rdi
leaq -0xae0(%rbp), %rsi
callq 0xfef0
movl %eax, -0xae4(%rbp)
movl $0x0, -0xae8(%rbp)
movq -0x18(%rbp), %rdi
leaq -0xae0(%rbp), %rsi
movl -0xae4(%rbp), %edx
leaq 0x24f0a(%rip), %rax     # 0x3cc88
movq (%rax), %rcx
movq %rcx, -0xaf4(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xaec(%rbp)
movq -0xaf4(%rbp), %rax
movq %rax, -0xb04(%rbp)
movl -0xaec(%rbp), %eax
movl %eax, -0xafc(%rbp)
movq -0xb04(%rbp), %rcx
movl -0xafc(%rbp), %r8d
callq 0x11b50
movl %eax, -0xae4(%rbp)
movl $0x0, -0xb08(%rbp)
movl -0xb08(%rbp), %eax
cmpl -0xae4(%rbp), %eax
jge 0x17ff9
movq -0x18(%rbp), %rdi
movslq -0xb08(%rbp), %rcx
leaq -0xae0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0xb14(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xb0c(%rbp)
movq -0xb14(%rbp), %rax
movq %rax, -0xb24(%rbp)
movl -0xb0c(%rbp), %eax
movl %eax, -0xb1c(%rbp)
movq -0xb24(%rbp), %rsi
movl -0xb1c(%rbp), %edx
callq 0xd190
testb $0x1, %al
jne 0x17e41
jmp 0x17f8b
movq -0xb68(%rbp), %rdi
movl -0xae8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xae8(%rbp)
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
subl $0x1, %edx
xorl %ecx, %ecx
subl -0x24(%rbp), %ecx
xorl %r8d, %r8d
subl -0x20(%rbp), %r8d
movl -0x28(%rbp), %r9d
addl $0x1, %r9d
leaq -0x62c(%rbp), %rax
movq %rax, (%rsp)
callq 0x17c10
movl %eax, %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, -0xb28(%rbp)
movq -0x18(%rbp), %rdi
movslq -0xb08(%rbp), %rcx
leaq -0xae0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0xb34(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xb2c(%rbp)
movq -0xb34(%rbp), %rax
movq %rax, -0xb44(%rbp)
movl -0xb2c(%rbp), %eax
movl %eax, -0xb3c(%rbp)
movq -0xb44(%rbp), %rsi
movl -0xb3c(%rbp), %edx
callq 0xdd20
cmpl $0x80000000, -0xb28(%rbp) # imm = 0x80000000
jne 0x17f03
movl -0xb28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x18043
movl -0xb28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jl 0x17f19
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x18043
movl -0xb28(%rbp), %eax
cmpl -0x20(%rbp), %eax
jle 0x17f89
movl -0xb28(%rbp), %eax
movl %eax, -0x20(%rbp)
movslq -0xb08(%rbp), %rax
leaq -0xae0(%rbp), %rcx
imulq $0xc, %rax, %rax
addq %rax, %rcx
movq 0x10(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, 0x4(%rax)
movl 0x8(%rcx), %ecx
movl %ecx, 0xc(%rax)
movl -0x62c(%rbp), %ecx
addl $0x1, %ecx
movq 0x10(%rbp), %rax
movl %ecx, (%rax)
movq 0x10(%rbp), %rdi
addq $0x4, %rdi
addq $0xc, %rdi
leaq -0x62c(%rbp), %rsi
addq $0x4, %rsi
movslq -0x62c(%rbp), %rax
imulq $0xc, %rax, %rdx
callq 0x5360
jmp 0x17fe3
movq -0x18(%rbp), %rdi
movslq -0xb08(%rbp), %rcx
leaq -0xae0(%rbp), %rax
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, -0xb50(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0xb48(%rbp)
movq -0xb50(%rbp), %rax
movq %rax, -0xb60(%rbp)
movl -0xb48(%rbp), %eax
movl %eax, -0xb58(%rbp)
movq -0xb60(%rbp), %rsi
movl -0xb58(%rbp), %edx
callq 0xdd20
jmp 0x17fe5
movl -0xb08(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb08(%rbp)
jmp 0x17dce
cmpl $0x0, -0xae8(%rbp)
jne 0x1803d
movq -0x18(%rbp), %rdi
callq 0xbc10
testb $0x1, %al
jne 0x18011
jmp 0x18028
movq 0x10(%rbp), %rax
movl $0x0, (%rax)
movl -0x28(%rbp), %eax
addl $0xffc2f700, %eax       # imm = 0xFFC2F700
movl %eax, -0x4(%rbp)
jmp 0x18043
movq 0x10(%rbp), %rax
movl $0x0, (%rax)
movl -0x630(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x18043
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb90, %rsp            # imm = 0xB90
popq %rbp
retq
nop