// File: /workspace/github_c_makefile_star2_O3/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 0x1d261(%rip), %rsi     # 0x572bd
callq 0x19470
movq %rax, %rbx
testq %rax, %rax
jne 0x3a07f
movq 0x2e590(%rip), %rcx     # 0x68600
leaq 0x19e86(%rip), %rdi     # 0x53efd
movq %r14, %rsi
xorl %eax, %eax
callq *0x38(%rcx)
movq 0x2e57a(%rip), %r15     # 0x68600
movl $0x2fe, %edi            # imm = 0x2FE
callq *0x118(%r15)
movq 0x2e4bf(%rip), %rax     # 0x68558
movq (%rax), %rdi
movq 0x2ec9d(%rip), %rax     # 0x68d40
movslq 0x614(%rax), %rax
imulq $0x4a8, %rax, %rdx      # imm = 0x4A8
xorl %esi, %esi
callq 0x18880
movq 0x2e471(%rip), %rax     # 0x68530
movq (%rax), %rax
movss 0x20(%rax), %xmm0
addss 0x18e59(%rip), %xmm0    # 0x52f28
cvttss2si %xmm0, %eax
movq 0x2ea2e(%rip), %rcx     # 0x68b08
movl %eax, 0x8c(%rcx)
leaq 0xc(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x183f0
cmpl $0x4a8, (%r14)          # imm = 0x4A8
jne 0x3a1da
leaq 0x10(%rsp), %r14
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x183f0
movq (%r14), %rsi
subq 0x2e55d(%rip), %rsi     # 0x68688
leaq 0x1dfd9(%rip), %rdi     # 0x5810b
xorl %eax, %eax
callq *0x8(%r15)
movq %rbx, %rdi
callq 0x19500
leaq 0x8(%rsp), %r14
movl $0x4, %esi
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rcx
callq 0x183f0
cmpq $0x1, %rax
je 0x3a175
movq %rbx, %rdi
callq 0x185a0
leaq 0x1dfb1(%rip), %rdi     # 0x58120
xorl %eax, %eax
callq *0x38(%r15)
movslq 0x8(%rsp), %rax
cmpq $-0x1, %rax
movq 0x2e3d3(%rip), %r12     # 0x68558
je 0x3a1ff
movq 0x2e97a(%rip), %rcx     # 0x68b08
cmpl 0x8c(%rcx), %eax
jl 0x3a1a6
leal 0x1(%rax), %ecx
movq 0x2e968(%rip), %rdx     # 0x68b08
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 0x3a145
movq %rbx, %rdi
callq 0x185a0
movq 0x38(%r15), %rcx
leaq 0x1defd(%rip), %rdi     # 0x580ea
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 0x2e322(%rip), %rdi     # 0x68530
movq (%rdi), %rax
movss 0x20(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jbe 0x3a26b
movq 0x2eb1b(%rip), %rax     # 0x68d40
movq 0x408(%rax), %rax
movl $0x500, %ecx            # imm = 0x500
movl $0x1, %edx
movq (%r12), %rsi
movq %rax, (%rsi,%rcx)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movl $0x0, 0x304(%rax)
movq (%rdi), %rsi
movss 0x20(%rsi), %xmm1
addq $0x9140, %rax           # imm = 0x9140
addq $0x4a8, %rcx            # imm = 0x4A8
incl %edx
ucomiss %xmm0, %xmm1
ja 0x3a236
movq 0x2e896(%rip), %rax     # 0x68b08
movslq 0x8c(%rax), %rax
testq %rax, %rax
jle 0x3a2ec
movq (%r12), %r14
movq 0x2eb17(%rip), %rcx     # 0x68da0
movss 0x4(%rcx), %xmm0
movss %xmm0, 0x4(%rsp)
imulq $0x4a8, %rax, %r15      # imm = 0x4A8
xorl %r12d, %r12d
leaq 0x1de9c(%rip), %rbx     # 0x58141
cmpl $0x0, 0x60(%r14,%r12)
je 0x3a2e0
movq 0x138(%r14,%r12), %rdi
testq %rdi, %rdi
je 0x3a2e0
movq %rbx, %rsi
callq 0x18b20
testl %eax, %eax
jne 0x3a2e0
movss 0x2fc(%r14,%r12), %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x200(%r14,%r12)
addq $0x4a8, %r12            # imm = 0x4A8
cmpq %r12, %r15
jne 0x3a2a5
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop