// File: /workspace/github_cpp_makefile_star2_O3/vygr[P]C-Chess/chess.cpp
// Function: best_move(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&)
auto best_move(const board &brd, int colour, const boards &history)
{
	//first ply of boards
	auto next_boards = all_moves(brd, colour);
	for (auto &sbrd : next_boards)
	{
		auto rep = std::count(begin(history), end(history), sbrd.brd);
		sbrd.bias = static_cast<int>(-(rep * queen_value));
	}
	if (next_boards.size() == 0) return std::string("");
	if (next_boards.size() == 1) return next_boards[0].brd;
	std::sort(begin(next_boards), end(next_boards), [&] (const auto &brd1, const auto &brd2)
	{
		return brd1.score > brd2.score;
	});

	//start move timer
	start_time = std::chrono::high_resolution_clock::now();
	for (auto ply = 1; ply <= max_ply; ++ply)
 	{
		//iterative deepening of ply so we allways have a best move to go with if the timer expires
		std::cout << "\nPly = " << ply << " " << std::flush;
		auto best_index = 0;
		auto alpha = -mate_value*10;
		auto beta = mate_value*10;
		for (auto index = 0; index < static_cast<int>(next_boards.size()); ++index)
		{
			auto score_board = &next_boards[index];
			score_board->score = -score(*score_board, -colour, -beta, -alpha, ply);
			if (score_board->score == timeout_value || score_board->score == -timeout_value)
			{
				//move timer expired
				return next_boards[0].brd;
			}
			score_board->score += score_board->bias;
			if (score_board->score > alpha)
			{
				//got a better board than last best
				alpha = score_board->score;
				best_index = index;
				std::cout << "*" << std::flush;
			}
			else
			{
				//just tick off another board
				std::cout << "." << std::flush;
			}
		}
		if (best_index != 0)
		{
			//promote board to PV
			auto score_board = next_boards[best_index];
			next_boards.erase(begin(next_boards) + best_index);
			next_boards.insert(begin(next_boards), score_board);
		}
		if (alpha >= mate_value || alpha <= -mate_value)
		{
			//don't look further ahead if we allready can force mate
			break;
		}
	}
	return next_boards[0].brd;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, (%rsp)
movq %rdi, %r14
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movl %edx, 0xc(%rsp)
callq 0x3861
movq (%rbx), %r15
movq 0x8(%rbx), %r12
cmpq %r12, %r15
movq %r14, 0x10(%rsp)
je 0x41a1
leaq 0x30(%rsp), %r13
movq (%rsp), %rax
movq (%rax), %rbx
movq 0x8(%rax), %rbp
leaq 0x8(%r15), %rax
movq %rax, 0x30(%rsp)
cmpq %rbp, %rbx
je 0x4183
xorl %r14d, %r14d
movq %r13, %rdi
movq %rbx, %rsi
callq 0x858c
movzbl %al, %eax
addl %eax, %r14d
addq $0x20, %rbx
cmpq %rbp, %rbx
jne 0x4160
imull $0xfffffc7c, %r14d, %eax # imm = 0xFFFFFC7C
jmp 0x4185
xorl %eax, %eax
movl %eax, 0x4(%r15)
addq $0x28, %r15
cmpq %r12, %r15
jne 0x4144
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %r14
movq %r12, %rbx
subq %r15, %rbx
movq %rbx, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
testq %rax, %rax
je 0x41e6
cmpq $0x1, %rax
jne 0x4204
leaq 0x10(%r14), %rax
movq %rax, (%r14)
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x6974
jmp 0x44ab
leaq 0x10(%r14), %rax
movq %rax, (%r14)
leaq 0x5ad6(%rip), %rdx      # 0x9cca
movq %r14, %rdi
movq %rdx, %rsi
callq 0x6bce
jmp 0x44ab
cmpq %r12, %r15
je 0x425a
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x5178
cmpq $0x281, %rbx            # imm = 0x281
jl 0x424f
leaq 0x280(%r15), %rbx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x58d0
cmpq %r12, %rbx
je 0x425a
movq %rbx, %rdi
callq 0x5a26
addq $0x28, %rbx
jmp 0x423c
movq %r15, %rdi
movq %r12, %rsi
callq 0x58d0
callq 0x2040
movq %rax, 0xa1b2(%rip)      # 0xe418
negl 0xc(%rsp)
movl $0x1, (%rsp)
leaq 0x59b2(%rip), %rsi      # 0x9c2a
leaq 0x5a06(%rip), %r15      # 0x9c85
movl $0x7, %edx
movq 0x9d4d(%rip), %rdi      # 0xdfd8
callq 0x21d0
movq 0x9d41(%rip), %rdi      # 0xdfd8
movl (%rsp), %esi
callq 0x22b0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21d0
movq %rbx, %rdi
callq 0x2120
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rax
subq %r15, %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
testl %eax, %eax
jle 0x447d
xorl %r14d, %r14d
movl $0xffe17b80, %r13d      # imm = 0xFFE17B80
xorl %ebp, %ebp
xorl %r12d, %r12d
leaq (%r15,%r14), %rbx
movl %r13d, %ecx
negl %ecx
movq %rbx, %rdi
movl 0xc(%rsp), %esi
movl $0xffe17b80, %edx       # imm = 0xFFE17B80
movl (%rsp), %r8d
callq 0x3986
movl %eax, %ecx
negl %ecx
movl %ecx, (%rbx)
cmpl $0x61a80, %eax          # imm = 0x61A80
je 0x4487
cmpl $0xfff9e580, %eax       # imm = 0xFFF9E580
je 0x4487
movl 0x4(%r15,%r14), %ebx
subl %eax, %ebx
movl %ebx, (%r15,%r14)
cmpl %r13d, %ebx
jle 0x435d
movl $0x1, %edx
movq 0x9c9b(%rip), %rdi      # 0xdfd8
leaq 0x58ee(%rip), %rsi      # 0x9c32
callq 0x21d0
movq 0x9c88(%rip), %rdi      # 0xdfd8
callq 0x2120
movl %ebp, %r12d
movl %ebx, %r13d
jmp 0x4381
movl $0x1, %edx
movq 0x9c6f(%rip), %rdi      # 0xdfd8
leaq 0x58c4(%rip), %rsi      # 0x9c34
callq 0x21d0
movq 0x9c5c(%rip), %rdi      # 0xdfd8
callq 0x2120
incq %rbp
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %rax
subq %r15, %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
cltq
addq $0x28, %r14
cmpq %rax, %rbp
jl 0x42e7
addl $0xfffcf2c0, %r13d      # imm = 0xFFFCF2C0
testl %r12d, %r12d
je 0x4450
movslq %r12d, %rax
leaq (%rax,%rax,4), %rbx
movq (%r15,%rbx,8), %rax
movq %rax, 0x30(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x8(%r15,%rbx,8), %rsi
movq 0x10(%r15,%rbx,8), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x6974
shlq $0x3, %rbx
addq 0x18(%rsp), %rbx
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x85be
movq 0x18(%rsp), %rsi
movq %r14, %rdi
leaq 0x30(%rsp), %rdx
callq 0x64d6
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x4432
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
cmpl $0xfff9e581, %r13d      # imm = 0xFFF9E581
movq 0x10(%rsp), %r14
leaq 0x57e5(%rip), %rsi      # 0x9c2a
leaq 0x5839(%rip), %r15      # 0x9c85
jae 0x446c
jmp 0x447d
cmpl $0xfff9e580, %r13d      # imm = 0xFFF9E580
movq 0x10(%rsp), %r14
leaq 0x57c7(%rip), %rsi      # 0x9c2a
leaq 0x581b(%rip), %r15      # 0x9c85
jbe 0x447d
movl (%rsp), %eax
incl %eax
movl %eax, (%rsp)
cmpl $0x15, %eax
jne 0x427f
movq 0x18(%rsp), %r15
jmp 0x41c7
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %r14
leaq 0x10(%r14), %rcx
movq %rcx, (%r14)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x6974
leaq 0x18(%rsp), %rdi
callq 0x646c
movq %r14, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x44c9
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x44f7
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x44f7
jmp 0x44f4
jmp 0x44f4
jmp 0x44f4
jmp 0x44f4
jmp 0x44f4
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x646c
movq %rbx, %rdi
callq 0x22c0