// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5f8, %rsp            # imm = 0x5F8
movq (%rdx), %rax
movq %rdi, %rbx
cmpq %rax, 0x8(%rdx)
je 0x1e557
movq %rsi, %r14
movq (%rax), %rsi
movq %rdx, %r15
leaq 0x3f0(%rsp), %r12
pushq $0x8
popq %rdx
movq %r12, %rdi
callq 0x5690
movq (%r12), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x410(%rsp,%rax)
je 0x1e598
movq (%r15), %rax
movq (%rax), %rsi
leaq 0x1f0(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x5380
movq 0x1f0(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x210(%rsp,%rax)
je 0x1e609
movq (%r15), %rdx
leaq 0x81db(%rip), %rsi      # 0x266e6
leaq 0x48(%rsp), %rdi
callq 0x8070
leaq 0x81d8(%rip), %rdx      # 0x266f4
leaq 0x190(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x8051
leaq 0x190(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8d00
leaq 0x190(%rsp), %rdi
callq 0x5150
leaq 0x48(%rsp), %rdi
jmp 0x1e8a8
leaq 0x8151(%rip), %rsi      # 0x266af
leaq 0x1d0(%rsp), %rdi
leaq 0x3f0(%rsp), %rdx
callq 0x5110
pushq $0x1
popq %rsi
leaq 0x1d0(%rsp), %rdx
movq %rbx, %rdi
callq 0x8d00
leaq 0x1d0(%rsp), %rdi
callq 0x5150
jmp 0x1e8c7
leaq 0x3f0(%rsp), %rdi
callq 0x5070
movq (%r15), %rdx
leaq 0x8121(%rip), %rsi      # 0x266d0
leaq 0x1f0(%rsp), %rdi
callq 0x8070
leaq 0x8113(%rip), %rdx      # 0x266d6
leaq 0x1b0(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x8051
leaq 0x1b0(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8d00
leaq 0x1b0(%rsp), %rdi
callq 0x5150
leaq 0x1f0(%rsp), %rdi
callq 0x5150
jmp 0x1e8ba
movq (%r15), %rdi
movq 0x8(%r15), %rax
subq %rdi, %rax
cmpq $0x21, %rax
jb 0x1e626
addq $0x20, %rdi
callq 0x15762
movl %eax, %ebp
jmp 0x1e629
pushq $-0x1
popq %rbp
leaq 0x10(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x1f248
leaq 0x8013(%rip), %rsi      # 0x26655
leaq 0x170(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x5110
leaq 0x10(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x1f318
leaq 0x170(%rsp), %rdi
callq 0x5150
andq $0x0, 0xe0(%rsp)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, 0xd0(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x1dce0
leaq 0x80(%rsp), %rax
leaq 0x38(%rsp), %rcx
movq -0x10(%rax), %rdx
movq %rcx, -0x10(%rcx)
cmpq %rax, %rdx
je 0x1e6c1
movq 0x80(%rsp), %rcx
movq %rdx, 0x28(%rsp)
movq %rcx, 0x38(%rsp)
jmp 0x1e6c9
vmovups (%rax), %xmm0
vmovups %xmm0, (%rcx)
movq 0x78(%rsp), %rcx
movq %rax, 0x70(%rsp)
andq $0x0, 0x78(%rsp)
andq $0x0, 0xc0(%rsp)
vxorps %xmm0, %xmm0, %xmm0
movb $0x0, 0x80(%rsp)
vmovaps %xmm0, 0xb0(%rsp)
movq %rcx, 0x30(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x1dd6a
leaq 0x160(%rsp), %rax
leaq 0xa0(%rsp), %rcx
movq -0x10(%rax), %rdx
movq %rcx, -0x10(%rcx)
cmpq %rax, %rdx
je 0x1e740
movq 0x160(%rsp), %rcx
movq %rdx, 0x90(%rsp)
movq %rcx, 0xa0(%rsp)
jmp 0x1e748
vmovups (%rax), %xmm0
vmovups %xmm0, (%rcx)
movq 0x158(%rsp), %rcx
movq %rax, 0x150(%rsp)
andq $0x0, 0x158(%rsp)
leaq 0x90(%rsp), %rdx
movb $0x0, 0x160(%rsp)
movq %rcx, 0x8(%rdx)
leaq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1f3be
leaq 0x90(%rsp), %rdi
callq 0x5150
leaq 0x128(%rsp), %rdi
callq 0x7ef6
leaq 0xb0(%rsp), %rdi
callq 0x8e84
leaq 0x28(%rsp), %rdi
callq 0x5150
leaq 0x48(%rsp), %rdi
callq 0x7ef6
leaq 0xd0(%rsp), %rdi
callq 0x8e84
leaq 0x80(%r14), %rsi
leaq 0x108(%rsp), %rdi
callq 0x1ef62
leaq 0x10(%rsp), %rdi
leaq 0x108(%rsp), %rsi
callq 0x1f49c
leaq 0x108(%rsp), %rdi
callq 0x5150
leaq 0x10(%rsp), %rdi
callq 0x1f542
leaq 0x869a(%rip), %rax      # 0x26eaa
movl %ebp, 0xc(%rsp)
movq 0x68(%r14), %r12
movq 0x70(%r14), %r13
leaq 0x10(%rsp), %r15
movb (%rax), %bpl
cmpq %r13, %r12
je 0x1e847
movzwl (%r12), %edx
movzbl %bpl, %esi
movq %r15, %rdi
callq 0xe492
movb $0x3, %al
addq $0x2, %r12
subb %bpl, %al
movl %eax, %ebp
jmp 0x1e824
movq 0xd8(%r14), %rdi
movq (%rdi), %rax
movl 0xc(%rsp), %edx
leaq 0x10(%rsp), %rsi
callq *0x60(%rax)
leaq 0x10(%rsp), %rdi
callq 0x1f5be
leaq 0x1f0(%rsp), %rdi
callq 0x54b0
leaq 0x4062(%rip), %rsi      # 0x228dd
leaq 0xe8(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x5110
pushq $0x1
popq %rsi
leaq 0xe8(%rsp), %rdx
movq %rbx, %rdi
callq 0x8d00
leaq 0xe8(%rsp), %rdi
callq 0x5150
leaq 0x1f0(%rsp), %rdi
callq 0x5500
leaq 0x3f0(%rsp), %rdi
callq 0x50a0
movq %rbx, %rax
addq $0x5f8, %rsp            # imm = 0x5F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1e9cb
leaq 0xe8(%rsp), %rdi
jmp 0x1e959
jmp 0x1e9cb
leaq 0x108(%rsp), %rdi
jmp 0x1e959
leaq 0x90(%rsp), %rdi
movq %rax, %rbx
callq 0x5150
leaq 0x128(%rsp), %rdi
callq 0x7ef6
jmp 0x1e91c
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x8e84
leaq 0x28(%rsp), %rdi
callq 0x5150
leaq 0x48(%rsp), %rdi
callq 0x7ef6
jmp 0x1e942
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x8e84
jmp 0x1e9ce
leaq 0x170(%rsp), %rdi
movq %rax, %rbx
jmp 0x1e9a2
jmp 0x1e9cb
leaq 0x1b0(%rsp), %rdi
movq %rax, %rbx
callq 0x5150
jmp 0x1e975
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
callq 0x5150
jmp 0x1e9db
jmp 0x1e9c2
jmp 0x1e9c2
leaq 0x190(%rsp), %rdi
movq %rax, %rbx
callq 0x5150
jmp 0x1e99d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5150
jmp 0x1e9ce
jmp 0x1e9cb
leaq 0x1d0(%rsp), %rdi
movq %rax, %rbx
callq 0x5150
jmp 0x1e9e8
movq %rax, %rbx
jmp 0x1e9e8
movq %rax, %rbx
jmp 0x1e9db
jmp 0x1e9cb
jmp 0x1e9cb
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
callq 0x5500
leaq 0x3f0(%rsp), %rdi
callq 0x50a0
movq %rbx, %rdi
callq 0x5650