// File: /workspace/github_cpp_makefile_star2_O2/ATOMIC-LICHESS[P]ATOMKRAFT/pgn.cpp
// Function: appendPGN(char*, char*, char*, ResultPGN, int, int, char*, Move*, int)
void appendPGN(
	char* filename, 
	char* name_white, 
	char* name_black, 
	ResultPGN result,
	int init_time, int inc,
	char* fen, Move* moves, int move_count) {
	
	FILE* file;
	file = fopen(filename, "a");
	
	char res[20];
	switch (result) {
		case WHITE_WINS: 	strcpy(res, "1-0"); break;
		case BLACK_WINS: 	strcpy(res, "0-1"); break;
		case DRAW: 			strcpy(res, "1/2-1/2"); break;
		case NO_RESULT: 	strcpy(res, "*"); break;
		default: break;
	}
	
	time_t rawtime;
	struct tm* tinfo;
	time(&rawtime);
	tinfo = localtime (&rawtime);
	
	
	// print meta data
	fprintf(file, "[Date \"%i.%i.%i\"]\n", tinfo->tm_year + 1900, tinfo->tm_mon + 1, tinfo->tm_mday);
	fprintf(file, "[Time \"%i:%i:%i\"]\n", tinfo->tm_hour, tinfo->tm_min, tinfo->tm_sec);
	fprintf(file, "[White \"%s\"]\n", name_white);
	fprintf(file, "[Black \"%s\"]\n", name_black);
	fprintf(file, "[Result \"%s\"]\n", res);
	fprintf(file, "[TimeControl \"%i+%i\"]\n", init_time, inc);
	fprintf(file, "[Variant \"atomic\"]\n");
	
	if (strcmp(fen, "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")) {
		fprintf(file, "[FEN \"%s\"]\n", fen);
	}
	
	fprintf(file, "\n");
	
	Position pos(fen, 0, 0);
	
	// Babaschess doesn't understand move lists beginning with other numbers than 1
	int move_number = 1; //pos.startpos_ply_counter() / 2 + 1;
	Color curr_color = pos.side_to_move();
	
	// print the moves
	for (int k = 0; k < move_count; ++k) {
		
		//printf("%s\n", move_to_string(moves[k]).c_str());
		
		if (!pos.move_is_legal(moves[k])) {
			cout << "PGN: ILLEGAL MOVE, ply: " << move_count << endl;
			break;
		}
		
		
		if (curr_color == WHITE) {
			fprintf(file, "%i. %s ", move_number, move_to_san(pos, moves[k]).c_str());
		} else if (curr_color == BLACK) {	
			if (k == 0) {
				fprintf(file, "%i. ... %s ", move_number, move_to_san(pos, moves[k]).c_str());
			} else {
				fprintf(file, "%s ", move_to_san(pos, moves[k]).c_str());
			}
			++move_number;
		}
		
		if (move_number % 7 == 0) {
			// Babaschess reads no lines longer than 512 bytes!
			fprintf(file, "\n");
		}
		
		pos.do_setup_move(moves[k]);
		curr_color = pos.side_to_move();
	}
	
	// Babaschess doesn't understand this
	
	// finish the moves with '...' and the result
//	if (curr_color == WHITE) {
//		fprintf(file, "%i. ... %s\n\n", move_number, res);
//	} else if (curr_color == BLACK) {	
//		fprintf(file, "... %s\n\n", res);
//	}
	
	// Babaschess demands a final '*'
	fprintf(file, "*\n\n");
	
	fclose(file);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1998, %rsp           # imm = 0x1998
movl %r9d, %ebp
movl %r8d, %r15d
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %r13
leaq 0x12ad1(%rip), %rsi     # 0x299e4
callq 0x5470
movq %rax, %rbx
cmpl $0x3, %r14d
ja 0x16f60
movl %r14d, %eax
leaq 0x12aa9(%rip), %rcx     # 0x299d4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x302d31, 0x30(%rsp)   # imm = 0x302D31
jmp 0x16f60
movabsq $0x322f312d322f31, %rax # imm = 0x322F312D322F31
movq %rax, 0x30(%rsp)
jmp 0x16f60
movw $0x2a, 0x30(%rsp)
jmp 0x16f60
movl $0x312d30, 0x30(%rsp)   # imm = 0x312D30
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x5360
movq %r14, %rdi
callq 0x52b0
movq %rax, %r14
movl $0x76c, %edx            # imm = 0x76C
addl 0x14(%rax), %edx
movl 0xc(%rax), %r8d
movl 0x10(%rax), %ecx
incl %ecx
leaq 0x12a56(%rip), %rsi     # 0x299e6
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55a0
movl 0x8(%r14), %edx
movl (%r14), %r8d
movl 0x4(%r14), %ecx
leaq 0x12a4d(%rip), %rsi     # 0x299f9
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55a0
leaq 0x12a4f(%rip), %rsi     # 0x29a0c
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x55a0
leaq 0x12a49(%rip), %rsi     # 0x29a1a
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x55a0
leaq 0x12a43(%rip), %rsi     # 0x29a28
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55a0
leaq 0x12a3c(%rip), %rsi     # 0x29a37
movq %rbx, %rdi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x55a0
leaq 0x12a3d(%rip), %rdi     # 0x29a4e
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x5820
leaq 0x1157d(%rip), %rsi     # 0x285a3
movq 0x19d0(%rsp), %r15
movq %r15, %rdi
callq 0x5580
testl %eax, %eax
je 0x1704e
leaq 0x12a21(%rip), %rsi     # 0x29a62
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x55a0
pushq $0xa
popq %rdi
movq %rbx, 0x8(%rsp)
movq %rbx, %rsi
callq 0x54d0
leaq 0x10(%rsp), %r14
leaq 0x7(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x9c42
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x173dc
movl 0x19e0(%rsp), %ebx
movq 0x19d8(%rsp), %r13
leaq 0x10(%rsp), %rdi
callq 0x5858
leaq 0x58(%rsp), %r15
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x48(%rsp)
movl 0x1090(%r15), %ebx
pushq $0x1
popq %r12
xorl %r14d, %r14d
cmpq %r14, 0x48(%rsp)
je 0x171ff
movl (%r13,%r14,4), %esi
movq %r15, %rdi
callq 0x18b3c
testb %al, %al
je 0x171d5
cmpl $0x1, %ebx
je 0x17125
testl %ebx, %ebx
jne 0x1715c
movl (%r13,%r14,4), %edx
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x136d4
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x12976(%rip), %rsi     # 0x29a87
movl %r12d, %edx
xorl %eax, %eax
callq 0x55a0
movq %rbp, %rdi
callq 0x5858
jmp 0x171a1
testq %r14, %r14
je 0x17163
movl (%r13,%r14,4), %edx
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x136d4
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x12944(%rip), %rsi     # 0x29a97
xorl %eax, %eax
callq 0x55a0
jmp 0x17196
movq 0x8(%rsp), %rbx
jmp 0x171a1
movl (%r13,%r14,4), %edx
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x136d4
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x12903(%rip), %rsi     # 0x29a8f
movl %r12d, %edx
xorl %eax, %eax
callq 0x55a0
movq %rbp, %rdi
callq 0x5858
incl %r12d
movl %r12d, %eax
cltd
pushq $0x7
popq %rcx
idivl %ecx
testl %edx, %edx
jne 0x171b9
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x54d0
movl (%r13,%r14,4), %esi
movq %r15, %rdi
callq 0x190b8
movl 0x10e8(%rsp), %ebx
incq %r14
jmp 0x170bf
movq 0x1ddc4(%rip), %rdi     # 0x34fa0
leaq 0x1288b(%rip), %rsi     # 0x29a6e
callq 0x53b0
movq %rax, %rdi
movl 0x19e0(%rsp), %esi
callq 0x5760
movq %rax, %rdi
callq 0x51d0
leaq 0x12895(%rip), %rdi     # 0x29a9b
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x5820
movq %rbx, %rdi
callq 0x53c0
addq $0x1998, %rsp           # imm = 0x1998
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5858
movq %rbx, %rdi
callq 0x5780