// File: /workspace/github_cpp_makefile_star2_O1/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 $0x68, %rsp
movq %rcx, %r14
movq %rdi, 0x10(%rsp)
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movl %edx, 0x1c(%rsp)
callq 0x38ad
movq (%r15), %rbx
movq 0x8(%r15), %rax
movq %rax, 0x8(%rsp)
cmpq %rax, %rbx
je 0x41fe
leaq 0x38(%rsp), %r15
movq (%r14), %r12
movq 0x8(%r14), %rbp
leaq 0x8(%rbx), %rax
movq %rax, 0x38(%rsp)
movl $0x0, %eax
cmpq %rbp, %r12
je 0x41f0
xorl %r13d, %r13d
movq %r15, %rdi
movq %r12, %rsi
callq 0x8692
movzbl %al, %eax
addq %rax, %r13
addq $0x20, %r12
cmpq %rbp, %r12
jne 0x41cf
imull $0xfffffc7c, %r13d, %eax # imm = 0xFFFFFC7C
movl %eax, 0x4(%rbx)
addq $0x28, %rbx
cmpq 0x8(%rsp), %rbx
jne 0x41b2
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %r14
movq %r14, %rbx
subq %r15, %rbx
movq %rbx, %rcx
sarq $0x3, %rcx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rax
testq %rax, %rax
je 0x4238
cmpq $0x1, %rax
jne 0x425b
movq 0x10(%rsp), %rbx
jmp 0x4540
movq 0x10(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
leaq 0x5a7f(%rip), %rdx      # 0x9cca
movq %rbx, %rdi
movq %rdx, %rsi
callq 0x6c98
jmp 0x455a
cmpq %r14, %r15
je 0x42b1
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x5252
cmpq $0x281, %rbx            # imm = 0x281
jl 0x42a6
leaq 0x280(%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x59d7
cmpq %r14, %r12
je 0x42b1
movq %r12, %rdi
callq 0x5b27
addq $0x28, %r12
jmp 0x4293
movq %r15, %rdi
movq %r14, %rsi
callq 0x59d7
callq 0x2040
movq %rax, 0xa15b(%rip)      # 0xe418
negl 0x1c(%rsp)
movq 0x10(%rsp), %rbx
leaq 0x10(%rbx), %rax
movq %rax, 0x60(%rsp)
movl $0x1, 0x8(%rsp)
leaq 0x20(%rsp), %r14
movl $0x7, %edx
movq 0x9cf0(%rip), %rdi      # 0xdfd8
leaq 0x593b(%rip), %rsi      # 0x9c2a
callq 0x21d0
movq 0x9cdd(%rip), %rdi      # 0xdfd8
movl 0x8(%rsp), %esi
callq 0x22b0
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
leaq 0x596f(%rip), %rsi      # 0x9c85
callq 0x21d0
movq %r13, %rdi
callq 0x2120
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rax
subq %r15, %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
testl %eax, %eax
setg %r12b
jle 0x4459
xorl %r14d, %r14d
movl $0xffe17b80, %ebx       # imm = 0xFFE17B80
xorl %ebp, %ebp
movl $0x0, 0x18(%rsp)
leaq (%r15,%r14), %r13
movl %ebx, %ecx
negl %ecx
movq %r13, %rdi
movl 0x1c(%rsp), %esi
movl $0xffe17b80, %edx       # imm = 0xFFE17B80
movl 0x8(%rsp), %r8d
callq 0x39d2
movl %eax, %ecx
negl %ecx
movl %ecx, (%r13)
cmpl $0x61a80, %eax          # imm = 0x61A80
je 0x438c
cmpl $0xfff9e580, %eax       # imm = 0xFFF9E580
jne 0x43ec
movq 0x20(%rsp), %rax
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rcx
movq %rcx, (%rdi)
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
addq %rsi, %rdx
callq 0x6a42
xorl %r15d, %r15d
testb %r15b, %r15b
je 0x4467
incq %rbp
movq 0x20(%rsp), %r15
movq 0x28(%rsp), %rax
subq %r15, %rax
shrq $0x3, %rax
imull $0xcccccccd, %eax, %eax # imm = 0xCCCCCCCD
cltq
addq $0x28, %r14
cmpq %rax, %rbp
setl %r12b
jl 0x4358
jmp 0x446e
movl 0x4(%r15,%r14), %r13d
subl %eax, %r13d
movl %r13d, (%r15,%r14)
cmpl %ebx, %r13d
jle 0x442d
movl $0x1, %edx
movq 0x9bcf(%rip), %rdi      # 0xdfd8
leaq 0x5822(%rip), %rsi      # 0x9c32
callq 0x21d0
movq 0x9bbc(%rip), %rdi      # 0xdfd8
callq 0x2120
movb $0x1, %r15b
movl %ebp, 0x18(%rsp)
movl %r13d, %ebx
jmp 0x43b1
movl $0x1, %edx
movq 0x9b9f(%rip), %rdi      # 0xdfd8
leaq 0x57f4(%rip), %rsi      # 0x9c34
callq 0x21d0
movb $0x1, %r15b
movq 0x9b89(%rip), %rdi      # 0xdfd8
callq 0x2120
jmp 0x43b1
xorl %ecx, %ecx
movl $0x7, %eax
movl $0x4, %ebp
jmp 0x4496
movl $0x1, %eax
jmp 0x4473
movl $0x7, %eax
addl $0xfffcf2c0, %ebx       # imm = 0xFFFCF2C0
xorl %ebp, %ebp
cmpl $0xfff9e581, %ebx       # imm = 0xFFF9E581
setb %bpl
shll $0x2, %ebp
movq 0x10(%rsp), %rbx
leaq 0x20(%rsp), %r14
movl 0x18(%rsp), %ecx
testb $0x1, %r12b
jne 0x451a
testl %ecx, %ecx
je 0x4518
movslq %ecx, %rax
movq 0x20(%rsp), %rcx
leaq (%rax,%rax,4), %r13
movq (%rcx,%r13,8), %rax
movq %rax, 0x38(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x8(%rcx,%r13,8), %rsi
movq 0x10(%rcx,%r13,8), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0x6a42
shlq $0x3, %r13
addq 0x20(%rsp), %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x86c4
movq 0x20(%rsp), %rsi
movq %r14, %rdi
leaq 0x38(%rsp), %rdx
callq 0x65a6
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x4518
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
movl %ebp, %eax
testl %eax, %eax
jne 0x4536
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
cmpl $0x15, %eax
jne 0x42dc
movl $0x4, %eax
cmpl $0x4, %eax
jne 0x455a
movq 0x20(%rsp), %r15
leaq 0x10(%rbx), %rax
movq %rax, (%rbx)
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x6a42
leaq 0x20(%rsp), %rdi
callq 0x653e
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x45a1
jmp 0x45a1
jmp 0x457c
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x45a4
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x21a0
jmp 0x45a4
jmp 0x45a1
jmp 0x45a1
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x653e
movq %rbx, %rdi
callq 0x22c0