// File: /workspace/github_cpp_makefile_star2_O3/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, (%rsp)
movq %rdi, %r15
testl %r8d, %r8d
je 0x3f76
movl %r8d, %r13d
movl %edx, %ebp
movl %esi, %edx
addq $0x8, %r15
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movl %edx, 0x8(%rsp)
callq 0x3861
movq (%rbx), %r14
movq 0x8(%rbx), %rbx
movq %rbx, %rax
subq %r14, %rax
movq %rbx, %r12
subq %r14, %r12
je 0x409c
cmpl $0x2, %r13d
jl 0x3f8b
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4805
cmpq $0x281, %r12            # imm = 0x281
jl 0x3f80
leaq 0x280(%r14), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x4f5d
cmpq %rbx, %r12
je 0x3f8b
movq %r12, %rdi
callq 0x50b3
addq $0x28, %r12
jmp 0x3f63
xorl %ebp, %ebp
subl (%r15), %ebp
jmp 0x40e0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4f5d
movq 0x28(%rsp), %r14
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq %rax, %r14
je 0x409c
movq %r13, %rax
movl 0x8(%rsp), %r13d
negl %r13d
movq %rax, 0x20(%rsp)
decl %eax
movl %eax, 0x4(%rsp)
movl (%rsp), %eax
negl %eax
movl %eax, 0xc(%rsp)
movb $0x1, %al
movq %r14, 0x18(%rsp)
movl %ebp, %r12d
negl %r12d
testb $0x1, %al
je 0x3fec
movq %r14, %rdi
movl %r13d, %esi
movl 0xc(%rsp), %edx
movl %r12d, %ecx
movl 0x4(%rsp), %r8d
callq 0x3986
jmp 0x4027
movl %ebp, %edx
notl %edx
movq %r14, %rdi
movl %r13d, %esi
movl %r12d, %ecx
movl 0x4(%rsp), %r8d
callq 0x3986
movl %eax, %ebx
negl %ebx
cmpl %ebx, %ebp
jge 0x402b
cmpl (%rsp), %ebx
jge 0x402b
movq %r14, %rdi
movl %r13d, %esi
movl 0xc(%rsp), %edx
movl %r12d, %ecx
movl 0x4(%rsp), %r8d
callq 0x3986
movl %eax, %ebx
negl %ebx
cmpl $0xfff9e580, %ebx       # imm = 0xFFF9E580
je 0x40c8
cmpl $0x61a80, %ebx          # imm = 0x61A80
je 0x40c8
cmpl $0x30d3f, %ebx          # imm = 0x30D3F
jg 0x40c8
cmpl (%rsp), %ebx
jge 0x40cc
callq 0x2040
subq 0xa3bc(%rip), %rax      # 0xe418
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x4f98(%rip), %xmm0     # 0x9004
ucomiss 0x4f95(%rip), %xmm0     # 0x9008
jae 0x40d1
cmpl %ebp, %ebx
cmovgl %ebx, %ebp
addq $0x28, %r14
xorl %eax, %eax
cmpq 0x10(%rsp), %r14
jne 0x3fc9
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movq 0x20(%rsp), %r13
jne 0x40d6
leaq 0x40(%rsp), %rdx
movq $0x0, (%rdx)
movq %r15, %rdi
movl 0x8(%rsp), %esi
callq 0x32c9
movl $0xfffcf2c0, %ecx       # imm = 0xFFFCF2C0
subl %r13d, %ecx
testb %al, %al
movl $0x30d40, %ebp          # imm = 0x30D40
cmovnel %ecx, %ebp
jmp 0x40d6
movl %ebx, %ebp
jmp 0x40d6
movl (%rsp), %ebp
jmp 0x40d6
movl $0x61a80, %ebp          # imm = 0x61A80
leaq 0x28(%rsp), %rdi
callq 0x646c
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40f3
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x646c
movq %rbx, %rdi
callq 0x22c0