// File: 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:
subq $0x1a78, %rsp           # imm = 0x1A78
movl 0x1a90(%rsp), %eax
movq 0x1a88(%rsp), %rax
movq 0x1a80(%rsp), %rax
movq %rdi, 0x1a70(%rsp)
movq %rsi, 0x1a68(%rsp)
movq %rdx, 0x1a60(%rsp)
movl %ecx, 0x1a5c(%rsp)
movl %r8d, 0x1a58(%rsp)
movl %r9d, 0x1a54(%rsp)
movq 0x1a70(%rsp), %rdi
leaq 0x250f4(%rip), %rsi     # 0x5ab60
callq 0x65f0
movq %rax, 0x1a48(%rsp)
movl 0x1a5c(%rsp), %eax
movq %rax, 0x38(%rsp)
subq $0x3, %rax
ja 0x35af8
movq 0x38(%rsp), %rax
leaq 0x250b9(%rip), %rcx     # 0x5ab50
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x1a30(%rsp), %rdi
leaq 0x250b3(%rip), %rsi     # 0x5ab62
callq 0x6460
jmp 0x35afa
leaq 0x1a30(%rsp), %rdi
leaq 0x250a1(%rip), %rsi     # 0x5ab66
callq 0x6460
jmp 0x35afa
leaq 0x1a30(%rsp), %rdi
leaq 0x2508f(%rip), %rsi     # 0x5ab6a
callq 0x6460
jmp 0x35afa
leaq 0x1a30(%rsp), %rdi
leaq 0x25081(%rip), %rsi     # 0x5ab72
callq 0x6460
jmp 0x35afa
jmp 0x35afa
leaq 0x1a28(%rsp), %rdi
callq 0x6450
leaq 0x1a28(%rsp), %rdi
callq 0x6370
movq %rax, 0x1a20(%rsp)
movq 0x1a48(%rsp), %rdi
movq 0x1a20(%rsp), %rax
movl 0x14(%rax), %edx
addl $0x76c, %edx            # imm = 0x76C
movq 0x1a20(%rsp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movq 0x1a20(%rsp), %rax
movl 0xc(%rax), %r8d
leaq 0x2501e(%rip), %rsi     # 0x5ab74
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
movq 0x1a20(%rsp), %rax
movl 0x8(%rax), %edx
movq 0x1a20(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x1a20(%rsp), %rax
movl (%rax), %r8d
leaq 0x24ffa(%rip), %rsi     # 0x5ab87
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
movq 0x1a68(%rsp), %rdx
leaq 0x24fef(%rip), %rsi     # 0x5ab9a
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
movq 0x1a60(%rsp), %rdx
leaq 0x24fdf(%rip), %rsi     # 0x5aba8
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
leaq 0x1a30(%rsp), %rdx
leaq 0x24fcf(%rip), %rsi     # 0x5abb6
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
movl 0x1a58(%rsp), %edx
movl 0x1a54(%rsp), %ecx
leaq 0x24fba(%rip), %rsi     # 0x5abc5
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
leaq 0x24fbb(%rip), %rsi     # 0x5abdc
movb $0x0, %al
callq 0x6760
movq 0x1a80(%rsp), %rdi
leaq 0x2396c(%rip), %rsi     # 0x595a3
callq 0x6740
cmpl $0x0, %eax
je 0x35c5f
movq 0x1a48(%rsp), %rdi
movq 0x1a80(%rsp), %rdx
leaq 0x24f98(%rip), %rsi     # 0x5abf0
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
leaq 0x24fbd(%rip), %rsi     # 0x5ac2b
xorl %eax, %eax
callq 0x6760
movq 0x1a80(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xbf(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x6980
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0xc0(%rsp), %rdi
callq 0x12070
jmp 0x35cad
leaq 0xe0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x36520
jmp 0x35cc8
leaq 0xc0(%rsp), %rdi
callq 0x6a78
leaq 0xbf(%rsp), %rdi
callq 0x6550
movl $0x1, 0xa8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xf5d0
movl %eax, 0xa4(%rsp)
movl $0x0, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0x1a90(%rsp), %eax
jge 0x35fac
movq 0x1a88(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0xe0(%rsp), %rdi
callq 0x39680
testb $0x1, %al
jne 0x35dc3
movq 0x3626d(%rip), %rdi     # 0x6bfb8
leaq 0x24eaa(%rip), %rsi     # 0x5abfc
callq 0x64b0
movq %rax, %rdi
movl 0x1a90(%rsp), %esi
callq 0x6940
movq %rax, %rdi
movq 0x36220(%rip), %rsi     # 0x6bf90
callq 0x6520
jmp 0x35fac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
jmp 0x35db1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x6a78
leaq 0xbf(%rsp), %rdi
callq 0x6550
jmp 0x35fd7
cmpl $0x0, 0xa4(%rsp)
jne 0x35e46
movq 0x1a48(%rsp), %rax
movq %rax, 0x18(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x1a88(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x80(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x2cee0
leaq 0x80(%rsp), %rdi
callq 0x61d0
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %edx
movq %rax, %rcx
leaq 0x24de8(%rip), %rsi     # 0x5ac15
movb $0x0, %al
callq 0x6760
leaq 0x80(%rsp), %rdi
callq 0x6a78
jmp 0x35f38
cmpl $0x1, 0xa4(%rsp)
jne 0x35f36
cmpl $0x0, 0xa0(%rsp)
jne 0x35ecb
movq 0x1a48(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x1a88(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x60(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x2cee0
leaq 0x60(%rsp), %rdi
callq 0x61d0
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %edx
movq %rax, %rcx
leaq 0x24d65(%rip), %rsi     # 0x5ac1d
movb $0x0, %al
callq 0x6760
leaq 0x60(%rsp), %rdi
callq 0x6a78
jmp 0x35f25
movq 0x1a48(%rsp), %rax
movq %rax, (%rsp)
movq 0x1a88(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movl (%rax,%rcx,4), %edx
leaq 0x40(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x2cee0
leaq 0x40(%rsp), %rdi
callq 0x61d0
movq (%rsp), %rdi
movq %rax, %rdx
leaq 0x24d11(%rip), %rsi     # 0x5ac25
movb $0x0, %al
callq 0x6760
leaq 0x40(%rsp), %rdi
callq 0x6a78
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x35f38
movl 0xa8(%rsp), %eax
movl $0x7, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x35f62
movq 0x1a48(%rsp), %rdi
leaq 0x24cd0(%rip), %rsi     # 0x5ac2b
movb $0x0, %al
callq 0x6760
movq 0x1a88(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movl (%rax,%rcx,4), %esi
leaq 0xe0(%rsp), %rdi
callq 0x3a310
leaq 0xe0(%rsp), %rdi
callq 0xf5d0
movl %eax, 0xa4(%rsp)
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x35d0c
movq 0x1a48(%rsp), %rdi
leaq 0x24c6e(%rip), %rsi     # 0x5ac29
movb $0x0, %al
callq 0x6760
movq 0x1a48(%rsp), %rdi
callq 0x64c0
addq $0x1a78, %rsp           # imm = 0x1A78
retq
movq 0xb0(%rsp), %rdi
callq 0x6970
nopw %cs:(%rax,%rax)
nop