// File: /workspace/github_cpp_makefile_star2_O1/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 0x15935(%rip), %rsi     # 0x31ad4
callq 0x54a0
movq %rax, %rbx
cmpl $0x3, %r14d
ja 0x1c1ec
movl %r14d, %eax
leaq 0x1590d(%rip), %rcx     # 0x31ac4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x302d31, 0x30(%rsp)   # imm = 0x302D31
jmp 0x1c1ec
movabsq $0x322f312d322f31, %rax # imm = 0x322F312D322F31
movq %rax, 0x30(%rsp)
jmp 0x1c1ec
movw $0x2a, 0x30(%rsp)
jmp 0x1c1ec
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 0x158ba(%rip), %rsi     # 0x31ad6
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55c0
movl 0x8(%r14), %edx
movl (%r14), %r8d
movl 0x4(%r14), %ecx
leaq 0x158b1(%rip), %rsi     # 0x31ae9
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55c0
leaq 0x158b3(%rip), %rsi     # 0x31afc
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x55c0
leaq 0x158ad(%rip), %rsi     # 0x31b0a
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x55c0
leaq 0x158a7(%rip), %rsi     # 0x31b18
leaq 0x30(%rsp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x55c0
leaq 0x158a0(%rip), %rsi     # 0x31b27
movq %rbx, %rdi
movl %r15d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x55c0
leaq 0x158a1(%rip), %rdi     # 0x31b3e
movl $0x13, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x5840
leaq 0x143cd(%rip), %rsi     # 0x30683
movq 0x19d0(%rsp), %r15
movq %r15, %rdi
callq 0x5590
testl %eax, %eax
je 0x1c2de
leaq 0x15881(%rip), %rsi     # 0x31b52
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 0x1c55a
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1c7c2
movq %rbx, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1c331
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5400
cmpl $0x0, 0x19e0(%rsp)
jle 0x1c4fb
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 0x1e7c0
testb %al, %al
je 0x1c4a7
cmpl $0x1, %ebx
je 0x1c3db
testl %ebx, %ebx
jne 0x1c40f
movl (%rbp,%r13,4), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x17895
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x157cb(%rip), %rsi     # 0x31b77
movl %r15d, %edx
xorl %eax, %eax
callq 0x55c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1c465
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5400
jmp 0x1c465
testq %r13, %r13
je 0x1c416
movl (%rbp,%r13,4), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x17895
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x15781(%rip), %rsi     # 0x31b87
xorl %eax, %eax
callq 0x55c0
jmp 0x1c446
movq 0x8(%rsp), %rbx
jmp 0x1c465
movl (%rbp,%r13,4), %edx
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x17895
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x15743(%rip), %rsi     # 0x31b7f
movl %r15d, %edx
xorl %eax, %eax
callq 0x55c0
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1c462
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 0x1c485
movl $0xa, %edi
movq %rbx, %rsi
callq 0x5500
movl (%rbp,%r13,4), %esi
movq %r14, %rdi
callq 0x1eeae
movl 0x10e8(%rsp), %ebx
incq %r13
cmpq %r13, %r12
jne 0x1c364
jmp 0x1c4fb
movq 0x1faf2(%rip), %r14     # 0x3bfa0
leaq 0x156a9(%rip), %rsi     # 0x31b5e
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 0x15689(%rip), %rdi     # 0x31b8b
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 0x1c551
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x5400
movq %rbx, %rdi
callq 0x5790
nop