// File: /workspace/github_cpp_makefile_star2_O3/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 0x16485(%rip), %rsi     # 0x32b54
callq 0x54a0
movq %rax, %rbx
cmpl $0x3, %r14d
ja 0x1c71c
movl %r14d, %eax
leaq 0x1645d(%rip), %rcx     # 0x32b44
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x302d31, 0x30(%rsp)   # imm = 0x302D31
jmp 0x1c71c
movabsq $0x322f312d322f31, %rax # imm = 0x322F312D322F31
movq %rax, 0x30(%rsp)
jmp 0x1c71c
movw $0x2a, 0x30(%rsp)
jmp 0x1c71c
movl $0x312d30, 0x30(%rsp)   # imm = 0x312D30
leaq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x5370
movq %r14, %rdi
callq 0x52d0
movq %rax, %r14
movl $0x76c, %edx            # imm = 0x76C
addl 0x14(%rax), %edx
movl 0xc(%rax), %r8d
movl 0x10(%rax), %ecx
incl %ecx
leaq 0x1640a(%rip), %rsi     # 0x32b56
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55c0
movl 0x8(%r14), %edx
movl (%r14), %r8d
movl 0x4(%r14), %ecx
leaq 0x16401(%rip), %rsi     # 0x32b69
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55c0
leaq 0x16403(%rip), %rsi     # 0x32b7c
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x55c0
leaq 0x163fd(%rip), %rsi     # 0x32b8a
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x55c0
leaq 0x163f7(%rip), %rsi     # 0x32b98
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55c0
leaq 0x163f0(%rip), %rsi     # 0x32ba7
movq %rbx, %rdi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x55c0
leaq 0x163f1(%rip), %rdi     # 0x32bbe
movl $0x13, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x5840
leaq 0x14f1d(%rip), %rsi     # 0x31703
movq 0x19d0(%rsp), %r15
movq %r15, %rdi
callq 0x5590
testl %eax, %eax
je 0x1c80e
leaq 0x163d1(%rip), %rsi     # 0x32bd2
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x55c0
movl $0xa, %edi
movq %rbx, %rsi
callq 0x5500
leaq 0x10(%rsp), %r14
leaq 0x7(%rsp), %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1ca8a
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1ccf2
movq %rbx, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1c861
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5400
cmpl $0x0, 0x19e0(%rsp)
jle 0x1ca2b
movq 0x19d8(%rsp), %rbp
leaq 0x58(%rsp), %r14
movl 0x1090(%r14), %ebx
movl 0x19e0(%rsp), %r12d
movl $0x1, %r15d
xorl %r13d, %r13d
movl (%rbp,%r13,4), %esi
movq %r14, %rdi
callq 0x1ece2
testb %al, %al
je 0x1c9d7
cmpl $0x1, %ebx
je 0x1c90b
testl %ebx, %ebx
jne 0x1c93f
movl (%rbp,%r13,4), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x17df0
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1631b(%rip), %rsi     # 0x32bf7
movl %r15d, %edx
xorl %eax, %eax
callq 0x55c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1c995
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x1c995
testq %r13, %r13
je 0x1c946
movl (%rbp,%r13,4), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x17df0
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x162d1(%rip), %rsi     # 0x32c07
xorl %eax, %eax
callq 0x55c0
jmp 0x1c976
movq 0x8(%rsp), %rbx
jmp 0x1c995
movl (%rbp,%r13,4), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x17df0
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x16293(%rip), %rsi     # 0x32bff
movl %r15d, %edx
xorl %eax, %eax
callq 0x55c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1c992
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5400
incl %r15d
imull $0xb6db6db7, %r15d, %eax # imm = 0xB6DB6DB7
addl $0x12492492, %eax       # imm = 0x12492492
cmpl $0x24924924, %eax       # imm = 0x24924924
ja 0x1c9b5
movl $0xa, %edi
movq %rbx, %rsi
callq 0x5500
movl (%rbp,%r13,4), %esi
movq %r14, %rdi
callq 0x1f374
movl 0x10e8(%rsp), %ebx
incq %r13
cmpq %r13, %r12
jne 0x1c894
jmp 0x1ca2b
movq 0x205c2(%rip), %r14     # 0x3cfa0
leaq 0x161f9(%rip), %rsi     # 0x32bde
movl $0x18, %edx
movq %r14, %rdi
callq 0x5460
movq %r14, %rdi
movl 0x19e0(%rsp), %esi
callq 0x5760
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x53c0
movsbl %al, %esi
movq %r14, %rdi
callq 0x5050
movq %rax, %rdi
callq 0x5270
leaq 0x161d9(%rip), %rdi     # 0x32c0b
movl $0x3, %esi
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
callq 0x5840
movq %rbx, %rdi
callq 0x53e0
addq $0x1998, %rsp           # imm = 0x1998
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1ca81
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5400
movq %rbx, %rdi
callq 0x5790
nop