// File: /workspace/github_c_makefile_star2_O2/QwazyWabbitWOS[P]lmctf60/g_menu.c
// Function: SaveServer_Exec
void SaveServer_Exec(edict_t *ent)
{
	FILE	*fp;

	char	name[MAX_INFO_STRING];
	char	text[MAX_INFO_STRING];
	size_t	size;
	char*	tempbuf;
	size_t	i = 0;

	strcpy(name, gamedir->string);
	strcat(name,"/");
	strcat(name, server_file->string);

	fp = fopen (name, "r");
	if (!fp)
		return;

	tempbuf = (char *) gi.TagMalloc(40000, TAG_GAME); //server.cfg files must not exceed 40k arbitrary

	if (tempbuf)
	{
		size = fread(tempbuf, 1, 40000, fp);
		fclose(fp);

		if (size > 0 && size < 40000)
		{
			ctf_SafePrint(ent,PRINT_HIGH,"Read server.cfg successfully");
		}
		else
		{
			ctf_SafePrint(ent,PRINT_HIGH,"Failed to read server.cfg");
		}

		fp = fopen(name, "w");
		if (!fp)
			return;

		//dump out the server settings we record
		sprintf(text, "set dmflags %d\n", (int) dmflags->value);
		fwrite(text, 1, strlen(text), fp);
		sprintf(text, "set ctfflags %d\n", (int) ctfflags->value);
		fwrite(text, 1, strlen(text), fp);
		sprintf(text, "set timelimit %d\n", (int) timelimit->value);
		fwrite(text, 1, strlen(text), fp);
		sprintf(text, "set fraglimit %d\n", (int) fraglimit->value);
		fwrite(text, 1, strlen(text), fp);

		//now dump out remainder of file, ignoring the things we dumped out above
		while (i <= size)
		{
			if (
				strncmp(&tempbuf[i], "set dmflags", 11) == 0 || strncmp(&tempbuf[i], "dmflags", 7) == 0 ||
				strncmp(&tempbuf[i], "set ctfflags", 12) == 0 || strncmp(&tempbuf[i], "ctfflags", 8) == 0 ||
				strncmp(&tempbuf[i], "set timelimit", 13) == 0 || strncmp(&tempbuf[i], "timelimit", 9) == 0 ||
				strncmp(&tempbuf[i], "set fraglimit", 13) == 0 || strncmp(&tempbuf[i], "fraglimit", 9) == 0
				)
			{
				while(tempbuf[i] != '\n' && i < size)
					i++;
				i++; //get past that additional \n character
			}
			else
			{
				while(tempbuf[i] != '\n' && i < size)
				{
					fwrite(&tempbuf[i], 1, 1, fp);
					i++;
				}
				fwrite("\n",1,1,fp);
				i++;
			}
		}
		fclose(fp);
		ctf_SafePrint(ent,PRINT_HIGH,"Success: Current server values saved to server.cfg.\n");

	}
	else
	{
		ctf_SafePrint(ent,PRINT_HIGH,"Error: unable to allocate memory for server.cfg read.\n");
	}

	gi.TagFree(tempbuf);
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdi, %r13
movq 0x4baee(%rip), %rax     # 0x66eb0
movq (%rax), %rax
movq 0x8(%rax), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x182c0
movq %rbx, %rdi
callq 0x186c0
movw $0x2f, 0x10(%rsp,%rax)
movq 0x4b554(%rip), %rax     # 0x66940
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x19800
leaq 0x32e7c(%rip), %rsi     # 0x4e27e
movq %rbx, %rdi
callq 0x19620
testq %rax, %rax
je 0x1b6e4
movq %rax, %rbx
movq 0x4b1e3(%rip), %r15     # 0x66600
movl $0x9c40, %edi           # imm = 0x9C40
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x108(%r15)
movq %rax, %r14
testq %rax, %rax
je 0x1b6c8
pushq $0x1
popq %rsi
movl $0x9c40, %edx           # imm = 0x9C40
movq %r14, %rdi
movq %rbx, %rcx
callq 0x18420
movq %rax, %r15
movq %rbx, %rdi
callq 0x185e0
leaq -0x1(%r15), %rax
cmpq $0x9c3f, %rax           # imm = 0x9C3F
leaq 0x3302e(%rip), %rax     # 0x4e497
leaq 0x33044(%rip), %rdx     # 0x4e4b4
cmovbq %rax, %rdx
pushq $0x2
popq %rsi
movq %r13, %rdi
callq 0x18ec0
leaq 0x38869(%rip), %rsi     # 0x53cef
leaq 0x10(%rsp), %rdi
callq 0x19620
testq %rax, %rax
je 0x1b6e4
movq %rax, %r12
movq %r13, 0x8(%rsp)
movq 0x4b170(%rip), %rax     # 0x66618
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x33017(%rip), %rsi     # 0x4e4ce
xorl %r13d, %r13d
leaq 0x210(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x198c0
movq %rbx, %rdi
callq 0x186c0
pushq $0x1
popq %rbp
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x198e0
movq 0x4b669(%rip), %rax     # 0x66b58
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x32fe0(%rip), %rsi     # 0x4e4de
movq %rbx, %rdi
xorl %eax, %eax
callq 0x198c0
movq %rbx, %rdi
callq 0x186c0
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x198e0
movq 0x4b188(%rip), %rax     # 0x666b0
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x32fb8(%rip), %rsi     # 0x4e4ef
movq %rbx, %rdi
xorl %eax, %eax
callq 0x198c0
movq %rbx, %rdi
callq 0x186c0
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x198e0
movq 0x4b89f(%rip), %rax     # 0x66e00
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x32f91(%rip), %rsi     # 0x4e501
movq %rbx, %rdi
xorl %eax, %eax
callq 0x198c0
movq %rbx, %rdi
callq 0x186c0
movq %rbx, %rdi
movq %rbp, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0x198e0
pushq $0xb
popq %rbp
cmpq %r15, %r13
ja 0x1b6ab
leaq (%r14,%r13), %rbx
movq %rbx, %rdi
leaq 0x32f66(%rip), %rsi     # 0x4e513
movq %rbp, %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
movq %rbx, %rdi
leaq 0x32f50(%rip), %rsi     # 0x4e517
pushq $0x7
popq %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
movq %rbx, %rdi
leaq 0x32f3e(%rip), %rsi     # 0x4e51f
pushq $0xc
popq %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
movq %rbx, %rdi
leaq 0x32f28(%rip), %rsi     # 0x4e523
pushq $0x8
popq %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
movq %rbx, %rdi
leaq 0x32f1b(%rip), %rsi     # 0x4e52c
pushq $0xd
popq %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
movq %rbx, %rdi
leaq 0x32f09(%rip), %rsi     # 0x4e530
pushq $0x9
popq %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
movq %rbx, %rdi
leaq 0x32efd(%rip), %rsi     # 0x4e53a
pushq $0xd
popq %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
movq %rbx, %rdi
leaq 0x32eeb(%rip), %rsi     # 0x4e53e
pushq $0x9
popq %rdx
callq 0x18200
testl %eax, %eax
je 0x1b67c
cmpq %r15, %r13
jae 0x1b698
movb (%r14,%r13), %al
cmpb $0xa, %al
je 0x1b698
movsbl %al, %edi
movq %r12, %rsi
callq 0x18a30
incq %r13
jmp 0x1b65f
movq %r13, %rcx
movb (%r14,%r13), %al
incq %r13
cmpq %r15, %rcx
jae 0x1b596
cmpb $0xa, %al
jne 0x1b67c
jmp 0x1b596
pushq $0xa
popq %rdi
movq %r12, %rsi
callq 0x18a30
incq %r13
jmp 0x1b596
movq %r12, %rdi
callq 0x185e0
leaq 0x32e8e(%rip), %rdx     # 0x4e548
movq 0x8(%rsp), %r13
movq 0x4af3a(%rip), %r15     # 0x66600
jmp 0x1b6cf
leaq 0x32eae(%rip), %rdx     # 0x4e57d
pushq $0x2
popq %rsi
movq %r13, %rdi
callq 0x18ec0
movq %r14, %rdi
callq *0x110(%r15)
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq