// File: /workspace/github_c_makefile_star2_O3/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 0x2e4d8(%rip), %rax     # 0x68ec0
movq (%rax), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x3a9fe
jnp 0x3abd3
movq %rdi, %r14
movq 0x2dbf8(%rip), %rbp     # 0x68600
leaq 0x1af6b(%rip), %rdi     # 0x5597a
leaq 0x1952f(%rip), %r15     # 0x53f45
movq %r15, %rsi
xorl %edx, %edx
callq *0x120(%rbp)
movq %rax, %r12
leaq 0x1db4f(%rip), %rdi     # 0x5857a
movq %r15, %rsi
xorl %edx, %edx
callq *0x120(%rbp)
movq %rax, %r15
movq 0x8(%r12), %rdi
leaq 0x1db47(%rip), %rsi     # 0x5858c
callq 0x19980
testq %rax, %rax
je 0x3aafc
movq 0x2e456(%rip), %rax     # 0x68eb0
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 0x18ac0(%rip), %rsi     # 0x53568
movq %r15, %rdi
callq 0x19470
testq %rax, %rax
je 0x3abd3
movq %rax, %r12
movq $-0x1, %r15
movq %r12, %rdi
callq 0x18900
incq %r15
cmpl $-0x1, %eax
jne 0x3aac3
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x19100
cmpl $0x40000, %r15d         # imm = 0x40000
jb 0x3ab15
leaq 0x1dadc(%rip), %rdi     # 0x585cb
movq %rsp, %rsi
xorl %eax, %eax
callq *0x8(%rbp)
jmp 0x3abd3
movq 0x8(%r15), %rax
cmpb $0x0, (%rax)
jne 0x3abd3
leaq 0x1da81(%rip), %rdi     # 0x58591
jmp 0x3ab9e
leal 0x2(%r15), %edi
movl $0x2fe, %esi            # imm = 0x2FE
callq *0x108(%rbp)
testq %rax, %rax
je 0x3ab97
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 0x3abbe
leaq 0x1dab6(%rip), %rsi     # 0x5860c
movl $0x7, %edx
movq %r14, %rdi
callq 0x181e0
testl %eax, %eax
jne 0x3aba5
movq %rbx, %rdi
callq 0x18680
addq %r15, %rax
cmpq $0x3ffff, %rax          # imm = 0x3FFFF
ja 0x3aba5
leaq 0x1da93(%rip), %rdi     # 0x58614
xorl %eax, %eax
callq *0x8(%rbp)
movq %r14, %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x19640
jmp 0x3abca
leaq 0x18d7d(%rip), %rdi     # 0x5391b
xorl %eax, %eax
callq *0x8(%rbp)
jmp 0x3abd3
leaq 0x1da89(%rip), %rdi     # 0x58635
xorl %eax, %eax
callq *0x8(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x182a0
jmp 0x3abca
leaq 0x1da94(%rip), %rdi     # 0x58659
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