// File: /workspace/github_cpp_makefile_star2_O3/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 $0x9878, %rsp           # imm = 0x9878
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
leaq 0x28(%rsp), %rdi
callq 0x7758
leaq 0x8b0(%rsp), %rbx
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rdi
xorl %esi, %esi
callq 0x4150
movl $0x1, (%rbx)
leaq 0x6b0(%rsp), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x4150
xorl %eax, %eax
cmpl $0x0, 0x170(%r13)
setne %al
movl 0x81e8(%r12,%rax,4), %ecx
movl %ecx, 0x24(%rsp)
movl 0x81f0(%r12,%rax,4), %eax
movl %eax, 0x20(%rsp)
leaq 0x10e4(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x14(%rbx)
movl $0x0, -0x4(%rbx)
movq %rbx, %rdi
callq 0x7b28
movq $0x1, -0x14(%rbx)
movq %r13, %rdi
xorl %esi, %esi
callq 0xd36e
movl %eax, -0xc(%rbx)
movl $0xffffffff, -0x4(%rbx) # imm = 0xFFFFFFFF
leaq 0x16e8(%rsp), %rdi
leaq 0x10d0(%rsp), %rsi
movl $0x618, %edx            # imm = 0x618
callq 0x4230
cmpq $0x0, 0x10(%r15)
je 0x10ade
leaq 0x16e8(%rsp), %rsi
movq %r15, %rdi
callq *0x18(%r15)
leaq 0x81b0(%r12), %rdi
movq %rdi, 0x8(%rsp)
callq 0x4400
testl %eax, %eax
jne 0x10ae3
movq %r14, 0x58(%rsp)
callq 0x15fa3
movq %rax, 0x81d8(%r12)
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x4200
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xchgl %eax, 0x81e0(%r12)
leaq 0xac4(%rsp), %rbx
xorl %ebp, %ebp
movl %ebp, -0x4(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, -0x14(%rbx)
movq %rbx, %rdi
callq 0x7b28
movq %r13, %rdi
callq 0xfb92
xchgl %ebp, 0x81e4(%r12)
leaq 0x81a8(%r12), %rax
movq %r12, %rdi
movq %r13, %rsi
movl $0x1, %edx
movl $0xffc2f700, %ecx       # imm = 0xFFC2F700
movl $0x3d0900, %r8d         # imm = 0x3D0900
xorl %r9d, %r9d
movq %rax, 0x48(%rsp)
pushq %rax
pushq %rbx
callq 0x10b54
addq $0x10, %rsp
movl %eax, %ebx
movq %r14, %rdi
callq 0x4400
testl %eax, %eax
jne 0x10ae5
movq 0x81d8(%r12), %rdi
callq 0x15fde
movl %eax, %ebp
movq 0x8(%rsp), %rdi
callq 0x4200
leaq 0xab0(%rsp), %r14
movl $0x1, 0x4(%r14)
movq %r13, %rdi
callq 0xfb9e
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, 0x28(%rsp)
movl 0x20(%r14), %eax
movl %eax, 0x30(%rsp)
leaq 0x16e8(%rsp), %rdi
movl $0x618, %edx            # imm = 0x618
movq %r14, %rsi
callq 0x4230
cmpq $0x0, 0x10(%r15)
je 0x10ae7
leaq 0x170(%r13), %rbx
leaq 0x16e8(%rsp), %rsi
movq %r15, %rdi
callq *0x18(%r15)
callq 0x15fa3
movq %rax, 0x40(%rsp)
movl 0x24(%rsp), %eax
movl $0xcccccccd, %ecx       # imm = 0xCCCCCCCD
imulq %rax, %rcx
shrq $0x23, %rcx
movq %rcx, 0x60(%rsp)
leaq 0x158(%r13), %rax
movq %rax, 0x80(%rsp)
leaq 0x8180(%r12), %rax
movq %rax, 0x78(%rsp)
movss 0xa925(%rip), %xmm0     # 0x1ae40
movss %xmm0, 0x14(%rsp)
movl $0x2, %eax
movq %rax, 0x18(%rsp)
movq %r13, 0x50(%rsp)
movq %r15, 0x68(%rsp)
movq %rbx, 0x88(%rsp)
movq %r12, 0x70(%rsp)
movb 0x8200(%r12), %al
testb $0x1, %al
jne 0x1056f
movl 0x81fc(%r12), %eax
testl %eax, %eax
jle 0x1056f
movl 0x81fc(%r12), %eax
cltq
cmpq %rax, 0x18(%rsp)
jg 0x10a85
movb 0x8200(%r12), %al
testb $0x1, %al
jne 0x105a1
movl 0x81f8(%r12), %eax
testl %eax, %eax
jle 0x105a1
movq 0x40(%rsp), %rdi
callq 0x15fde
movl 0x81f8(%r12), %ecx
cmpl %ecx, %eax
jge 0x10a85
movq 0x48(%rsp), %rax
movb (%rax), %al
testb $0x1, %al
jne 0x10a85
cmpq $0x7f, 0x18(%rsp)
ja 0x10a85
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xchgl %eax, 0x81e0(%r12)
movl 0x81f8(%r12), %eax
testl %eax, %eax
jle 0x10a50
movl 0x81f8(%r12), %eax
xchgl %eax, 0x81e0(%r12)
movss 0x14(%rsp), %xmm0
ucomiss 0xa84e(%rip), %xmm0     # 0x1ae44
jbe 0x1063f
movl 0x81e0(%r12), %eax
testl %eax, %eax
jle 0x1063f
movq 0x40(%rsp), %rdi
callq 0x15fde
movq 0x18(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2ssl 0x6ac(%rsp,%rcx,4), %xmm0
movss 0x14(%rsp), %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %ecx
addl %eax, %ecx
movl 0x81e0(%r12), %eax
cmpl %eax, %ecx
jge 0x10a85
movq %r13, %rdi
callq 0xfb92
movq 0x8(%rsp), %rdi
callq 0x4400
testl %eax, %eax
jne 0x10ad0
callq 0x15fa3
movq %rax, 0x81d8(%r12)
movq 0x8(%rsp), %rdi
callq 0x4200
xorl %eax, %eax
xchgl %eax, 0x81e4(%r12)
xorl %eax, %eax
xchgb %al, 0x8198(%r12)
movl 0x8178(%r12), %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, %r15
movl 0x8178(%r12), %eax
cmpl $0x2, %eax
jl 0x10857
xorl %r14d, %r14d
movl $0x0, (%r15,%r14,4)
movq %r12, 0x16e8(%rsp)
movq 0x18(%rsp), %rax
movl %eax, 0x16f0(%rsp)
movl %r14d, 0x16f4(%rsp)
movl $0x158, %edx            # imm = 0x158
leaq 0x16f8(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4230
leaq 0x1850(%rsp), %rbp
movq %rbp, %rdi
movq 0x80(%rsp), %rsi
callq 0x11dd8
movl $0x8008, %edx           # imm = 0x8008
leaq 0x1868(%rsp), %rdi
movq 0x88(%rsp), %r12
movq %r12, %rsi
callq 0x4230
movq %r15, 0x9870(%rsp)
movq $0x0, 0x38(%rsp)
movl $0x8198, %edi           # imm = 0x8198
callq 0x42a0
movq %rax, %r13
leaq 0x1156a(%rip), %rax     # 0x21cc8
movq %rax, (%r13)
movups 0x16e8(%rsp), %xmm0
movups %xmm0, 0x8(%r13)
leaq 0x18(%r13), %rdi
movl $0x158, %edx            # imm = 0x158
movq %rbx, %rsi
callq 0x4230
movups 0x1850(%rsp), %xmm0
movups %xmm0, 0x170(%r13)
movq 0x1860(%rsp), %rax
movq %rax, 0x180(%r13)
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
movq $0x0, 0x10(%rbp)
movq %r13, %rdi
addq $0x188, %rdi            # imm = 0x188
movl $0x8008, %edx           # imm = 0x8008
movq %r12, %rsi
callq 0x4230
movq %r15, 0x8190(%r13)
movq %r13, 0x90(%rsp)
leaq 0x38(%rsp), %r12
movq %r12, %rdi
leaq 0x90(%rsp), %rsi
xorl %edx, %edx
callq 0x4260
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x107fe
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x78(%rsp), %rdi
movq %r12, %rsi
callq 0x11fa6
movq 0x50(%rsp), %r13
movq 0x70(%rsp), %r12
cmpq $0x0, 0x38(%rsp)
jne 0x10acb
movq 0x1850(%rsp), %rdi
testq %rdi, %rdi
je 0x1083e
movq 0x1860(%rsp), %rsi
subq %rdi, %rsi
callq 0x42b0
incq %r14
movl 0x8178(%r12), %eax
cltq
decq %rax
cmpq %rax, %r14
jl 0x106c8
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movl $0x0, 0xa0(%rsp)
leaq 0xa4(%rsp), %rbx
movq %rbx, %rdi
callq 0x7b28
movq 0x18(%rsp), %rdx
movl %edx, 0x94(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movl $0xffc2f700, %ecx       # imm = 0xFFC2F700
movl $0x3d0900, %r8d         # imm = 0x3D0900
xorl %r9d, %r9d
pushq 0x48(%rsp)
pushq %rbx
callq 0x10b54
addq $0x10, %rsp
movl %eax, 0x98(%rsp)
movq %r13, %rdi
callq 0xfb9e
movl %eax, 0x90(%rsp)
movb $0x1, %al
xchgb %al, 0x8198(%r12)
movq 0x8180(%r12), %r13
movq 0x8188(%r12), %r14
cmpq %r14, %r13
je 0x108f5
cmpq $0x0, (%r13)
je 0x108ef
movq %r13, %rdi
callq 0x44b0
addq $0x8, %r13
jmp 0x108db
movl 0x8178(%r12), %eax
cmpl $0x2, %eax
jl 0x10925
xorl %eax, %eax
movl (%r15,%rax,4), %ecx
addl %ecx, 0x90(%rsp)
incq %rax
movl 0x8178(%r12), %ecx
movslq %ecx, %rcx
decq %rcx
cmpq %rcx, %rax
jl 0x10904
movq 0x8180(%r12), %rax
movq 0x8188(%r12), %rcx
movq %rax, %rdx
cmpq %rax, %rcx
movq 0x68(%rsp), %r15
movq 0x50(%rsp), %r13
leaq 0x16e8(%rsp), %r14
je 0x1096a
cmpq $0x0, (%rdx)
jne 0x10ac6
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x1094f
movq %rax, 0x8188(%r12)
xorl %eax, %eax
cmpl 0x98(%rsp), %eax
jo 0x10a85
leaq 0xa4(%rsp), %rcx
movl 0xc(%rcx), %eax
movl %eax, 0x30(%rsp)
movq 0x4(%rcx), %rax
movq %rax, 0x28(%rsp)
movl 0x81e4(%r12), %eax
movl %eax, 0xa0(%rsp)
movq 0x8(%rsp), %rdi
callq 0x4400
testl %eax, %eax
jne 0x10ad2
movq 0x81d8(%r12), %rdi
callq 0x15fde
movl %eax, 0x9c(%rsp)
movq 0x8(%rsp), %rdi
callq 0x4200
movl 0x90(%rsp), %eax
movl 0x9c(%rsp), %ecx
cvtsi2ss %eax, %xmm0
movq 0x18(%rsp), %rdx
movl %eax, 0x8b0(%rsp,%rdx,4)
cvtsi2ssl 0x8ac(%rsp,%rdx,4), %xmm1
movl %ecx, 0x6b0(%rsp,%rdx,4)
divss %xmm1, %xmm0
ucomiss 0xa43b(%rip), %xmm0     # 0x1ae44
jb 0x10a11
sqrtss %xmm0, %xmm0
jmp 0x10a16
callq 0x4520
movss %xmm0, 0x14(%rsp)
movl $0x618, %edx            # imm = 0x618
movq %r14, %rdi
leaq 0x90(%rsp), %rsi
callq 0x4230
cmpq $0x0, 0x10(%r15)
je 0x10ad9
movq %r15, %rdi
movq %r14, %rsi
callq *0x18(%r15)
incq 0x18(%rsp)
jmp 0x10542
cmpl $0x0, 0x24(%rsp)
jle 0x105e9
movq 0x60(%rsp), %rax
xchgl %eax, 0x81e0(%r12)
cmpl $0x0, 0x20(%rsp)
jle 0x105e9
movl 0x20(%rsp), %eax
lock
addl %eax, 0x81e0(%r12)
jmp 0x105e9
movq 0x28(%rsp), %rax
movl 0x30(%rsp), %ecx
movq %rax, 0x16e8(%rsp)
movl %ecx, 0x16f0(%rsp)
movq 0x58(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x10aec
leaq 0x16e8(%rsp), %rsi
callq *0x18(%rdi)
addq $0x9878, %rsp           # imm = 0x9878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4070
callq 0x4070
jmp 0x10ad2
movl %eax, %edi
callq 0x41a0
callq 0x40c0
callq 0x40c0
jmp 0x10ad2
jmp 0x10ad2
callq 0x40c0
callq 0x40c0
jmp 0x10af7
jmp 0x10af7
jmp 0x10af7
movq %rax, %rbx
movq 0x8(%rsp), %rdi
callq 0x4200
jmp 0x10b4b
cmpq $0x0, 0x38(%rsp)
je 0x10b2b
callq 0x4070
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x10b2e
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x10b2e
movq %rax, %rbx
movq 0x1850(%rsp), %rdi
testq %rdi, %rdi
je 0x10b4b
movq 0x1860(%rsp), %rsi
subq %rdi, %rsi
callq 0x42b0
movq %rbx, %rdi
callq 0x44f0
nop