// File: /workspace/github_cpp_makefile_star2_O2/moporgic[P]2048-Framework/2048.cpp
// Function: main
int main(int argc, const char* argv[]) {
	std::cout << "2048 Demo: ";
	std::copy(argv, argv + argc, std::ostream_iterator<const char*>(std::cout, " "));
	std::cout << std::endl << std::endl;

	size_t total = 1000, block = 0, limit = 0;
	std::string slide_args, place_args;
	std::string load_path, save_path;
	for (int i = 1; i < argc; i++) {
		std::string arg = argv[i];
		auto match_arg = [&](std::string flag) -> bool {
			auto it = arg.find_first_not_of('-');
			return arg.find(flag, it) == it;
		};
		auto next_opt = [&]() -> std::string {
			auto it = arg.find('=') + 1;
			return it ? arg.substr(it) : argv[++i];
		};
		if (match_arg("total")) {
			total = std::stoull(next_opt());
		} else if (match_arg("block")) {
			block = std::stoull(next_opt());
		} else if (match_arg("limit")) {
			limit = std::stoull(next_opt());
		} else if (match_arg("slide") || match_arg("play")) {
			slide_args = next_opt();
		} else if (match_arg("place") || match_arg("env")) {
			place_args = next_opt();
		} else if (match_arg("load")) {
			load_path = next_opt();
		} else if (match_arg("save")) {
			save_path = next_opt();
		}
	}

	statistics stats(total, block, limit);

	if (load_path.size()) {
		std::ifstream in(load_path, std::ios::in);
		in >> stats;
		in.close();
		if (stats.is_finished()) stats.summary();
	}

	random_slider slide(slide_args);
	random_placer place(place_args);

	while (!stats.is_finished()) {
//		std::cerr << "======== Game " << stats.step() << " ========" << std::endl;
		slide.open_episode("~:" + place.name());
		place.open_episode(slide.name() + ":~");

		stats.open_episode(slide.name() + ":" + place.name());
		episode& game = stats.back();
		while (true) {
			agent& who = game.take_turns(slide, place);
			action move = who.take_action(game.state());
//			std::cerr << game.state() << "#" << game.step() << " " << who.name() << ": " << move << std::endl;
			if (game.apply_action(move) != true) break;
			if (who.check_for_win(game.state())) break;
		}
		agent& win = game.last_turns(slide, place);
		stats.close_episode(win.name());

		slide.close_episode(win.name());
		place.close_episode(win.name());
	}

	if (save_path.size()) {
		std::ofstream out(save_path, std::ios::out | std::ios::trunc);
		out << stats;
		out.close();
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x598, %rsp            # imm = 0x598
movq %rsi, %r15
movl %edi, %ebx
leaq 0x20(%rsp), %rax
movq %rsi, (%rax)
movq 0x99df(%rip), %r12      # 0xcfd0
leaq 0x4a68(%rip), %rsi      # 0x8060
movq %r12, %rdi
callq 0x3270
movl %ebx, 0xc(%rsp)
movslq %ebx, %rax
leaq (%r15,%rax,8), %rdx
leaq 0x218(%rsp), %rcx
movq %r12, (%rcx)
leaq 0x4b48(%rip), %rax      # 0x8165
movq %rax, 0x8(%rcx)
leaq 0x390(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x40d1
movq %r12, %rdi
callq 0x3170
movq %rax, %rdi
callq 0x3170
leaq 0x1a8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %esi, %esi
leaq 0x188(%rsp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
leaq 0xf8(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %sil, (%rax)
movb %sil, (%rcx)
movb %sil, (%rdx)
leaq 0xd8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq $0x0, 0x10(%rsp)
movb %sil, (%rax)
movl $0x3e8, %r15d           # imm = 0x3E8
pushq $0x1
popq %rax
leaq 0x228(%rsp), %r12
leaq 0x350(%rsp), %rbx
movq $0x0, 0x18(%rsp)
movl %eax, 0x28(%rsp)
cmpl 0xc(%rsp), %eax
jge 0x3a3a
movq 0x20(%rsp), %rcx
cltq
movq (%rcx,%rax,8), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x3110
movq %rbx, %r13
movq %r14, 0x108(%rsp)
movq %r14, 0x48(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x370(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4951(%rip), %rsi      # 0x806c
movq %r12, %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0x3f38
movq %rbx, %rdi
movl %eax, %ebx
callq 0x3150
testb %bl, %bl
je 0x3766
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3f6e
movq %r13, %rbx
movq %r12, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x40f0
movq %rax, %r15
jmp 0x38c4
movq %r13, %rbx
movq %r13, %rdi
leaq 0x48ff(%rip), %rsi      # 0x8072
movq %r12, %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0x3f38
movq %rbx, %rdi
movl %eax, %ebx
callq 0x3150
testb %bl, %bl
je 0x37c0
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3f6e
movq %r12, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x40f0
movq %rax, 0x18(%rsp)
movq %r13, %rbx
jmp 0x38c4
leaq 0x330(%rsp), %rbp
movq %rbp, %rdi
leaq 0x48a6(%rip), %rsi      # 0x8078
movq %r12, %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbp, %rsi
callq 0x3f38
movl %eax, %ebx
movq %rbp, %rdi
callq 0x3150
testb %bl, %bl
je 0x381f
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3f6e
movq %r13, %rbx
movq %r12, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x40f0
movq %rax, 0x10(%rsp)
jmp 0x38c4
leaq 0x1f8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x484d(%rip), %rsi      # 0x807e
movq %r12, %rdx
callq 0x3110
movq %r13, %rbx
leaq 0x108(%rsp), %rdi
movq %rbp, %rsi
callq 0x3f38
testb %al, %al
je 0x385a
movq %rbp, %rdi
callq 0x3150
jmp 0x38a7
leaq 0x310(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4818(%rip), %rsi      # 0x8084
leaq 0x158(%rsp), %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0x3f38
movq %rbx, %rdi
movl %eax, %ebx
callq 0x3150
leaq 0x1f8(%rsp), %rdi
callq 0x3150
testb %bl, %bl
movq %r13, %rbx
je 0x38df
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3f6e
leaq 0x198(%rsp), %rdi
movq %r12, %rsi
callq 0x32d0
movq %r12, %rdi
callq 0x3150
movq %r14, %rdi
callq 0x3150
movl 0x28(%rsp), %eax
incl %eax
jmp 0x36c1
leaq 0x1d8(%rsp), %rbp
movq %rbp, %rdi
leaq 0x4798(%rip), %rsi      # 0x8089
movq %r12, %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbp, %rsi
callq 0x3f38
testb %al, %al
je 0x3917
movq %rbp, %rdi
callq 0x3150
jmp 0x3964
leaq 0x2f0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x4766(%rip), %rsi      # 0x808f
leaq 0x158(%rsp), %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0x3f38
movq %rbx, %rdi
movl %eax, %ebx
callq 0x3150
leaq 0x1d8(%rsp), %rdi
callq 0x3150
testb %bl, %bl
movq %r13, %rbx
je 0x397e
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3f6e
leaq 0x178(%rsp), %rdi
jmp 0x38bc
leaq 0x2d0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x4703(%rip), %rsi      # 0x8093
movq %r12, %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbp, %rsi
callq 0x3f38
movl %eax, %ebx
movq %rbp, %rdi
callq 0x3150
testb %bl, %bl
je 0x39dd
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3f6e
leaq 0xe8(%rsp), %rdi
movq %r12, %rsi
callq 0x32d0
movq %r12, %rdi
callq 0x3150
jmp 0x3a32
leaq 0x2b0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x46a9(%rip), %rsi      # 0x8098
movq %r12, %rdx
callq 0x3110
leaq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0x3f38
movq %rbx, %rdi
movl %eax, %ebx
callq 0x3150
testb %bl, %bl
je 0x3a32
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
callq 0x3f6e
movq %r13, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x38bc
movq %r13, %rbx
jmp 0x38cc
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0x410c
cmpq $0x0, 0xf0(%rsp)
je 0x3ab6
leaq 0x390(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x33d0
leaq 0x390(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x4133
leaq 0x390(%rsp), %rdi
callq 0x30a0
movq 0x60(%rsp), %rax
cmpq 0x48(%rsp), %rax
jb 0x3aa9
leaq 0x48(%rsp), %rdi
callq 0x4236
leaq 0x390(%rsp), %rdi
callq 0x30c0
leaq 0x108(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x4252
leaq 0x228(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x42ba
leaq 0x28(%rsp), %rbx
leaq 0x228(%rsp), %r14
leaq 0x390(%rsp), %r12
leaq 0x108(%rsp), %r13
leaq 0x158(%rsp), %rbp
movq 0x60(%rsp), %rax
cmpq 0x48(%rsp), %rax
jae 0x3cd3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x437a
movq %r12, %rdi
leaq 0x4573(%rip), %rsi      # 0x809d
movq %rbx, %rdx
callq 0x4355
movq %r12, %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x3150
movq %rbx, %rdi
movq %r13, %rsi
callq 0x437a
movq %r12, %rdi
movq %rbx, %rsi
leaq 0x4546(%rip), %rdx      # 0x80a0
callq 0x43dd
movq %r12, %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x3150
movq %rbp, %rdi
movq %r13, %rsi
callq 0x437a
movq %rbx, %rdi
movq %rbp, %rsi
leaq 0x4517(%rip), %rdx      # 0x809e
callq 0x43dd
leaq 0x1b8(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x437a
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x4457
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x43fc
movq %r12, %rdi
callq 0x3150
leaq 0x1b8(%rsp), %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x3150
movq %rbp, %rdi
callq 0x3150
movq 0x98(%rsp), %rbp
cmpq 0xa0(%rsp), %rbp
jne 0x3c05
movq 0xb0(%rsp), %rax
movq -0x8(%rax), %rbp
movl $0x180, %eax            # imm = 0x180
addq %rax, %rbp
addq $-0xc0, %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x44be
movq %rax, %rbx
movq (%rax), %rax
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq *0x20(%rax)
leaq 0x8ead(%rip), %rax      # 0xcae0
movq %rax, 0xb8(%rsp)
movl 0x398(%rsp), %eax
movl %eax, 0xc0(%rsp)
movq %rbp, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x4504
testb %al, %al
je 0x3c6d
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x28(%rax)
testb %al, %al
je 0x3c0c
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x4570
movq %rax, %rbp
movq (%rax), %rax
movq %r12, %rdi
movq %rbp, %rsi
callq *0x40(%rax)
leaq 0x48(%rsp), %rdi
movq %r12, %rsi
callq 0x459e
movq %r12, %rdi
callq 0x3150
movq (%rbp), %rax
movq %r12, %rdi
movq %rbp, %rsi
callq *0x40(%rax)
movq %r12, %rdi
callq 0x3150
movq (%rbp), %rax
movq %r12, %rdi
movq %rbp, %rsi
callq *0x40(%rax)
movq %r12, %rdi
callq 0x3150
leaq 0x28(%rsp), %rbx
jmp 0x3afd
cmpq $0x0, 0xd0(%rsp)
je 0x3d22
leaq 0x390(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
pushq $0x30
popq %rdx
callq 0x3470
leaq 0x390(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x45e5
leaq 0x390(%rsp), %rdi
callq 0x3340
leaq 0x390(%rsp), %rdi
callq 0x3370
leaq 0x228(%rsp), %rdi
callq 0x4644
leaq 0x108(%rsp), %rdi
callq 0x4644
leaq 0x68(%rsp), %rdi
callq 0x7524
leaq 0xc8(%rsp), %rdi
callq 0x3150
leaq 0xe8(%rsp), %rdi
callq 0x3150
leaq 0x178(%rsp), %rdi
callq 0x3150
leaq 0x198(%rsp), %rdi
callq 0x3150
xorl %eax, %eax
addq $0x598, %rsp            # imm = 0x598
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3eb7
jmp 0x3eb7
jmp 0x3eb7
jmp 0x3eb7
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
jmp 0x3e37
jmp 0x3ed4
jmp 0x3dcb
jmp 0x3eb7
jmp 0x3eb7
movq %rax, %rbx
jmp 0x3ee4
movq %rax, %rbx
jmp 0x3ef1
jmp 0x3ec9
movq %rax, %rbx
leaq 0x390(%rsp), %rdi
callq 0x3370
jmp 0x3ed7
movq %rax, %rbx
leaq 0x390(%rsp), %rdi
callq 0x30c0
jmp 0x3ef1
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
jmp 0x3e37
jmp 0x3eb7
jmp 0x3e2c
jmp 0x3eb7
jmp 0x3eb7
jmp 0x3eb7
jmp 0x3eb7
jmp 0x3e2c
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x3150
jmp 0x3eba
jmp 0x3eb7
jmp 0x3eb7
jmp 0x3ed4
jmp 0x3ed4
jmp 0x3ed4
movq %rax, %rbx
leaq 0x390(%rsp), %rdi
jmp 0x3eb0
jmp 0x3ed4
movq %rax, %rbx
jmp 0x3ea8
movq %rax, %rbx
leaq 0x390(%rsp), %rdi
callq 0x3150
jmp 0x3e7c
jmp 0x3e8f
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0x3150
jmp 0x3e9e
jmp 0x3ed4
jmp 0x3ed4
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x3eb0
jmp 0x3ed4
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3150
leaq 0x158(%rsp), %rdi
callq 0x3150
jmp 0x3ed7
movq %rax, %rbx
leaq 0x390(%rsp), %rdi
callq 0x3150
jmp 0x3efb
movq %rax, %rbx
jmp 0x3efb
jmp 0x3ed4
jmp 0x3ed4
jmp 0x3ed4
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
callq 0x4644
leaq 0x108(%rsp), %rdi
callq 0x4644
leaq 0x68(%rsp), %rdi
callq 0x7524
leaq 0xc8(%rsp), %rdi
callq 0x3150
leaq 0xe8(%rsp), %rdi
callq 0x3150
leaq 0x178(%rsp), %rdi
callq 0x3150
leaq 0x198(%rsp), %rdi
callq 0x3150
movq %rbx, %rdi
callq 0x3420
nop