// File: /workspace/github_cpp_makefile_star2_O3/vygr[P]C-Chess/chess.cpp
// Function: main
int main(int, const char *[])
{
	//setup first board, loop for white..black..white..black...
	auto game_start_time = std::chrono::high_resolution_clock::now();
	auto brd = board("rnbqkbnrpppppppp                                PPPPPPPPRNBQKBNR");
	//auto brd = board("rnb kbnrpppppppp                                PPPPPPPPRNBQKBNR");
	//auto brd = board(" k                         Q P     Q P  K                       ");
	//auto brd = board(" k                           P     Q P  K                       ");
	//auto brd = board("        p         k    p   rb         p      r              K   ");
	//auto brd = board("        p         k    p   r          p      r              K   ");
	//auto brd = board("                   k               K         Q                  ");
	//auto brd = board("    k     R                               K                      ");
	//auto brd = board("   k              KBB                                            ");
	auto history = boards{};
	auto colour = white;
	display_board(brd);
	for (;;)
	{
		auto end_time = std::chrono::high_resolution_clock::now();
		std::chrono::duration<double> elapsed = end_time - game_start_time;
		std::cout << "\nElapsed Time: " << elapsed.count() << "\n";
		if (colour == white)
		{
			std::cout << "White to move:\n";
		}
		else
		{
			std::cout << "Black to move:\n";
		}
		auto new_brd = best_move(brd, colour, history);
		if (new_brd == "")
		{
			std::size_t king_index = 0;
			if (in_check(brd, colour, king_index))
			{
				std::cout << "\n** Checkmate **\n";
			}
			else
			{
				std::cout << "\n** Stalemate **\n";
			}
			break;
		}
		auto rep = std::count(begin(history), end(history), brd);
		if (rep >= 3)
		{
			std::cout << "\n** Draw **\n";
			break;
		}
		history.push_back(new_brd);
		for (auto i = 0; i < 3; ++i)
		{
			display_board(brd);
			std::this_thread::sleep_for(std::chrono::duration<float>(0.1));
			display_board(new_brd);
			std::this_thread::sleep_for(std::chrono::duration<float>(0.1));
		}
		colour = -colour;
		brd = new_brd;
	}
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
callq 0x2040
movq %rax, 0x40(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x5702(%rip), %rsi      # 0x9c36
leaq 0x573b(%rip), %rdx      # 0x9c76
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x6bce
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq %r14, %rdi
callq 0x3064
movl $0x1, %r15d
leaq 0x78(%rsp), %rbp
leaq 0x68(%rsp), %r12
leaq 0x10(%rsp), %r13
callq 0x2040
movq %rax, %rbx
movl $0xf, %edx
movq 0x9a4c(%rip), %rdi      # 0xdfd8
leaq 0x56e4(%rip), %rsi      # 0x9c77
callq 0x21d0
subq 0x40(%rsp), %rbx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
divsd 0x4a63(%rip), %xmm0     # 0x9010
movq 0x9a24(%rip), %rdi      # 0xdfd8
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5701(%rip), %rsi      # 0x9cc9
callq 0x21d0
cmpl $0x1, %r15d
leaq 0x56bf(%rip), %rsi      # 0x9c97
leaq 0x56a8(%rip), %rax      # 0x9c87
cmoveq %rax, %rsi
movl $0xf, %edx
movq 0x99e9(%rip), %rdi      # 0xdfd8
callq 0x21d0
movq %r12, %rdi
movq %r14, %rsi
movl %r15d, %edx
leaq 0x20(%rsp), %rcx
callq 0x4108
movq %r12, %rdi
leaq 0x56b9(%rip), %rsi      # 0x9cca
callq 0x2090
testl %eax, %eax
je 0x4715
movl %r15d, 0xc(%rsp)
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r15
movq %r14, 0x10(%rsp)
cmpq %r15, %rbx
je 0x4680
xorl %ebp, %ebp
movq %r13, %rdi
movq %rbx, %rsi
callq 0x8918
movzbl %al, %eax
addq %rax, %rbp
addq $0x20, %rbx
cmpq %r15, %rbx
jne 0x4639
cmpq $0x3, %rbp
leaq 0x78(%rsp), %rbp
jb 0x4680
movl $0xc, %edx
movq 0x996e(%rip), %rdi      # 0xdfd8
leaq 0x565a(%rip), %rsi      # 0x9ccb
callq 0x21d0
movl 0xc(%rsp), %r15d
jmp 0x4751
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x65c4
movl 0xc(%rsp), %r15d
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x3064
movq $0x0, 0x10(%rsp)
movq $0x5f5e100, 0x18(%rsp)  # imm = 0x5F5E100
movq %r13, %rdi
movq %r13, %rsi
callq 0x20a0
cmpl $-0x1, %eax
jne 0x46c8
callq 0x2030
cmpl $0x4, (%rax)
je 0x46ae
movq %r12, %rdi
callq 0x3064
movq $0x0, 0x10(%rsp)
movq $0x5f5e100, 0x18(%rsp)  # imm = 0x5F5E100
movq %r13, %rdi
movq %r13, %rsi
callq 0x20a0
cmpl $-0x1, %eax
jne 0x46fc
callq 0x2030
cmpl $0x4, (%rax)
je 0x46e2
incl %ebx
cmpl $0x3, %ebx
jne 0x4694
movq %r14, %rdi
movq %r12, %rsi
callq 0x20f0
negl %r15d
movb $0x1, %bl
jmp 0x4753
movq $0x0, 0x10(%rsp)
movq %r14, %rdi
movl %r15d, %esi
movq %r13, %rdx
callq 0x32c9
testb %al, %al
leaq 0x5584(%rip), %rsi      # 0x9cb9
leaq 0x556b(%rip), %rax      # 0x9ca7
cmovneq %rax, %rsi
movl $0x11, %edx
movq 0x988c(%rip), %rdi      # 0xdfd8
callq 0x21d0
xorl %ebx, %ebx
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x476a
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21a0
testb %bl, %bl
jne 0x4578
leaq 0x20(%rsp), %rdi
callq 0x6602
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x4798
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x47b4
jmp 0x47bd
jmp 0x47b4
jmp 0x47bd
movq %rax, %rbx
jmp 0x47d7
jmp 0x47bd
jmp 0x47bd
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x47d7
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x20(%rsp), %rdi
callq 0x6602
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x47fd
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22c0