// File: /workspace/github_cpp_makefile_star2_O1/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 0x5655(%rip), %rsi      # 0x9c36
leaq 0x568e(%rip), %rdx      # 0x9c76
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0x6c98
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
movq %r14, %rdi
callq 0x3074
movl $0x1, %r15d
leaq 0x78(%rsp), %rbp
leaq 0x68(%rsp), %r12
leaq 0x10(%rsp), %r13
callq 0x2040
movq %rax, %rbx
movl $0xf, %edx
movq 0x999f(%rip), %rdi      # 0xdfd8
leaq 0x5637(%rip), %rsi      # 0x9c77
callq 0x21d0
subq 0x40(%rsp), %rbx
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
divsd 0x49b6(%rip), %xmm0     # 0x9010
movq 0x9977(%rip), %rdi      # 0xdfd8
callq 0x2270
movl $0x1, %edx
movq %rax, %rdi
leaq 0x5654(%rip), %rsi      # 0x9cc9
callq 0x21d0
cmpl $0x1, %r15d
leaq 0x5612(%rip), %rsi      # 0x9c97
leaq 0x55fb(%rip), %rax      # 0x9c87
cmoveq %rax, %rsi
movl $0xf, %edx
movq 0x993c(%rip), %rdi      # 0xdfd8
callq 0x21d0
movq %r12, %rdi
movq %r14, %rsi
movl %r15d, %edx
leaq 0x20(%rsp), %rcx
callq 0x4175
movq %r12, %rdi
leaq 0x560c(%rip), %rsi      # 0x9cca
callq 0x2090
testl %eax, %eax
je 0x47c2
movl %r15d, 0xc(%rsp)
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r15
movq %r14, 0x10(%rsp)
cmpq %r15, %rbx
je 0x472d
xorl %ebp, %ebp
movq %r13, %rdi
movq %rbx, %rsi
callq 0x8a42
movzbl %al, %eax
addq %rax, %rbp
addq $0x20, %rbx
cmpq %r15, %rbx
jne 0x46e6
cmpq $0x3, %rbp
leaq 0x78(%rsp), %rbp
jb 0x472d
movl $0xc, %edx
movq 0x98c1(%rip), %rdi      # 0xdfd8
leaq 0x55ad(%rip), %rsi      # 0x9ccb
callq 0x21d0
movl 0xc(%rsp), %r15d
jmp 0x47fe
leaq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x6694
movl 0xc(%rsp), %r15d
xorl %ebx, %ebx
movq %r14, %rdi
callq 0x3074
movq $0x0, 0x10(%rsp)
movq $0x5f5e100, 0x18(%rsp)  # imm = 0x5F5E100
movq %r13, %rdi
movq %r13, %rsi
callq 0x20a0
cmpl $-0x1, %eax
jne 0x4775
callq 0x2030
cmpl $0x4, (%rax)
je 0x475b
movq %r12, %rdi
callq 0x3074
movq $0x0, 0x10(%rsp)
movq $0x5f5e100, 0x18(%rsp)  # imm = 0x5F5E100
movq %r13, %rdi
movq %r13, %rsi
callq 0x20a0
cmpl $-0x1, %eax
jne 0x47a9
callq 0x2030
cmpl $0x4, (%rax)
je 0x478f
incl %ebx
cmpl $0x3, %ebx
jne 0x4741
movq %r14, %rdi
movq %r12, %rsi
callq 0x20f0
negl %r15d
movb $0x1, %bl
jmp 0x4800
movq $0x0, 0x10(%rsp)
movq %r14, %rdi
movl %r15d, %esi
movq %r13, %rdx
callq 0x3324
testb %al, %al
leaq 0x54d7(%rip), %rsi      # 0x9cb9
leaq 0x54be(%rip), %rax      # 0x9ca7
cmovneq %rax, %rsi
movl $0x11, %edx
movq 0x97df(%rip), %rdi      # 0xdfd8
callq 0x21d0
xorl %ebx, %ebx
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4817
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21a0
testb %bl, %bl
jne 0x4625
leaq 0x20(%rsp), %rdi
callq 0x66d2
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x4845
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 0x4861
jmp 0x486a
jmp 0x4861
jmp 0x486a
movq %rax, %rbx
jmp 0x4884
jmp 0x486a
jmp 0x486a
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4884
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x21a0
leaq 0x20(%rsp), %rdi
callq 0x66d2
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x48aa
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x22c0