// File: /workspace/github_cpp_makefile_star2_O2/vygr[P]C-Chess/chess.cpp
// Function: score_impl(score_board const&, int, int, int, int)
int score_impl(const score_board &sbrd, int colour, int alpha, int beta, int ply)
{
	if (ply == 0) return -sbrd.score;
	auto next_boards = all_moves(sbrd.brd, colour);
	auto mate = true;
	if (next_boards.size() != 0)
	{
		if (ply > 1)
		{
			std::sort(begin(next_boards), end(next_boards), [&] (const auto &brd1, const auto &brd2)
			{
				return brd1.score > brd2.score;
			});
		}
		for (auto &score_board : next_boards)
		{
			int value;
			if (!mate)
			{
				//not first child so null search window
				value = -score(score_board, -colour, -alpha-1, -alpha, ply-1);
				if (alpha < value && value < beta)
				{
					//failed high, so full re-search
					value = -score(score_board, -colour, -beta, -alpha, ply-1);
				}
			}
			else
			{
				value = -score(score_board, -colour, -beta, -alpha, ply-1);
			}
			mate = false;
			if (value == timeout_value || value == -timeout_value)
			{
				//move time out
				return value;
			}
			if (value >= mate_value)
			{
				//early return if mate
				return value;
			}
			if (value >= beta)
			{
				//fail hard beta cutoff
				return beta;
			}
			if (value > alpha) alpha = value;
			auto end_time = std::chrono::high_resolution_clock::now();
			std::chrono::duration<float> elapsed = end_time - start_time;
			if (elapsed.count() >= max_time_per_move)
			{
				//time has expired for this move
				return timeout_value;
			}
		}
	}
	if (!mate) return alpha;
	std::size_t king_index = 0;
	if (in_check(sbrd.brd, colour, king_index))
	{
		//check mate
		return -mate_value - ply;
	}
	//stale mate
	return mate_value;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, 0x4(%rsp)
movq %rdi, %rbp
testl %r8d, %r8d
je 0x3a73
movl %r8d, %r13d
movl %edx, %r12d
movl %esi, %edx
addq $0x8, %rbp
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movl %edx, 0x8(%rsp)
callq 0x35e4
movq (%r14), %rbx
movq 0x8(%r14), %r14
movq %r14, %rax
subq %rbx, %rax
pushq $0x28
popq %rcx
cqto
idivq %rcx
movq %r14, %r15
subq %rbx, %r15
je 0x3b89
movq %r13, 0x10(%rsp)
cmpl $0x2, %r13d
jl 0x3a8a
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4104
cmpq $0x281, %r15            # imm = 0x281
jl 0x3a7f
leaq 0x280(%rbx), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4418
jmp 0x3a6c
movq %r15, %rdi
callq 0x44ab
addq $0x28, %r15
cmpq %r14, %r15
jne 0x3a60
jmp 0x3a8a
xorl %r12d, %r12d
subl (%rbp), %r12d
jmp 0x3bbc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4418
movq %rbp, 0x18(%rsp)
movq 0x28(%rsp), %r13
movq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movl 0x8(%rsp), %ebp
negl %ebp
movq 0x10(%rsp), %rax
leal -0x1(%rax), %r14d
movl 0x4(%rsp), %eax
negl %eax
movl %eax, 0xc(%rsp)
movb $0x1, %al
cmpq 0x20(%rsp), %r13
je 0x3b7b
movl %r12d, %r15d
negl %r15d
testb $0x1, %al
je 0x3ae4
movq %r13, %rdi
movl %ebp, %esi
movl 0xc(%rsp), %edx
movl %r15d, %ecx
movl %r14d, %r8d
callq 0x36c5
jmp 0x3b1c
movl %r12d, %edx
notl %edx
movq %r13, %rdi
movl %ebp, %esi
movl %r15d, %ecx
movl %r14d, %r8d
callq 0x36c5
movl %eax, %ebx
negl %ebx
cmpl %ebx, %r12d
jge 0x3b20
cmpl 0x4(%rsp), %ebx
jge 0x3b20
movq %r13, %rdi
movl %ebp, %esi
movl 0xc(%rsp), %edx
movl %r15d, %ecx
movl %r14d, %r8d
callq 0x36c5
movl %eax, %ebx
negl %ebx
cmpl $0xfff9e580, %ebx       # imm = 0xFFF9E580
je 0x3b76
cmpl $0x61a80, %ebx          # imm = 0x61A80
je 0x3b76
cmpl $0x30d3f, %ebx          # imm = 0x30D3F
jg 0x3b76
cmpl 0x4(%rsp), %ebx
jge 0x3bce
callq 0x2040
subq 0xa8e2(%rip), %rax      # 0xe430
cvtsi2ss %rax, %xmm0
divss 0x44a9(%rip), %xmm0     # 0x8004
ucomiss 0x44a6(%rip), %xmm0     # 0x8008
jae 0x3bd5
cmpl %r12d, %ebx
cmovgl %ebx, %r12d
addq $0x28, %r13
xorl %eax, %eax
jmp 0x3ab9
movl %ebx, %r12d
jmp 0x3bb2
testb $0x1, %al
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
je 0x3bb2
leaq 0x40(%rsp), %rdx
andq $0x0, (%rdx)
movq %rbp, %rdi
movl 0x8(%rsp), %esi
callq 0x313a
movl $0xfffcf2c0, %ecx       # imm = 0xFFFCF2C0
subl %r13d, %ecx
testb %al, %al
movl $0x30d40, %r12d         # imm = 0x30D40
cmovnel %ecx, %r12d
leaq 0x28(%rsp), %rdi
callq 0x4edc
movl %r12d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %r12d
jmp 0x3bb2
movl $0x61a80, %r12d         # imm = 0x61A80
jmp 0x3bb2
jmp 0x3be1
jmp 0x3be1
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4edc
movq %rbx, %rdi
callq 0x22e0