// File: /workspace/github_c_makefile_star2_O1/QwazyWabbitWOS[P]lmctf60/g_save.c
// Function: ReadLevel
void ReadLevel(char* filename)
{
	int		entnum;
	FILE* f;
	int		i;
	void* base;
	edict_t* ent;
	size_t	count;

	f = fopen(filename, "rb");
	if (!f)
		gi.error("Couldn't open %s", filename);

	// free any dynamic memory allocated by loading the level
	// base state
	gi.FreeTags(TAG_LEVEL);

	// wipe all the entities
	memset(g_edicts, 0, game.maxentities * sizeof(g_edicts[0]));
	globals.num_edicts = maxclients->value + 1;

	// check edict size
	count = fread(&i, sizeof(i), 1, f);
	if (i != sizeof(edict_t))
	{
		fclose(f);
		gi.error("ReadLevel: mismatched edict size");
		return; //never reached
	}

	// check function pointer base address
	count = fread(&base, sizeof(base), 1, f);
#ifdef _WIN32
	if (base != (void*)InitGame)
	{
		fclose(f);
		gi.error("ReadLevel: function pointers have moved");
		return;
	}
#else
	gi.dprintf("Function offsets %d\n", ((byte*)base) - ((byte*)InitGame));
#endif

	// load the level locals
	ReadLevelLocals(f);

	// load all the entities
	while (1)
	{
		count = fread(&entnum, sizeof(entnum), 1, f);
		if (count != 1)
		{
			fclose(f);
			gi.error("ReadLevel: failed to read entnum");
		}
		if (entnum == -1)
			break;
		if (entnum >= globals.num_edicts)
			globals.num_edicts = entnum + 1;

		ent = &g_edicts[entnum];
		ReadEdict(f, ent);

		// let the server rebuild world links for this ent
		memset(&ent->area, 0, sizeof(ent->area));
		gi.linkentity(ent);
	}

	fclose(f);

	// mark all clients as unconnected
	for (i = 0; i < maxclients->value; i++)
	{
		ent = &g_edicts[i + 1];
		ent->client = game.clients + i;
		ent->client->pers.connected = false;
	}

	// do any load time things at this point
	for (i = 0; i < globals.num_edicts; i++)
	{
		ent = &g_edicts[i];

		if (!ent->inuse)
			continue;

		// fire any cross-level triggers
		if (ent->classname)
			if (strcmp(ent->classname, "target_crosslevel_target") == 0)
				ent->nextthink = level.time + ent->delay;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
leaq 0x1d858(%rip), %rsi     # 0x581c4
callq 0x19470
movq %rax, %rbx
testq %rax, %rax
jne 0x3a98f
movq 0x2ec80(%rip), %rcx     # 0x69600
leaq 0x1a536(%rip), %rdi     # 0x54ebd
movq %r14, %rsi
xorl %eax, %eax
callq *0x38(%rcx)
movq 0x2ec6a(%rip), %r15     # 0x69600
movl $0x2fe, %edi            # imm = 0x2FE
callq *0x118(%r15)
movq 0x2ebaf(%rip), %rax     # 0x69558
movq (%rax), %rdi
movq 0x2f38d(%rip), %rax     # 0x69d40
movslq 0x614(%rax), %rax
imulq $0x4a8, %rax, %rdx      # imm = 0x4A8
xorl %esi, %esi
callq 0x18880
movq 0x2eb61(%rip), %rax     # 0x69530
movq (%rax), %rax
movss 0x20(%rax), %xmm0
addss 0x19549(%rip), %xmm0    # 0x53f28
cvttss2si %xmm0, %eax
movq 0x2f11e(%rip), %rcx     # 0x69b08
movl %eax, 0x8c(%rcx)
leaq 0x8(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x183f0
cmpl $0x4a8, (%r14)          # imm = 0x4A8
jne 0x3aaea
leaq 0x10(%rsp), %r14
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x183f0
movq (%r14), %rsi
subq 0x2ec4d(%rip), %rsi     # 0x69688
leaq 0x1e4b5(%rip), %rdi     # 0x58ef7
xorl %eax, %eax
callq *0x8(%r15)
movq %rbx, %rdi
callq 0x19500
leaq 0xc(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x183f0
cmpq $0x1, %rax
je 0x3aa85
movq %rbx, %rdi
callq 0x185a0
leaq 0x1e48d(%rip), %rdi     # 0x58f0c
xorl %eax, %eax
callq *0x38(%r15)
movslq 0xc(%rsp), %rax
cmpq $-0x1, %rax
movq 0x2eac3(%rip), %r12     # 0x69558
je 0x3ab0f
movq 0x2f06a(%rip), %rcx     # 0x69b08
cmpl 0x8c(%rcx), %eax
jl 0x3aab6
leal 0x1(%rax), %ecx
movq 0x2f058(%rip), %rdx     # 0x69b08
movl %ecx, 0x8c(%rdx)
movq (%r12), %r13
imulq $0x4a8, %rax, %rbp      # imm = 0x4A8
movq %r13, %r12
addq %rbp, %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x18640
xorps %xmm0, %xmm0
movups %xmm0, 0x68(%r13,%rbp)
movq %r12, %rdi
callq *0x90(%r15)
jmp 0x3aa55
movq %rbx, %rdi
callq 0x185a0
movq 0x38(%r15), %rcx
leaq 0x1e3d9(%rip), %rdi     # 0x58ed6
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rcx
movq %rbx, %rdi
callq 0x185a0
movq 0x2ea12(%rip), %rdi     # 0x69530
movq (%rdi), %rax
movss 0x20(%rax), %xmm0
xorl %eax, %eax
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x3ab7b
movq 0x2f209(%rip), %rax     # 0x69d40
movq 0x408(%rax), %rcx
movl $0x500, %edx            # imm = 0x500
xorl %eax, %eax
movq (%r12), %rsi
incl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movq %rcx, (%rsi,%rdx)
movl $0x0, 0x304(%rcx)
movq (%rdi), %rsi
movss 0x20(%rsi), %xmm1
addq $0x9140, %rcx           # imm = 0x9140
addq $0x4a8, %rdx            # imm = 0x4A8
ucomiss %xmm0, %xmm1
ja 0x3ab45
movl %eax, 0x8(%rsp)
movq 0x2ef82(%rip), %rax     # 0x69b08
movslq 0x8c(%rax), %r14
testq %r14, %r14
jle 0x3abfb
movq (%r12), %r15
movq 0x2f203(%rip), %rax     # 0x69da0
movss 0x4(%rax), %xmm0
movss %xmm0, 0x4(%rsp)
addq $0x60, %r15
xorl %r12d, %r12d
leaq 0x1e377(%rip), %rbx     # 0x58f2d
cmpl $0x0, (%r15)
je 0x3abec
movq 0xd8(%r15), %rdi
testq %rdi, %rdi
je 0x3abec
movq %rbx, %rsi
callq 0x18b20
testl %eax, %eax
jne 0x3abec
movss 0x29c(%r15), %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x1a0(%r15)
incq %r12
addq $0x4a8, %r15            # imm = 0x4A8
cmpq %r12, %r14
jne 0x3abb6
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop