// File: /workspace/github_cpp_makefile_star2_O2/codeandkey[P]neocortex/src/search.cpp
// Function: neocortex::search::Search::control_worker(neocortex::Position, std::function<void (neocortex::search::SearchInfo)>, std::function<void (neocortex::Move)>)
void search::Search::control_worker(Position root, std::function<void(SearchInfo)> info, std::function<void(Move)> bestmove) {
	int score;
	int cur_depth;
	int ourtime, ourinc;
	int iter_time;
	Move best_move;

	/* EBF variables, for depth time prediction */
	float ebf = -1.0f; /* effective branching factor, set after depth 3 */
	int ebf_nodes[MAX_DEPTH] = { 1 };
	int ebf_times[MAX_DEPTH] = { 0 }; /* we can assume depth 0 and 1 searches take effectively 1ms */

	/* Determine how much time we have */
	if (root.get_color_to_move() == piece::WHITE) {
		ourtime = wtime;
		ourinc = winc;
	} else {
		ourtime = btime;
		ourinc = binc;
	}

	/* Write depth 0 information */
	SearchInfo d0;

	d0.depth = 0;
	d0.nodes = 1;
	d0.score = root.evaluate();
	d0.seldepth = -1;

	info(d0);

	/* Search depth 1 with 1 thread */
	{
		std::lock_guard<std::mutex> dst_lock(depth_starttime_mutex);
		depth_starttime = util::time_now();
	}

	allocated_time = -1;

	SearchInfo d1;

	root.reset_eval_counter();
	max_ply_searched = 0;

	score = alphabeta(root, 1, score::CHECKMATED, score::CHECKMATE, 0, &d1.pv, control_should_stop);
	
	{
		std::lock_guard<std::mutex> dst_lock(depth_starttime_mutex);
		iter_time = util::time_elapsed_ms(depth_starttime);
	}

	d1.depth = 1;
	d1.nodes = root.get_eval_counter();
	d1.seldepth = max_ply_searched;
	d1.time = iter_time;
	d1.score = score;
	best_move = d1.pv.moves[0];

	info(d1);

	/* Set search start time */
	util::time_point search_starttime = util::time_now();

	cur_depth = 2;
	while (1) {
		if (!infinite && depth > 0 && cur_depth > depth) break;
		if (!infinite && movetime > 0 && util::time_elapsed_ms(search_starttime) >= movetime) break;
		if (control_should_stop) break;
		if (cur_depth >= MAX_DEPTH) break;

		/* Allocate time for the next depth if timed */
		allocated_time = -1;

		if (movetime > 0) {
			allocated_time = movetime.load();
		} else {
			if (ourtime > 0) {
				allocated_time = ourtime / search::ALLOC_FRACTION; /* TODO: actual time management */

				if (ourinc > 0) {
					allocated_time += ourinc;
				}
			}
		}

		/* Predict time for this depth */
		if (ebf > 0.0f) {
			if (allocated_time > 0) {
				if (util::time_elapsed_ms(search_starttime) + (int)(ebf * ebf_times[cur_depth - 1]) >= allocated_time) {
					break;
				}
			}
		}

		root.reset_eval_counter();

		/* Perform search (TODO: aspiration) */
		{
			std::lock_guard<std::mutex> dst_lock(depth_starttime_mutex);
			depth_starttime = util::time_now();
		}

		max_ply_searched = 0;

		/* Start n-1 SMP threads */
		smp_should_stop = false;

		int* smp_node_counts = new int[(size_t) num_threads - 1];

		for (int i = 0; i < num_threads - 1; ++i) {
			smp_node_counts[i] = 0;
			smp_threads.push_back(std::thread([=] { smp_worker(cur_depth + (i % 2), root, smp_node_counts + i); }));
		}

		/* Search on control thread */

		SearchInfo cur;
		cur.depth = cur_depth;

		cur.score = alphabeta(root, cur_depth, score::CHECKMATED, score::CHECKMATE, 0, &cur.pv, control_should_stop);

		cur.nodes = root.get_eval_counter();

		/* Join SMP threads */
		smp_should_stop = true;

		for (auto &i : smp_threads) {
			if (i.joinable()) {
				i.join();
			}
		}

		for (int i = 0; i < num_threads - 1; ++i) {
			cur.nodes += smp_node_counts[i];
		}

		smp_threads.clear();

		if (cur.score == score::INCOMPLETE) break;

		/* Search complete, store best move */
		best_move = cur.pv.moves[0];
		cur.seldepth = max_ply_searched;

		{
			std::lock_guard<std::mutex> dst_lock(depth_starttime_mutex);
			cur.time = util::time_elapsed_ms(depth_starttime);
		}

		/* Set EBF for next depth */
		ebf_nodes[cur_depth] = cur.nodes;
		ebf_times[cur_depth] = cur.time;
		ebf = sqrtf((float) ebf_nodes[cur_depth] / (float) ebf_nodes[cur_depth - 1]);

		/* Write depth result to uci */

		info(cur);
		++cur_depth;
	}

	/* Write bestmove */
	bestmove(best_move);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9858, %rsp           # imm = 0x9858
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
leaq 0x20(%rsp), %rdi
callq 0x6d44
leaq 0x890(%rsp), %rbx
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %esi, %esi
callq 0x41a0
movl $0x1, (%rbx)
leaq 0x690(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x41a0
xorl %eax, %eax
cmpl $0x0, 0x170(%r13)
setne %al
movl 0x81e8(%r12,%rax,4), %ecx
movl %ecx, 0x1c(%rsp)
movl 0x81f0(%r12,%rax,4), %eax
movl %eax, 0x18(%rsp)
leaq 0x10b0(%rsp), %rbx
movq %rbx, %rdi
callq 0xec88
movq $0x1, (%rbx)
movq %r13, %rdi
xorl %esi, %esi
callq 0xb5f4
movl %eax, 0x8(%rbx)
orl $-0x1, 0x10(%rbx)
leaq 0x16c8(%rsp), %rdi
movl $0x618, %edx            # imm = 0x618
movq %rbx, %rsi
callq 0x4290
cmpq $0x0, 0x10(%r15)
je 0xddaa
leaq 0x16c8(%rsp), %rsi
movq %r15, %rdi
movq %r15, 0x48(%rsp)
callq *0x18(%r15)
leaq 0x81b0(%r12), %r15
movq %r15, %rdi
callq 0xf116
callq 0x11ecc
movq %rax, 0x81d8(%r12)
movq %r15, %rdi
callq 0x4260
pushq $-0x1
popq %rax
xchgl %eax, 0x81e0(%r12)
leaq 0xa98(%rsp), %rdi
callq 0xec88
movq %r13, %rdi
callq 0xce34
xorl %eax, %eax
xchgl %eax, 0x81e4(%r12)
leaq 0xaac(%rsp), %rax
leaq 0x81a8(%r12), %r10
pushq $0x1
popq %rdx
movq %r12, %rdi
movq %r13, %rsi
movl $0xffc2f700, %ecx       # imm = 0xFFC2F700
movl $0x3d0900, %r8d         # imm = 0x3D0900
xorl %r9d, %r9d
movq %r10, 0x50(%rsp)
pushq %r10
pushq %rax
callq 0xde0e
popq %rcx
popq %rdx
movl %eax, %ebx
movq %r15, %rdi
callq 0xf116
movq 0x81d8(%r12), %rdi
callq 0x11ef6
movl %eax, %ebp
movq %r14, 0x70(%rsp)
movq %r15, 0x30(%rsp)
movq %r15, %rdi
callq 0x4260
leaq 0xa98(%rsp), %r14
movl $0x1, 0x4(%r14)
movq %r13, %rdi
callq 0xce3c
movl %eax, (%r14)
movl 0x81e4(%r12), %eax
movl %eax, 0x10(%r14)
movl %ebp, 0xc(%r14)
movl %ebx, 0x8(%r14)
movq 0x18(%r14), %rax
movq %rax, 0x20(%rsp)
movl 0x20(%r14), %eax
movl %eax, 0x28(%rsp)
leaq 0x16c8(%rsp), %rdi
movl $0x618, %edx            # imm = 0x618
movq %r14, %rsi
callq 0x4290
movq 0x48(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0xddaf
leaq 0x16c8(%rsp), %rsi
callq *0x18(%rdi)
callq 0x11ecc
movq %rax, 0x40(%rsp)
pushq $0xa
popq %rcx
movl 0x1c(%rsp), %eax
xorl %edx, %edx
divl %ecx
movl %eax, 0x3c(%rsp)
leaq 0x1830(%rsp), %rbx
leaq 0x8180(%r12), %rax
movq %rax, 0x58(%rsp)
movss 0x850b(%rip), %xmm0     # 0x15df0
movss %xmm0, 0xc(%rsp)
pushq $0x2
popq %rax
movq %rax, 0x10(%rsp)
leaq 0x68(%rsp), %r15
movq %r13, 0x60(%rsp)
movq 0x30(%rsp), %rbp
movq %r12, (%rsp)
movb 0x8200(%r12), %al
testb $0x1, %al
jne 0xd933
movl 0x81fc(%r12), %eax
testl %eax, %eax
jle 0xd933
movl 0x81fc(%r12), %eax
cltq
cmpq %rax, 0x10(%rsp)
jg 0xdd80
movb 0x8200(%r12), %al
testb $0x1, %al
jne 0xd969
movl 0x81f8(%r12), %eax
testl %eax, %eax
jle 0xd969
movq 0x40(%rsp), %rdi
callq 0x11ef6
movq (%rsp), %r12
movl 0x81f8(%r12), %ecx
cmpl %ecx, %eax
jge 0xdd80
movq 0x50(%rsp), %rax
movb (%rax), %al
testb $0x1, %al
jne 0xdd80
cmpq $0x7f, 0x10(%rsp)
ja 0xdd80
pushq $-0x1
popq %rax
xchgl %eax, 0x81e0(%r12)
movl 0x81f8(%r12), %eax
testl %eax, %eax
jle 0xdd4c
movl 0x81f8(%r12), %eax
xchgl %eax, 0x81e0(%r12)
movss 0xc(%rsp), %xmm0
ucomiss 0x8438(%rip), %xmm0     # 0x15df4
jbe 0xda04
movl 0x81e0(%r12), %eax
testl %eax, %eax
jle 0xda04
movq 0x40(%rsp), %rdi
callq 0x11ef6
movq 0x10(%rsp), %rcx
cvtsi2ssl 0x68c(%rsp,%rcx,4), %xmm0
movss 0xc(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ecx
addl %eax, %ecx
movq (%rsp), %rax
movl 0x81e0(%rax), %eax
cmpl %eax, %ecx
jge 0xdd80
movq %r13, %rdi
callq 0xce34
movq %rbp, %rdi
callq 0xf116
callq 0x11ecc
movq (%rsp), %r14
movq %rax, 0x81d8(%r14)
movq %rbp, %rdi
callq 0x4260
xorl %eax, %eax
xchgl %eax, 0x81e4(%r14)
xorl %eax, %eax
xchgb %al, 0x8198(%r14)
movl 0x8178(%r14), %eax
cltq
leaq -0x1(%rax), %rcx
shrq $0x3e, %rcx
leaq -0x4(,%rax,4), %rax
negq %rcx
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rax, %rdi
callq 0x4030
movq %rax, %rbp
xorl %r14d, %r14d
movq (%rsp), %rcx
movl 0x8178(%rcx), %eax
cltq
decq %rax
cmpq %rax, %r14
jge 0xdba9
movq %r15, %r12
movq %rbx, %r15
andl $0x0, (%rbp,%r14,4)
movq %rcx, 0x16c8(%rsp)
movq 0x10(%rsp), %rax
movl %eax, 0x16d0(%rsp)
movl %r14d, 0x16d4(%rsp)
leaq 0x16d8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xeb68
movq %rbp, 0x9850(%rsp)
andq $0x0, 0x68(%rsp)
movl $0x8198, %edi           # imm = 0x8198
callq 0x4310
movq %rax, %r13
leaq 0xf1e3(%rip), %rax      # 0x1ccc8
movq %rax, (%r13)
movups 0x16c8(%rsp), %xmm0
movups %xmm0, 0x8(%r13)
leaq 0x18(%r13), %rdi
movl $0x158, %edx            # imm = 0x158
movq %rbx, %rsi
callq 0x4290
movups 0x1830(%rsp), %xmm0
movups %xmm0, 0x170(%r13)
movq 0x1840(%rsp), %rax
movq %r15, %rbx
andq $0x0, 0x10(%r15)
movq %rax, 0x180(%r13)
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq %r13, %rdi
addq $0x188, %rdi            # imm = 0x188
movl $0x8008, %edx           # imm = 0x8008
leaq 0x1848(%rsp), %rsi
callq 0x4290
movq %rbp, 0x8190(%r13)
movq %r13, 0x78(%rsp)
movq %r12, %r15
movq %r12, %rdi
leaq 0x78(%rsp), %rsi
xorl %edx, %edx
callq 0x42d0
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0xdb7f
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0xf12a
movq 0x60(%rsp), %r13
movq %r15, %rdi
callq 0xebb2
movq %rbx, %rdi
callq 0xd174
incq %r14
jmp 0xda70
leaq 0x78(%rsp), %rdi
callq 0xec88
movq 0x10(%rsp), %rdx
movl %edx, 0x7c(%rsp)
movq (%rsp), %rdi
movq %r13, %rsi
movl $0xffc2f700, %ecx       # imm = 0xFFC2F700
movl $0x3d0900, %r8d         # imm = 0x3D0900
xorl %r9d, %r9d
pushq 0x50(%rsp)
leaq 0x94(%rsp), %rax
pushq %rax
callq 0xde0e
popq %rcx
popq %rdx
movl %eax, 0x80(%rsp)
movq %r13, %rdi
callq 0xce3c
movq (%rsp), %rdx
movl %eax, 0x78(%rsp)
movb $0x1, %al
xchgb %al, 0x8198(%rdx)
movq 0x8180(%rdx), %r13
movq 0x8188(%rdx), %r14
cmpq %r14, %r13
je 0xdc30
cmpq $0x0, (%r13)
je 0xdc2a
movq %r13, %rdi
callq 0x4510
movq (%rsp), %rdx
addq $0x8, %r13
jmp 0xdc12
xorl %eax, %eax
movq 0x60(%rsp), %r13
movl 0x8178(%rdx), %ecx
movslq %ecx, %rcx
decq %rcx
cmpq %rcx, %rax
jge 0xdc55
movl (%rbp,%rax,4), %ecx
addl %ecx, 0x78(%rsp)
incq %rax
jmp 0xdc37
movq 0x58(%rsp), %rdi
callq 0xec9e
xorl %eax, %eax
cmpl 0x80(%rsp), %eax
jo 0xdd80
leaq 0x8c(%rsp), %rcx
movl 0xc(%rcx), %eax
movl %eax, 0x28(%rsp)
movq 0x4(%rcx), %rax
movq %rax, 0x20(%rsp)
movq (%rsp), %r14
movl 0x81e4(%r14), %eax
movl %eax, 0x88(%rsp)
movq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0xf116
movq 0x81d8(%r14), %rdi
callq 0x11ef6
movl %eax, 0x84(%rsp)
movq %r12, %rdi
callq 0x4260
movl 0x78(%rsp), %eax
movl 0x84(%rsp), %ecx
cvtsi2ss %eax, %xmm0
movq 0x10(%rsp), %rdx
movl %eax, 0x890(%rsp,%rdx,4)
cvtsi2ssl 0x88c(%rsp,%rdx,4), %xmm1
movl %ecx, 0x690(%rsp,%rdx,4)
divss %xmm1, %xmm0
ucomiss 0x80fe(%rip), %xmm0     # 0x15df4
movq 0x48(%rsp), %r14
jb 0xdd03
sqrtss %xmm0, %xmm0
jmp 0xdd08
callq 0x4580
movss %xmm0, 0xc(%rsp)
leaq 0x16c8(%rsp), %rdi
movl $0x618, %edx            # imm = 0x618
movq %rdi, %rbp
leaq 0x78(%rsp), %rsi
callq 0x4290
cmpq $0x0, 0x10(%r14)
je 0xdda5
movq %r14, %rdi
movq %rbp, %rsi
callq *0x18(%r14)
incq 0x10(%rsp)
movq (%rsp), %r12
movq 0x30(%rsp), %rbp
jmp 0xd906
cmpl $0x0, 0x1c(%rsp)
jle 0xd9af
movl 0x3c(%rsp), %eax
xchgl %eax, 0x81e0(%r12)
cmpl $0x0, 0x18(%rsp)
jle 0xd9af
movl 0x18(%rsp), %eax
lock
addl %eax, 0x81e0(%r12)
jmp 0xd9af
movq 0x20(%rsp), %rsi
movl 0x28(%rsp), %edx
movq 0x70(%rsp), %rdi
callq 0xeca6
addq $0x9858, %rsp           # imm = 0x9858
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x40d0
callq 0x40d0
callq 0x40d0
jmp 0xddb6
movq %rax, %r14
movq %r15, %rdi
jmp 0xddcc
movq %rax, %r14
movq %r12, %rdi
jmp 0xddcc
movq %rax, %r14
movq %rbp, %rdi
callq 0x4260
jmp 0xde05
movq %rax, %r14
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0xddfd
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0xddfd
movq %rax, %r14
leaq 0x68(%rsp), %rdi
callq 0xebb2
jmp 0xddfd
movq %rax, %r14
movq %r15, %rbx
movq %rbx, %rdi
callq 0xd174
movq %r14, %rdi
callq 0x4550
nop