// File: /workspace/github_c_makefile_star2_O3/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 0x4cdc3(%rip), %rax     # 0x68eb0
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 0x4c829(%rip), %rax     # 0x68940
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbx, %rdi
callq 0x19640
leaq 0x36151(%rip), %rsi     # 0x5227e
movq %rbx, %rdi
callq 0x19470
testq %rax, %rax
je 0x1c441
movq %rax, %rbx
movq 0x4c4b8(%rip), %r15     # 0x68600
movl $0x9c40, %edi           # imm = 0x9C40
movl $0x2fd, %esi            # imm = 0x2FD
callq *0x108(%r15)
movq %rax, %r14
testq %rax, %rax
je 0x1c423
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 0x36301(%rip), %rax     # 0x52497
leaq 0x36317(%rip), %rdx     # 0x524b4
cmovbq %rax, %rdx
movl $0x2, %esi
movq %r13, %rdi
callq 0x18da0
leaq 0x3bb5a(%rip), %rsi     # 0x57d0f
leaq 0x10(%rsp), %rdi
callq 0x19470
testq %rax, %rax
je 0x1c441
movq %rax, %r12
movq %r13, 0x8(%rsp)
movq 0x4c441(%rip), %rax     # 0x68618
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x362e8(%rip), %rsi     # 0x524ce
xorl %ebp, %ebp
leaq 0x210(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x196f0
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19710
movq 0x4c93c(%rip), %rax     # 0x68b58
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x362b3(%rip), %rsi     # 0x524de
movq %rbx, %rdi
xorl %eax, %eax
callq 0x196f0
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19710
movq 0x4c459(%rip), %rax     # 0x686b0
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x36289(%rip), %rsi     # 0x524ef
movq %rbx, %rdi
xorl %eax, %eax
callq 0x196f0
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19710
movq 0x4cb6e(%rip), %rax     # 0x68e00
movq (%rax), %rax
cvttss2si 0x20(%rax), %edx
leaq 0x36260(%rip), %rsi     # 0x52501
movq %rbx, %rdi
xorl %eax, %eax
callq 0x196f0
movq %rbx, %rdi
callq 0x18680
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rdx
movq %r12, %rcx
callq 0x19710
leaq 0x36246(%rip), %r13     # 0x52513
leaq (%r14,%rbp), %rbx
movl $0xb, %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x3621f(%rip), %rsi     # 0x52517
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movl $0xc, %edx
movq %rbx, %rdi
leaq 0x3620b(%rip), %rsi     # 0x5251f
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movl $0x8, %edx
movq %rbx, %rdi
leaq 0x361f3(%rip), %rsi     # 0x52523
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movl $0xd, %edx
movq %rbx, %rdi
leaq 0x361e0(%rip), %rsi     # 0x5252c
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movl $0x9, %edx
movq %rbx, %rdi
leaq 0x361c8(%rip), %rsi     # 0x52530
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movl $0xd, %edx
movq %rbx, %rdi
leaq 0x361ba(%rip), %rsi     # 0x5253a
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movl $0x9, %edx
movq %rbx, %rdi
leaq 0x361a6(%rip), %rsi     # 0x5253e
callq 0x181e0
testl %eax, %eax
je 0x1c3e7
movb (%rbx), %al
cmpb $0xa, %al
sete %cl
cmpq %r15, %rbp
setae %dl
orb %cl, %dl
movq %rbp, %rbx
jne 0x1c3d5
movsbl %al, %edi
movq %r12, %rsi
callq 0x189a0
leaq 0x1(%rbp), %rbx
cmpq %r15, %rbx
jae 0x1c3d5
movb 0x1(%r14,%rbp), %al
movq %rbx, %rbp
cmpb $0xa, %al
jne 0x1c3b5
movl $0xa, %edi
movq %r12, %rsi
callq 0x189a0
incq %rbx
jmp 0x1c3fa
leaq 0x1(%rbp), %rbx
cmpq %r15, %rbp
jae 0x1c3fa
cmpb $0xa, (%r14,%rbp)
movq %rbx, %rbp
jne 0x1c3e7
movq %rbx, %rbp
cmpq %r15, %rbx
jbe 0x1c2cd
movq %r12, %rdi
callq 0x185a0
leaq 0x36133(%rip), %rdx     # 0x52548
movq 0x8(%rsp), %r13
movq 0x4c1df(%rip), %r15     # 0x68600
jmp 0x1c42a
leaq 0x36153(%rip), %rdx     # 0x5257d
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