// File: /workspace/github_cpp_makefile_star2_O1/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, 0x28(%rsp)
movq %rsi, %r13
movq %rdi, %r12
leaq 0x18(%rsp), %rdi
callq 0x7648
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
cmpl $0x0, 0x170(%r13)
je 0xffac
movl 0x81ec(%r12), %eax
movl %eax, 0x14(%rsp)
movl 0x81f4(%r12), %eax
jmp 0xffc0
movl 0x81e8(%r12), %eax
movl %eax, 0x14(%rsp)
movl 0x81f0(%r12), %eax
movl %eax, 0x10(%rsp)
leaq 0x10e4(%rsp), %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x14(%rbx)
movl $0x0, -0x4(%rbx)
movq %rbx, %rdi
callq 0x7a1c
movq $0x1, -0x14(%rbx)
movq %r13, %rdi
xorl %esi, %esi
callq 0xd30c
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
movq 0x28(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x10783
leaq 0x16e8(%rsp), %rsi
callq *0x18(%rdi)
leaq 0x81b0(%r12), %r15
movq %r15, %rdi
callq 0x4400
testl %eax, %eax
jne 0x10788
callq 0x15d13
movq %rax, 0x81d8(%r12)
movq %r15, %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 0x7a1c
movq %r13, %rdi
callq 0xf804
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, 0x40(%rsp)
pushq %rax
pushq %rbx
callq 0x10802
addq $0x10, %rsp
movl %eax, %ebx
movq %r15, %rdi
callq 0x4400
testl %eax, %eax
jne 0x1078a
movq %r14, 0x58(%rsp)
movq 0x81d8(%r12), %rdi
callq 0x15d4e
movl %eax, %ebp
movq %r15, %rdi
callq 0x4200
leaq 0xab0(%rsp), %r14
movl $0x1, 0x4(%r14)
movq %r13, %rdi
callq 0xf810
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, 0x18(%rsp)
movl 0x20(%r14), %eax
movl %eax, 0x20(%rsp)
leaq 0x16e8(%rsp), %rdi
movl $0x618, %edx            # imm = 0x618
movq %r14, %rsi
callq 0x4230
movq 0x28(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x1078c
leaq 0x170(%r13), %r14
leaq 0x16e8(%rsp), %rsi
callq *0x18(%rdi)
callq 0x15d13
movq %rax, 0x38(%rsp)
movl 0x14(%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)
movl $0x2, 0x8(%rsp)
movss 0x9b95(%rip), %xmm0     # 0x19d50
movss %xmm0, 0xc(%rsp)
movq %r13, 0x48(%rsp)
movq %r15, 0x68(%rsp)
movq %r14, 0x88(%rsp)
movq %r12, 0x70(%rsp)
movb 0x8200(%r12), %al
testb $0x1, %al
jne 0x10202
movl 0x81fc(%r12), %eax
testl %eax, %eax
jle 0x10202
movl 0x81fc(%r12), %eax
cmpl %eax, 0x8(%rsp)
jg 0x1072a
movb 0x8200(%r12), %al
testb $0x1, %al
jne 0x10234
movl 0x81f8(%r12), %eax
testl %eax, %eax
jle 0x10234
movq 0x38(%rsp), %rdi
callq 0x15d4e
movl 0x81f8(%r12), %ecx
cmpl %ecx, %eax
jge 0x1072a
movq 0x40(%rsp), %rax
movb (%rax), %al
testb $0x1, %al
jne 0x1072a
cmpl $0x7f, 0x8(%rsp)
jg 0x1072a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xchgl %eax, 0x81e0(%r12)
movl 0x81f8(%r12), %eax
testl %eax, %eax
jle 0x106a9
movl 0x81f8(%r12), %eax
xchgl %eax, 0x81e0(%r12)
movss 0xc(%rsp), %xmm0
ucomiss 0x9acc(%rip), %xmm0     # 0x19d54
jbe 0x102cd
movl 0x81e0(%r12), %eax
testl %eax, %eax
jle 0x102cd
movq 0x38(%rsp), %rdi
callq 0x15d4e
movslq 0x8(%rsp), %rcx
xorps %xmm0, %xmm0
cvtsi2ssl 0x6ac(%rsp,%rcx,4), %xmm0
mulss 0xc(%rsp), %xmm0
cvttss2si %xmm0, %ecx
addl %eax, %ecx
movl 0x81e0(%r12), %eax
cmpl %eax, %ecx
jge 0x1072a
movq %r13, %rdi
callq 0xf804
movq %r15, %rdi
callq 0x4400
testl %eax, %eax
jne 0x10775
callq 0x15d13
movq %rax, 0x81d8(%r12)
movq %r15, %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, 0x50(%rsp)
movl 0x8178(%r12), %eax
cmpl $0x2, %eax
jl 0x104f3
xorl %ebx, %ebx
movq 0x50(%rsp), %r15
movl $0x0, (%r15,%rbx,4)
movq %r12, 0x16e8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x16f0(%rsp)
movl %ebx, 0x16f4(%rsp)
movl $0x158, %edx            # imm = 0x158
leaq 0x16f8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x4230
leaq 0x1850(%rsp), %rbp
movq %rbp, %rdi
movq 0x80(%rsp), %rsi
callq 0x11b46
movl $0x8008, %edx           # imm = 0x8008
leaq 0x1868(%rsp), %rdi
movq 0x88(%rsp), %r12
movq %r12, %rsi
callq 0x4230
movq %r15, 0x9870(%rsp)
movq $0x0, 0x30(%rsp)
movl $0x8198, %edi           # imm = 0x8198
callq 0x42a0
movq %rax, %r13
leaq 0x108dc(%rip), %rax     # 0x20cc8
movq %rax, (%r13)
movups 0x16e8(%rsp), %xmm0
movups %xmm0, 0x8(%r13)
leaq 0x18(%r13), %rdi
movl $0x158, %edx            # imm = 0x158
movq %r14, %rsi
callq 0x4230
movq 0x1850(%rsp), %rax
movq %rax, 0x170(%r13)
movq 0x1858(%rsp), %rax
movq %rax, 0x178(%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 0x30(%rsp), %r14
movq %r14, %rdi
leaq 0x90(%rsp), %rsi
xorl %edx, %edx
callq 0x4260
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1049a
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x11d10
movq 0x48(%rsp), %r13
movq 0x70(%rsp), %r12
cmpq $0x0, 0x30(%rsp)
jne 0x10770
movq 0x1850(%rsp), %rdi
testq %rdi, %rdi
je 0x104da
movq 0x1860(%rsp), %rsi
subq %rdi, %rsi
callq 0x42b0
incq %rbx
movl 0x8178(%r12), %eax
cltq
decq %rax
cmpq %rax, %rbx
jl 0x10353
xorps %xmm0, %xmm0
movaps %xmm0, 0x90(%rsp)
movl $0x0, 0xa0(%rsp)
leaq 0xa4(%rsp), %rbx
movq %rbx, %rdi
callq 0x7a1c
movl 0x8(%rsp), %edx
movl %edx, 0x94(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movl $0xffc2f700, %ecx       # imm = 0xFFC2F700
movl $0x3d0900, %r8d         # imm = 0x3D0900
xorl %r9d, %r9d
pushq 0x40(%rsp)
pushq %rbx
callq 0x10802
addq $0x10, %rsp
movl %eax, 0x98(%rsp)
movq %r13, %rdi
callq 0xf810
movl %eax, 0x90(%rsp)
movb $0x1, %al
xchgb %al, 0x8198(%r12)
movq 0x8180(%r12), %r13
movq 0x8188(%r12), %rbx
cmpq %rbx, %r13
je 0x10590
cmpq $0x0, (%r13)
je 0x1058a
movq %r13, %rdi
callq 0x44b0
addq $0x8, %r13
jmp 0x10576
movl 0x8178(%r12), %eax
cmpl $0x2, %eax
movq 0x50(%rsp), %rdx
jl 0x105c4
xorl %eax, %eax
movl (%rdx,%rax,4), %ecx
addl %ecx, 0x90(%rsp)
incq %rax
movl 0x8178(%r12), %ecx
movslq %ecx, %rcx
decq %rcx
cmpq %rcx, %rax
jl 0x105a4
movq 0x8180(%r12), %rax
movq 0x8188(%r12), %rcx
movq %rax, %rdx
cmpq %rax, %rcx
movq 0x48(%rsp), %r13
movq 0x68(%rsp), %r15
je 0x10601
cmpq $0x0, (%rdx)
jne 0x1076b
addq $0x8, %rdx
cmpq %rcx, %rdx
jne 0x105e6
movq %rax, 0x8188(%r12)
movl 0x98(%rsp), %ebx
movl %ebx, %eax
negl %eax
jo 0x10722
leaq 0xa4(%rsp), %rcx
movl 0xc(%rcx), %eax
movl %eax, 0x20(%rsp)
movq 0x4(%rcx), %rax
movq %rax, 0x18(%rsp)
movl 0x81e4(%r12), %eax
movl %eax, 0xa0(%rsp)
movq %r15, %rdi
callq 0x4400
testl %eax, %eax
jne 0x10777
movq 0x81d8(%r12), %rdi
callq 0x15d4e
movl %eax, 0x9c(%rsp)
movq %r15, %rdi
callq 0x4200
movslq 0x8(%rsp), %rax
movl 0x90(%rsp), %ecx
movl 0x9c(%rsp), %edx
cvtsi2ss %ecx, %xmm0
movl %ecx, 0x8b0(%rsp,%rax,4)
cvtsi2ssl 0x8ac(%rsp,%rax,4), %xmm1
movl %edx, 0x6b0(%rsp,%rax,4)
divss %xmm1, %xmm0
ucomiss 0x96b6(%rip), %xmm0     # 0x19d54
jb 0x106de
movq %r15, %r14
sqrtss %xmm0, %xmm0
jmp 0x106e6
cmpl $0x0, 0x14(%rsp)
jle 0x1027b
movq 0x60(%rsp), %rax
xchgl %eax, 0x81e0(%r12)
cmpl $0x0, 0x10(%rsp)
jle 0x1027b
movl 0x10(%rsp), %eax
lock
addl %eax, 0x81e0(%r12)
jmp 0x1027b
movq %r15, %r14
callq 0x4520
movss %xmm0, 0xc(%rsp)
leaq 0x16e8(%rsp), %rdi
movl $0x618, %edx            # imm = 0x618
movq %rdi, %r15
leaq 0x90(%rsp), %rsi
callq 0x4230
movq 0x28(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x1077e
movq %r15, %rsi
callq *0x18(%rdi)
incl 0x8(%rsp)
movq %r14, %r15
negl %ebx
jno 0x101d8
movq 0x18(%rsp), %rax
movl 0x20(%rsp), %ecx
movq %rax, 0x16e8(%rsp)
movl %ecx, 0x16f0(%rsp)
movq 0x58(%rsp), %rdi
cmpq $0x0, 0x10(%rdi)
je 0x10791
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 0x10777
movl %eax, %edi
callq 0x41a0
callq 0x40c0
callq 0x40c0
jmp 0x10777
jmp 0x10777
callq 0x40c0
callq 0x40c0
jmp 0x1079c
jmp 0x1079c
jmp 0x1079c
movq %rax, %rbx
movq %r15, %rdi
callq 0x4200
jmp 0x107fa
cmpq $0x0, 0x30(%rsp)
je 0x107da
callq 0x4070
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x107cc
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0x90(%rsp)
jmp 0x107dd
movq %rax, %rbx
movq 0x1850(%rsp), %rdi
testq %rdi, %rdi
je 0x107fa
movq 0x1860(%rsp), %rsi
subq %rdi, %rsi
callq 0x42b0
movq %rbx, %rdi
callq 0x44f0