// File: 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:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movl %esi, 0xd4(%rsp)
movl %edx, 0xd0(%rsp)
movl %ecx, 0xcc(%rsp)
movl %r8d, 0xc8(%rsp)
cmpl $0x0, 0xc8(%rsp)
jne 0x591e
movq 0xd8(%rsp), %rcx
xorl %eax, %eax
subl (%rcx), %eax
movl %eax, 0xe4(%rsp)
jmp 0x5ce1
movq 0xd8(%rsp), %rsi
addq $0x8, %rsi
movl 0xd4(%rsp), %edx
leaq 0xb0(%rsp), %rdi
callq 0x5230
movb $0x1, 0xaf(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xac80
cmpq $0x0, %rax
je 0x5c4c
cmpl $0x1, 0xc8(%rsp)
jle 0x59ec
leaq 0xb0(%rsp), %rdi
callq 0xaca0
movq %rax, 0x30(%rsp)
jmp 0x597f
movq 0x30(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xacd0
movq %rax, 0x28(%rsp)
jmp 0x59a0
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xa0(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x5d00
jmp 0x59c4
jmp 0x59ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xa8d0
jmp 0x5cf0
leaq 0xb0(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rdi
callq 0xad00
movq %rax, 0x70(%rsp)
movq 0x78(%rsp), %rdi
callq 0xad30
movq %rax, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xad60
testb $0x1, %al
jne 0x5a2f
jmp 0x5c4a
leaq 0x70(%rsp), %rdi
callq 0xada0
movq %rax, 0x60(%rsp)
testb $0x1, 0xaf(%rsp)
jne 0x5ae7
movq 0x60(%rsp), %rdi
movl 0xd4(%rsp), %esi
negl %esi
movl 0xd0(%rsp), %edx
movl %edx, %ecx
negl %ecx
notl %edx
movl 0xc8(%rsp), %r8d
decl %r8d
callq 0x53e0
movl %eax, 0x24(%rsp)
jmp 0x5a7d
movl 0x24(%rsp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x5c(%rsp)
movl 0xd0(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jge 0x5ae5
movl 0x5c(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jge 0x5ae5
movq 0x60(%rsp), %rdi
movl 0xd4(%rsp), %esi
negl %esi
movl 0xcc(%rsp), %edx
negl %edx
movl 0xd0(%rsp), %ecx
negl %ecx
movl 0xc8(%rsp), %r8d
decl %r8d
callq 0x53e0
movl %eax, 0x20(%rsp)
jmp 0x5ad9
movl 0x20(%rsp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x5c(%rsp)
jmp 0x5b29
movq 0x60(%rsp), %rdi
movl 0xd4(%rsp), %esi
negl %esi
movl 0xcc(%rsp), %edx
negl %edx
movl 0xd0(%rsp), %ecx
negl %ecx
movl 0xc8(%rsp), %r8d
decl %r8d
callq 0x53e0
movl %eax, 0x1c(%rsp)
jmp 0x5b1d
movl 0x1c(%rsp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movl %eax, 0x5c(%rsp)
movb $0x0, 0xaf(%rsp)
cmpl $0x61a80, 0x5c(%rsp)    # imm = 0x61A80
je 0x5b45
cmpl $0xfff9e580, 0x5c(%rsp) # imm = 0xFFF9E580
jne 0x5b5d
movl 0x5c(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl $0x1, 0x58(%rsp)
jmp 0x5cd4
cmpl $0x30d40, 0x5c(%rsp)    # imm = 0x30D40
jl 0x5b7f
movl 0x5c(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl $0x1, 0x58(%rsp)
jmp 0x5cd4
movl 0x5c(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jl 0x5ba7
movl 0xcc(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl $0x1, 0x58(%rsp)
jmp 0x5cd4
movl 0x5c(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jle 0x5bbf
movl 0x5c(%rsp), %eax
movl %eax, 0xd0(%rsp)
callq 0x2050
movq %rax, 0x50(%rsp)
leaq 0x208b0(%rip), %rsi     # 0x26480
leaq 0x50(%rsp), %rdi
callq 0xadb0
movq %rax, 0x10(%rsp)
jmp 0x5be1
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x4c(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0xae00
jmp 0x5bfc
leaq 0x4c(%rsp), %rdi
callq 0xae40
movss %xmm0, 0xc(%rsp)
jmp 0x5c0e
movss 0xc(%rsp), %xmm0
movss 0x143e8(%rip), %xmm1    # 0x1a004
ucomiss %xmm1, %xmm0
jb 0x5c39
movl $0x61a80, 0xe4(%rsp) # imm = 0x61A80
movl $0x1, 0x58(%rsp)
jmp 0x5cd4
jmp 0x5c3b
leaq 0x70(%rsp), %rdi
callq 0xae50
jmp 0x5a17
jmp 0x5c4c
testb $0x1, 0xaf(%rsp)
jne 0x5c6e
movl 0xd0(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl $0x1, 0x58(%rsp)
jmp 0x5cd4
movq $0x0, 0x38(%rsp)
movq 0xd8(%rsp), %rdi
addq $0x8, %rdi
movl 0xd4(%rsp), %esi
leaq 0x38(%rsp), %rdx
callq 0x4800
movb %al, 0xb(%rsp)
jmp 0x5c9a
movb 0xb(%rsp), %al
testb $0x1, %al
jne 0x5ca4
jmp 0x5cc1
movl $0xfffcf2c0, %eax       # imm = 0xFFFCF2C0
subl 0xc8(%rsp), %eax
movl %eax, 0xe4(%rsp)
movl $0x1, 0x58(%rsp)
jmp 0x5cd4
movl $0x30d40, 0xe4(%rsp) # imm = 0x30D40
movl $0x1, 0x58(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0xa8d0
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
movq 0x98(%rsp), %rdi
callq 0x2380
nopl (%rax)