// File: /workspace/github_c_makefile_star2_O1/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 0x4ddd2(%rip), %rax     # 0x69eb0
movq (%rax), %rax
movq 0x8(%rax), %rsi
leaq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x182a0
movq %rbx, %rdi
callq 0x18680
movw $0x2f, 0x10(%rsp,%rax)
movq 0x4d838(%rip), %rax     # 0x69940
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x19640
leaq 0x37160(%rip), %rsi     # 0x5327e
movq %rbx, %rdi
callq 0x19470
testq %rax, %rax
je 0x1c437
movq %rax, %rbx
movq 0x4d4c7(%rip), %r15     # 0x69600
movl $0x9c40, %edi           # imm = 0x9C40
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x108(%r15)
movq %rax, %r14
testq %rax, %rax
je 0x1c419
movl $0x1, %esi
movl $0x9c40, %edx           # imm = 0x9C40
movq %r14, %rdi
movq %rbx, %rcx
callq 0x183f0
movq %rax, %r15
movq %rbx, %rdi
callq 0x185a0
leaq -0x1(%r15), %rax
cmpq $0x9c3f, %rax           # imm = 0x9C3F
leaq 0x37310(%rip), %rax     # 0x53497
leaq 0x37326(%rip), %rdx     # 0x534b4
cmovbq %rax, %rdx
movl $0x2, %esi
movq %r13, %rdi
callq 0x18da0
leaq 0x3c955(%rip), %rsi     # 0x58afb
leaq 0x10(%rsp), %rdi
callq 0x19470
testq %rax, %rax
je 0x1c437
movq %rax, %r12
movq %r13, 0x8(%rsp)
movq 0x4d450(%rip), %rax     # 0x69618
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x372f7(%rip), %rsi     # 0x534ce
xorl %ebp, %ebp
leaq 0x210(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x19700
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19720
movq 0x4d94b(%rip), %rax     # 0x69b58
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x372c2(%rip), %rsi     # 0x534de
movq %rbx, %rdi
xorl %eax, %eax
callq 0x19700
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19720
movq 0x4d468(%rip), %rax     # 0x696b0
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x37298(%rip), %rsi     # 0x534ef
movq %rbx, %rdi
xorl %eax, %eax
callq 0x19700
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19720
movq 0x4db7d(%rip), %rax     # 0x69e00
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x3726f(%rip), %rsi     # 0x53501
movq %rbx, %rdi
xorl %eax, %eax
callq 0x19700
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19720
leaq 0x37255(%rip), %r13     # 0x53513
leaq (%r14,%rbp), %rbx
movl $0xb, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x3722e(%rip), %rsi     # 0x53517
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movl $0xc, %edx
movq %rbx, %rdi
leaq 0x3721a(%rip), %rsi     # 0x5351f
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movl $0x8, %edx
movq %rbx, %rdi
leaq 0x37202(%rip), %rsi     # 0x53523
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movl $0xd, %edx
movq %rbx, %rdi
leaq 0x371ef(%rip), %rsi     # 0x5352c
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movl $0x9, %edx
movq %rbx, %rdi
leaq 0x371d7(%rip), %rsi     # 0x53530
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movl $0xd, %edx
movq %rbx, %rdi
leaq 0x371c9(%rip), %rsi     # 0x5353a
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movl $0x9, %edx
movq %rbx, %rdi
leaq 0x371b5(%rip), %rsi     # 0x5353e
callq 0x181e0
testl %eax, %eax
je 0x1c3d7
movb (%r14,%rbp), %al
cmpb $0xa, %al
sete %cl
cmpq %r15, %rbp
setae %dl
orb %cl, %dl
movq %rbp, %rbx
jne 0x1c3c8
movsbl %al, %edi
movq %r12, %rsi
callq 0x189a0
leaq 0x1(%rbp), %rbx
cmpq %r15, %rbx
jae 0x1c3c8
movb 0x1(%r14,%rbp), %al
movq %rbx, %rbp
cmpb $0xa, %al
jne 0x1c3a8
movl $0xa, %edi
movq %r12, %rsi
callq 0x189a0
jmp 0x1c3ed
leaq 0x1(%rbp), %rbx
cmpq %r15, %rbp
jae 0x1c3ea
cmpb $0xa, (%r14,%rbp)
movq %rbx, %rbp
jne 0x1c3d7
decq %rbx
incq %rbx
movq %rbx, %rbp
cmpq %r15, %rbx
jbe 0x1c2be
movq %r12, %rdi
callq 0x185a0
leaq 0x3713d(%rip), %rdx     # 0x53548
movq 0x8(%rsp), %r13
movq 0x4d1e9(%rip), %r15     # 0x69600
jmp 0x1c420
leaq 0x3715d(%rip), %rdx     # 0x5357d
movl $0x2, %esi
movq %r13, %rdi
callq 0x18da0
movq %r14, %rdi
callq *0x110(%r15)
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq