// File: /workspace/github_c_makefile_star2_O1/QwazyWabbitWOS[P]lmctf60/g_spawn.c
// Function: ReadEntFile
char* ReadEntFile(char* mapname, char* entities)
{
	FILE* fp;

	char	name[MAX_QPATH];
	size_t	size;
	char* tempbuf = NULL;
	int count, ch;
	cvar_t* vs;
	cvar_t* mop;

	if (!deathmatch->value)
		return entities;

	// expose these cvars so we can inspect them
	vs = gi.cvar("version", "", 0);
	mop = gi.cvar("map_override_path", "", 0);
	
	//R1Q2 allows us to modify entities.
	if (strstr(vs->string, "R1Q2") == NULL)
	{
		if(strcmp(mop->string, "") == 0)
			gi.dprintf("LMCTF Error: map_override_path not set for q2pro server.\n");
		return entities;  //Must use q2pro's internal entity management.
	}

	strcpy(name, gamedir->string);
	strcat(name, "/ent/");
	strcat(name, mapname);
	strcat(name, ".ent");

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

	// Prescan to get size we need.
	for (count = 0; (ch = fgetc(fp)) != EOF; count++);
	fseek(fp, 0, SEEK_SET);  //rewind

	if (count >= 0x40000)	//QW// This constant comes from MAX_MAP_ENTSTRING
	{
		gi.dprintf("Error: ent file %s rejected, entity string exceeds game limits.\n", name);
		return entities;
	}

	tempbuf = (char*)gi.TagMalloc(count + 2, TAG_LEVEL);

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

		if (size)
		{
			if ((strncmp(tempbuf, ":append", 7) == 0) && strlen(entities) + count < 0x40000)
			{
				gi.dprintf("Using .ent file for added ents.\n");
				strcat(entities, &tempbuf[8]);
			}
			else
			{
				gi.dprintf("Using .ent file for replaced ents.\n");
				strcpy(entities, tempbuf);
			}
		}
		else
		{
			gi.dprintf("Error: entity file empty.\n");
		}
		gi.TagFree(tempbuf);
	}
	else
	{
		gi.dprintf("Error: unable to allocate memory for entities.\n");
	}

	return entities;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq 0x2ebd4(%rip), %rax     # 0x69ec0
movq (%rax), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x3b302
jnp 0x3b4d7
movq %rdi, %r14
movq 0x2e2f4(%rip), %rbp     # 0x69600
leaq 0x1b647(%rip), %rdi     # 0x5695a
leaq 0x19beb(%rip), %r15     # 0x54f05
movq %r15, %rsi
xorl %edx, %edx
callq *0x120(%rbp)
movq %rax, %r12
leaq 0x1e037(%rip), %rdi     # 0x59366
movq %r15, %rsi
xorl %edx, %edx
callq *0x120(%rbp)
movq %rax, %r15
movq 0x8(%r12), %rdi
leaq 0x1e02f(%rip), %rsi     # 0x59378
callq 0x19990
testq %rax, %rax
je 0x3b400
movq 0x2eb52(%rip), %rax     # 0x69eb0
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rsp, %r15
movq %r15, %rdi
callq 0x182a0
movq %r15, %rdi
callq 0x18680
movl $0x746e652f, (%rsp,%rax) # imm = 0x746E652F
movw $0x2f, 0x4(%rsp,%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0x19640
movq %r15, %rdi
callq 0x18680
movl $0x746e652e, (%rsp,%rax) # imm = 0x746E652E
movb $0x0, 0x4(%rsp,%rax)
leaq 0x1918c(%rip), %rsi     # 0x54538
movq %r15, %rdi
callq 0x19470
testq %rax, %rax
je 0x3b4d7
movq %rax, %r12
movq $-0x1, %r15
movq %r12, %rdi
callq 0x18900
incq %r15
cmpl $-0x1, %eax
jne 0x3b3c7
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x19100
cmpl $0x40000, %r15d         # imm = 0x40000
jb 0x3b419
leaq 0x1dfc4(%rip), %rdi     # 0x593b7
movq %rsp, %rsi
xorl %eax, %eax
callq *0x8(%rbp)
jmp 0x3b4d7
movq 0x8(%r15), %rax
cmpb $0x0, (%rax)
jne 0x3b4d7
leaq 0x1df69(%rip), %rdi     # 0x5937d
jmp 0x3b4a2
leal 0x2(%r15), %edi
movl $0x2fe, %esi            # imm = 0x2FE
callq *0x108(%rbp)
testq %rax, %rax
je 0x3b49b
movq %rax, %r14
movl $0x1, %esi
movq %rax, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x183f0
movq %rax, %r13
movq %r12, %rdi
callq 0x185a0
testl %r13d, %r13d
je 0x3b4c2
leaq 0x1df9e(%rip), %rsi     # 0x593f8
movl $0x7, %edx
movq %r14, %rdi
callq 0x181e0
testl %eax, %eax
jne 0x3b4a9
movq %rbx, %rdi
callq 0x18680
addq %r15, %rax
cmpq $0x3ffff, %rax          # imm = 0x3FFFF
ja 0x3b4a9
leaq 0x1df7b(%rip), %rdi     # 0x59400
xorl %eax, %eax
callq *0x8(%rbp)
movq %r14, %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x19640
jmp 0x3b4ce
leaq 0x19449(%rip), %rdi     # 0x548eb
xorl %eax, %eax
callq *0x8(%rbp)
jmp 0x3b4d7
leaq 0x1df71(%rip), %rdi     # 0x59421
xorl %eax, %eax
callq *0x8(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x182a0
jmp 0x3b4ce
leaq 0x1df7c(%rip), %rdi     # 0x59445
xorl %eax, %eax
callq *0x8(%rbp)
movq %r14, %rdi
callq *0x110(%rbp)
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq