// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %rdi, %rbp
testl %r8d, %r8d
je 0x3fc5
movl %r8d, %r13d
movl %edx, %r12d
movl %esi, %edx
movl %ecx, 0x10(%rsp)
addq $0x8, %rbp
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movl %edx, 0x18(%rsp)
callq 0x38ad
movq (%rbx), %r14
movq 0x8(%rbx), %rbx
movq %rbx, %rax
subq %r14, %rax
movq %rbx, %r15
subq %r14, %r15
movq %r13, 0x28(%rsp)
je 0x3fd1
cmpl $0x2, %r13d
jl 0x3fe3
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 0x48b2
cmpq $0x281, %r15            # imm = 0x281
jl 0x3fd8
leaq 0x280(%r14), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x5037
cmpq %rbx, %r15
je 0x3fe3
movq %r15, %rdi
callq 0x5187
addq $0x28, %r15
jmp 0x3fb2
xorl %r13d, %r13d
subl (%rbp), %r13d
jmp 0x414c
movb $0x1, %al
jmp 0x410e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5037
movq %rbp, 0x20(%rsp)
movq 0x38(%rsp), %rbp
movq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq %rax, %rbp
sete %r14b
movb %r14b, 0xf(%rsp)
je 0x40ff
movl 0x18(%rsp), %ebx
negl %ebx
leal -0x1(%r13), %eax
movl %eax, 0x14(%rsp)
movl 0x10(%rsp), %eax
negl %eax
movl %eax, 0x1c(%rsp)
movb $0x1, %al
movl %r12d, %r15d
negl %r15d
testb $0x1, %al
je 0x4045
movq %rbp, %rdi
movl %ebx, %esi
movl 0x1c(%rsp), %edx
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
callq 0x39d2
jmp 0x407f
movl %r12d, %edx
notl %edx
movq %rbp, %rdi
movl %ebx, %esi
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
callq 0x39d2
negl %eax
cmpl %eax, %r12d
jge 0x4081
cmpl 0x10(%rsp), %eax
jge 0x4081
movq %rbp, %rdi
movl %ebx, %esi
movl 0x1c(%rsp), %edx
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
callq 0x39d2
negl %eax
xorl %ecx, %ecx
cmpl $0xfff9e580, %eax       # imm = 0xFFF9E580
je 0x409a
cmpl $0x61a80, %eax          # imm = 0x61A80
je 0x409a
cmpl $0x30d3f, %eax          # imm = 0x30D3F
jle 0x40b8
xorl %ecx, %ecx
movl %eax, %r13d
testb %cl, %cl
je 0x40ff
addq $0x28, %rbp
xorl %eax, %eax
cmpq 0x30(%rsp), %rbp
sete %r14b
jne 0x4023
jmp 0x40ff
movl 0x10(%rsp), %edx
cmpl %edx, %eax
jge 0x40f8
cmpl %r12d, %eax
cmovgl %eax, %r12d
callq 0x2040
subq 0xa345(%rip), %rax      # 0xe418
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
divss 0x4f21(%rip), %xmm0     # 0x9004
ucomiss 0x4f1e(%rip), %xmm0     # 0x9008
setb %cl
movl $0x61a80, %eax          # imm = 0x61A80
cmovael %eax, %r13d
jmp 0x409d
xorl %ecx, %ecx
movl %edx, %r13d
jmp 0x409d
testb $0x1, %r14b
movq 0x20(%rsp), %rbp
movb 0xf(%rsp), %al
je 0x4142
movl %r12d, %r13d
testb %al, %al
je 0x4142
leaq 0x50(%rsp), %rdx
movq $0x0, (%rdx)
movq %rbp, %rdi
movl 0x18(%rsp), %esi
callq 0x3324
movl $0xfffcf2c0, %ecx       # imm = 0xFFFCF2C0
subl 0x28(%rsp), %ecx
testb %al, %al
movl $0x30d40, %r13d         # imm = 0x30D40
cmovnel %ecx, %r13d
leaq 0x38(%rsp), %rdi
callq 0x653e
movl %r13d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4160
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x653e
movq %rbx, %rdi
callq 0x22c0