// File: tewalds[P]morat/hex/gtpgeneral.cpp
// Function: Morat::Hex::GTP::gtp_save_sgf(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>)
GTPResponse GTP::gtp_save_sgf(vecstr args){
	int limit = -1;
	if(args.size() == 0)
		return GTPResponse(true, "save_sgf <filename> [work limit]");

	std::ifstream infile(args[0].c_str());

	if(infile) {
		infile.close();
		return GTPResponse(false, "File " + args[0] + " already exists");
	}

	std::ofstream outfile(args[0].c_str());

	if(!outfile)
		return GTPResponse(false, "Opening file " + args[0] + " for writing failed");

	if(args.size() > 1)
		limit = from_str<unsigned int>(args[1]);

	SGFPrinter<Move> sgf(outfile);
	sgf.game(Board::name);
	sgf.program(gtp_name(vecstr()).response, gtp_version(vecstr()).response);
	sgf.size(hist->size());

	sgf.end_root();

	Side s = Side::P1;
	for(auto m : hist){
		sgf.move(s, m);
		s = ~s;
	}

	agent->gen_sgf(sgf, limit);

	sgf.end();
	outfile.close();
	return true;
}

// Assembly:
subq $0x718, %rsp            # imm = 0x718
movq %rdx, 0x60(%rsp)
movq %rdi, %rax
movq 0x60(%rsp), %rdi
movq %rax, 0x68(%rsp)
movq %rax, %rcx
movq %rcx, 0x70(%rsp)
movq %rax, 0x710(%rsp)
movq %rsi, 0x708(%rsp)
movq %rdi, 0x700(%rsp)
movq 0x708(%rsp), %rax
movq %rax, 0x78(%rsp)
movl $0xffffffff, 0x6fc(%rsp) # imm = 0xFFFFFFFF
callq 0x13c80
cmpq $0x0, %rax
jne 0x4240e
leaq 0x6d7(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x5770
movq 0x58(%rsp), %rdx
leaq 0xe2c5(%rip), %rsi      # 0x50643
leaq 0x6d8(%rsp), %rdi
callq 0x5590
jmp 0x4238d
movq 0x68(%rsp), %rdi
movl $0x1, %esi
leaq 0x6d8(%rsp), %rdx
callq 0xd9a0
jmp 0x423a6
leaq 0x6d8(%rsp), %rdi
callq 0x5260
leaq 0x6d7(%rsp), %rdi
callq 0x5450
jmp 0x42c1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x423fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x6d8(%rsp), %rdi
callq 0x5260
leaq 0x6d7(%rsp), %rdi
callq 0x5450
jmp 0x42c29
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13ca0
movq %rax, %rdi
callq 0x51a0
movq %rax, %rsi
leaq 0x4b8(%rsp), %rdi
movl $0x8, %edx
callq 0x57d0
movq 0x4b8(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x4b8(%rsp,%rax), %rdi
callq 0x5510
movb %al, 0x57(%rsp)
jmp 0x42458
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0x42465
jmp 0x4255b
leaq 0x4b8(%rsp), %rdi
callq 0x50c0
jmp 0x42474
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13ca0
movq %rax, %rdx
leaq 0xe1d8(%rip), %rsi      # 0x50664
leaq 0x478(%rsp), %rdi
callq 0xb960
jmp 0x4249b
leaq 0xe1c8(%rip), %rdx      # 0x5066a
leaq 0x498(%rsp), %rdi
leaq 0x478(%rsp), %rsi
callq 0xb910
jmp 0x424b9
movq 0x68(%rsp), %rdi
xorl %esi, %esi
leaq 0x498(%rsp), %rdx
callq 0xd9a0
jmp 0x424cf
leaq 0x498(%rsp), %rdi
callq 0x5260
leaq 0x478(%rsp), %rdi
callq 0x5260
movl $0x1, 0x474(%rsp)
jmp 0x42bfe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42c0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42549
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x498(%rsp), %rdi
callq 0x5260
leaq 0x478(%rsp), %rdi
callq 0x5260
jmp 0x42c0d
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13ca0
movq %rax, %rdi
callq 0x51a0
movq %rax, %rsi
leaq 0x270(%rsp), %rdi
movl $0x10, %edx
callq 0x5460
jmp 0x42588
movq 0x270(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x270(%rsp,%rax), %rdi
callq 0x51e0
movb %al, 0x56(%rsp)
jmp 0x425a7
movb 0x56(%rsp), %al
testb $0x1, %al
jne 0x425b4
jmp 0x4269b
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x13ca0
movq %rax, %rdx
leaq 0xe0ae(%rip), %rsi      # 0x5067a
leaq 0x230(%rsp), %rdi
callq 0xb960
jmp 0x425db
leaq 0xe0a6(%rip), %rdx      # 0x50688
leaq 0x250(%rsp), %rdi
leaq 0x230(%rsp), %rsi
callq 0xb910
jmp 0x425f9
movq 0x68(%rsp), %rdi
xorl %esi, %esi
leaq 0x250(%rsp), %rdx
callq 0xd9a0
jmp 0x4260f
leaq 0x250(%rsp), %rdi
callq 0x5260
leaq 0x230(%rsp), %rdi
callq 0x5260
movl $0x1, 0x474(%rsp)
jmp 0x42be0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42bef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42689
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x250(%rsp), %rdi
callq 0x5260
leaq 0x230(%rsp), %rdi
callq 0x5260
jmp 0x42bef
movq 0x60(%rsp), %rdi
callq 0x13c80
cmpq $0x1, %rax
jbe 0x426d3
movq 0x60(%rsp), %rdi
movl $0x1, %esi
callq 0x13ca0
movq %rax, %rdi
callq 0x2fff0
movl %eax, 0x50(%rsp)
jmp 0x426c8
movl 0x50(%rsp), %eax
movl %eax, 0x6fc(%rsp)
leaq 0x218(%rsp), %rdi
leaq 0x270(%rsp), %rsi
callq 0x43c60
jmp 0x426ea
leaq 0x1f7(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x5770
movq 0x48(%rsp), %rdx
leaq 0xdee1(%rip), %rsi      # 0x505e9
leaq 0x1f8(%rsp), %rdi
callq 0x5590
jmp 0x42717
leaq 0x218(%rsp), %rdi
leaq 0x1f8(%rsp), %rsi
callq 0x43e30
jmp 0x4272e
leaq 0x1f8(%rsp), %rdi
callq 0x5260
leaq 0x1f7(%rsp), %rdi
callq 0x5450
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0x170(%rsp)
movq $0x0, 0x180(%rsp)
leaq 0x170(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x440d0
movq 0x78(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0x188(%rsp), %rdi
callq 0x413a0
jmp 0x4278c
leaq 0x1b0(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0x5240
vpxor %xmm0, %xmm0, %xmm0
vmovdqa %xmm0, 0xf0(%rsp)
movq $0x0, 0x100(%rsp)
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x440d0
movq 0x78(%rsp), %rsi
movq 0x38(%rsp), %rdx
leaq 0x108(%rsp), %rdi
callq 0x414a0
jmp 0x427e5
leaq 0x130(%rsp), %rsi
leaq 0x150(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x5240
movq 0x30(%rsp), %rdx
leaq 0x218(%rsp), %rdi
leaq 0x1d0(%rsp), %rsi
callq 0x43f50
jmp 0x4281b
leaq 0x150(%rsp), %rdi
callq 0x5260
leaq 0x108(%rsp), %rdi
callq 0xb5f0
leaq 0xf0(%rsp), %rdi
callq 0xe140
leaq 0x1d0(%rsp), %rdi
callq 0x5260
leaq 0x188(%rsp), %rdi
callq 0xb5f0
leaq 0x170(%rsp), %rdi
callq 0xe140
movq 0x78(%rsp), %rdi
addq $0x68, %rdi
callq 0x3ea90
movq %rax, 0x28(%rsp)
jmp 0x4287e
movq 0x28(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x43630
jmp 0x42892
leaq 0x218(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x440e0
jmp 0x428a9
leaq 0xd0(%rsp), %rdi
callq 0x5260
leaq 0x218(%rsp), %rdi
callq 0x44200
jmp 0x428c5
movq 0x78(%rsp), %rax
leaq 0x2abeb(%rip), %rcx     # 0x6d4bc
movb (%rcx), %cl
movb %cl, 0xcf(%rsp)
addq $0x68, %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x439e0
movq %rax, 0x20(%rsp)
jmp 0x428fa
movq 0x20(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x43a10
movq %rax, 0x18(%rsp)
jmp 0x4291b
movq 0x18(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1c750
testb $0x1, %al
jne 0x42946
jmp 0x42ae9
leaq 0xb8(%rsp), %rdi
callq 0x1c790
movw (%rax), %ax
movw %ax, 0xae(%rsp)
movb 0xcf(%rsp), %al
movb %al, 0xad(%rsp)
movw 0xae(%rsp), %ax
movw %ax, 0xaa(%rsp)
movzwl 0xaa(%rsp), %edx
movzbl 0xad(%rsp), %esi
leaq 0x218(%rsp), %rdi
callq 0x1cd70
jmp 0x4299b
movb 0xcf(%rsp), %al
movb %al, 0xa8(%rsp)
movzbl 0xa8(%rsp), %edi
callq 0x1c680
movb %al, 0x17(%rsp)
jmp 0x429bc
movb 0x17(%rsp), %al
movb %al, 0xa9(%rsp)
movb 0xa9(%rsp), %al
movb %al, 0xcf(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x1c7a0
jmp 0x42928
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42a1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x5260
leaq 0x1f7(%rsp), %rdi
callq 0x5450
jmp 0x42bef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42ab1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42a8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x5260
leaq 0x108(%rsp), %rdi
callq 0xb5f0
leaq 0xf0(%rsp), %rdi
callq 0xe140
leaq 0x1d0(%rsp), %rdi
callq 0x5260
leaq 0x188(%rsp), %rdi
callq 0xb5f0
leaq 0x170(%rsp), %rdi
callq 0xe140
jmp 0x42bef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x5260
jmp 0x42bef
movq 0x78(%rsp), %rax
movq 0xd8(%rax), %rdi
movl 0x6fc(%rsp), %edx
movq (%rdi), %rax
movq 0x60(%rax), %rax
leaq 0x218(%rsp), %rsi
callq *%rax
jmp 0x42b0f
leaq 0x218(%rsp), %rdi
callq 0x442d0
jmp 0x42b1e
leaq 0x270(%rsp), %rdi
callq 0x55d0
jmp 0x42b2d
leaq 0x87(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x5770
movq 0x8(%rsp), %rdx
leaq 0x9d9e(%rip), %rsi      # 0x4c8e9
leaq 0x88(%rsp), %rdi
callq 0x5590
jmp 0x42b5a
movq 0x68(%rsp), %rdi
movl $0x1, %esi
leaq 0x88(%rsp), %rdx
callq 0xd9a0
jmp 0x42b73
leaq 0x88(%rsp), %rdi
callq 0x5260
leaq 0x87(%rsp), %rdi
callq 0x5450
movl $0x1, 0x474(%rsp)
jmp 0x42be0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
jmp 0x42bd1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x6c8(%rsp)
movl %eax, 0x6c4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x5260
leaq 0x87(%rsp), %rdi
callq 0x5450
jmp 0x42bef
leaq 0x270(%rsp), %rdi
callq 0x5630
jmp 0x42bfe
leaq 0x270(%rsp), %rdi
callq 0x5630
jmp 0x42c0d
leaq 0x4b8(%rsp), %rdi
callq 0x5100
jmp 0x42c1c
leaq 0x4b8(%rsp), %rdi
callq 0x5100
jmp 0x42c29
movq 0x70(%rsp), %rax
addq $0x718, %rsp            # imm = 0x718
retq
movq 0x6c8(%rsp), %rdi
callq 0x5760
nopw %cs:(%rax,%rax)