// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdi, %r13
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movl %edx, 0xc(%rsp)
callq 0x35e4
movq (%r12), %r15
movq 0x8(%r12), %rbx
cmpq %rbx, %r15
je 0x3c4c
movq (%r14), %rdi
movq 0x8(%r14), %rsi
addq $0x8, %r15
movq %r15, %rdx
callq 0x7592
imull $0xfffffc7c, %eax, %eax # imm = 0xFFFFFC7C
movl %eax, -0x4(%r15)
addq $0x20, %r15
jmp 0x3c24
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r14
movq %r14, %r12
subq %r15, %r12
pushq $0x28
popq %rcx
movq %r12, %rax
cqto
idivq %rcx
testq %rax, %rax
je 0x3c86
cmpq $0x1, %rax
jne 0x3c9f
addq $0x8, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x2250
jmp 0x3eb1
leaq 0x5051(%rip), %rsi      # 0x8cde
leaq 0x30(%rsp), %rdx
movq %r13, %rdi
callq 0x20c0
jmp 0x3eb1
cmpq %r14, %r15
je 0x3cf7
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x44f4
cmpq $0x281, %r12            # imm = 0x281
jl 0x3cec
leaq 0x280(%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x4808
jmp 0x3ce5
movq %r12, %rdi
callq 0x489b
addq $0x28, %r12
cmpq %r14, %r12
jne 0x3cd9
jmp 0x3cf7
movq %r15, %rdi
movq %r14, %rsi
callq 0x4808
callq 0x2040
movq %rax, 0xa72d(%rip)      # 0xe430
negl 0xc(%rsp)
pushq $0x1
popq %r15
leaq 0x4f2c(%rip), %rsi      # 0x8c3e
leaq 0x4f80(%rip), %rbx      # 0x8c99
movq %r13, 0x28(%rsp)
cmpl $0x15, %r15d
je 0x3e91
movq 0xa2a9(%rip), %rdi      # 0xdfd8
callq 0x21c0
movq %rax, %rdi
movl %r15d, %esi
callq 0x22d0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x21c0
movq %rax, %rdi
callq 0x2140
movl $0xffe17b80, %ebx       # imm = 0xFFE17B80
xorl %r12d, %r12d
xorl %ebp, %ebp
movl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rax
subq %r13, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cltq
cmpq %rax, %rbp
jge 0x3e1a
leaq (%r12,%r13), %r14
movl %ebx, %ecx
negl %ecx
movq %r14, %rdi
movl 0xc(%rsp), %esi
movl $0xffe17b80, %edx       # imm = 0xFFE17B80
movl %r15d, %r8d
callq 0x36c5
movl %eax, %ecx
negl %ecx
movl %ecx, (%r14)
cmpl $0x61a80, %eax          # imm = 0x61A80
je 0x3e9b
cmpl $0xfff9e580, %eax       # imm = 0xFFF9E580
je 0x3e9b
movl 0x4(%r13,%r12), %r14d
subl %eax, %r14d
movl %r14d, (%r13,%r12)
cmpl %ebx, %r14d
jle 0x3df3
movq 0xa202(%rip), %rdi      # 0xdfd8
leaq 0x4e69(%rip), %rsi      # 0x8c46
callq 0x21c0
movq %rax, %rdi
callq 0x2140
movl %ebp, 0x8(%rsp)
movl %r14d, %ebx
jmp 0x3e0e
movq 0xa1de(%rip), %rdi      # 0xdfd8
leaq 0x4e47(%rip), %rsi      # 0x8c48
callq 0x21c0
movq %rax, %rdi
callq 0x2140
incq %rbp
addq $0x28, %r12
jmp 0x3d64
movl 0x8(%rsp), %eax
testl %eax, %eax
je 0x3e69
cltq
imulq $0x28, %rax, %r14
addq %r14, %r13
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x4f76
addq 0x10(%rsp), %r14
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x4f8a
movq 0x10(%rsp), %rsi
movq %r12, %rdi
leaq 0x30(%rsp), %rdx
callq 0x4f90
leaq 0x38(%rsp), %rdi
callq 0x20f0
addl $0xfffcf2c0, %ebx       # imm = 0xFFFCF2C0
incl %r15d
cmpl $0xfff9e580, %ebx       # imm = 0xFFF9E580
movq 0x28(%rsp), %r13
leaq 0x4dba(%rip), %rsi      # 0x8c3e
leaq 0x4e0e(%rip), %rbx      # 0x8c99
ja 0x3d1e
movq 0x10(%rsp), %r15
jmp 0x3c72
movq 0x10(%rsp), %rsi
addq $0x8, %rsi
movq 0x28(%rsp), %r13
movq %r13, %rdi
callq 0x2250
leaq 0x10(%rsp), %rdi
callq 0x4edc
movq %r13, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3ecf
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x20f0
jmp 0x3eed
jmp 0x3eea
jmp 0x3eea
jmp 0x3eea
jmp 0x3eea
jmp 0x3eea
jmp 0x3eea
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x4edc
movq %rbx, %rdi
callq 0x22e0