// File: /workspace/github_c_makefile_star2_O2/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 0x2f6a4(%rip), %rax     # 0x66ec0
movq (%rax), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x37832
jnp 0x37a00
movq %rdi, %r14
movq 0x2edc4(%rip), %rbp     # 0x66600
leaq 0x1a117(%rip), %rdi     # 0x5195a
leaq 0x186fb(%rip), %r15     # 0x4ff45
movq %r15, %rsi
xorl %edx, %edx
callq *0x120(%rbp)
movq %rax, %r12
leaq 0x1ccfb(%rip), %rdi     # 0x5455a
movq %r15, %rsi
xorl %edx, %edx
callq *0x120(%rbp)
movq %rax, %r15
movq 0x8(%r12), %rdi
leaq 0x1ccf3(%rip), %rsi     # 0x5456c
callq 0x19b60
testq %rax, %rax
je 0x3792d
movq 0x2f622(%rip), %rax     # 0x66eb0
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rsp, %r15
movq %r15, %rdi
callq 0x182c0
movq %r15, %rdi
callq 0x186c0
movl $0x746e652f, (%rsp,%rax) # imm = 0x746E652F
movw $0x2f, 0x4(%rsp,%rax)
movq %r15, %rdi
movq %r14, %rsi
callq 0x19800
movq %r15, %rdi
callq 0x186c0
movl $0x746e652e, (%rsp,%rax) # imm = 0x746E652E
movb $0x0, 0x4(%rsp,%rax)
leaq 0x17c8c(%rip), %rsi     # 0x4f568
movq %r15, %rdi
callq 0x19620
testq %rax, %rax
je 0x37a00
movq %rax, %r12
pushq $-0x1
popq %r15
movq %r12, %rdi
callq 0x18960
incq %r15
cmpl $-0x1, %eax
jne 0x378f4
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x19270
cmpl $0x40000, %r15d         # imm = 0x40000
jb 0x37946
leaq 0x1cc8b(%rip), %rdi     # 0x545ab
movq %rsp, %rsi
xorl %eax, %eax
callq *0x8(%rbp)
jmp 0x37a00
movq 0x8(%r15), %rax
cmpb $0x0, (%rax)
jne 0x37a00
leaq 0x1cc30(%rip), %rdi     # 0x54571
jmp 0x379cb
leal 0x2(%r15), %edi
movl $0x2fe, %esi            # imm = 0x2FE
callq *0x108(%rbp)
testq %rax, %rax
je 0x379c4
movq %rax, %r14
pushq $0x1
popq %rsi
movq %rax, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x18420
movq %rax, %r13
movq %r12, %rdi
callq 0x185e0
testl %r13d, %r13d
je 0x379eb
leaq 0x1cc67(%rip), %rsi     # 0x545ec
pushq $0x7
popq %rdx
movq %r14, %rdi
callq 0x18200
testl %eax, %eax
jne 0x379d2
movq %rbx, %rdi
callq 0x186c0
addq %r15, %rax
cmpq $0x3ffff, %rax          # imm = 0x3FFFF
ja 0x379d2
leaq 0x1cc46(%rip), %rdi     # 0x545f4
xorl %eax, %eax
callq *0x8(%rbp)
movq %r14, %rsi
addq $0x8, %rsi
movq %rbx, %rdi
callq 0x19800
jmp 0x379f7
leaq 0x17f50(%rip), %rdi     # 0x4f91b
xorl %eax, %eax
callq *0x8(%rbp)
jmp 0x37a00
leaq 0x1cc3c(%rip), %rdi     # 0x54615
xorl %eax, %eax
callq *0x8(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x182c0
jmp 0x379f7
leaq 0x1cc47(%rip), %rdi     # 0x54639
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