// File: /workspace/github_c_makefile_star2_O2/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 0x1c3d5(%rip), %rsi     # 0x5329d
callq 0x19620
movq %rax, %rbx
testq %rax, %rax
jne 0x36eeb
movq 0x2f724(%rip), %rcx     # 0x66600
leaq 0x1901a(%rip), %rdi     # 0x4fefd
movq %r14, %rsi
xorl %eax, %eax
callq *0x38(%rcx)
movq 0x2f70e(%rip), %r13     # 0x66600
movl $0x2fe, %edi            # imm = 0x2FE
callq *0x118(%r13)
movq 0x2f653(%rip), %rax     # 0x66558
movq (%rax), %rdi
movq 0x2fe31(%rip), %rax     # 0x66d40
movslq 0x614(%rax), %rax
imulq $0x4a8, %rax, %rdx      # imm = 0x4A8
xorl %esi, %esi
callq 0x188e0
movq 0x2f605(%rip), %rax     # 0x66530
movq (%rax), %rax
movss 0x20(%rax), %xmm0
addss 0x17fed(%rip), %xmm0    # 0x4ef28
cvttss2si %xmm0, %eax
movq 0x2fbc2(%rip), %rcx     # 0x66b08
movl %eax, 0x8c(%rcx)
leaq 0xc(%rsp), %r15
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x18420
cmpl $0x4a8, (%r15)          # imm = 0x4A8
jne 0x37123
leaq 0x10(%rsp), %r15
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x18420
movq (%r15), %rsi
subq 0x2f6f9(%rip), %rsi     # 0x66688
leaq 0x1d155(%rip), %rdi     # 0x540eb
xorl %eax, %eax
callq *0x8(%r13)
movq %rbx, %rdi
callq 0x196b0
pushq $0x4
popq %r12
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x18420
cmpq $0x1, %rax
je 0x36fd6
movq %rbx, %rdi
callq 0x185e0
leaq 0x1d130(%rip), %rdi     # 0x54100
xorl %eax, %eax
callq *0x38(%r13)
movslq 0x8(%rsp), %rax
cmpq $-0x1, %rax
movq 0x2f572(%rip), %r14     # 0x66558
je 0x3703e
movq %r13, %r15
movq 0x2fb16(%rip), %rcx     # 0x66b08
cmpl 0x8c(%rcx), %eax
jl 0x3700a
leal 0x1(%rax), %ecx
movq 0x2fb04(%rip), %rdx     # 0x66b08
movl %ecx, 0x8c(%rdx)
movq (%r14), %r14
imulq $0x4a8, %rax, %r13      # imm = 0x4A8
leaq (%r14,%r13), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x18680
xorps %xmm0, %xmm0
movups %xmm0, 0x68(%r14,%r13)
movq %rbp, %rdi
movq %r15, %r13
callq *0x90(%r15)
jmp 0x36fa8
movq %rbx, %rdi
callq 0x185e0
movq 0x2fcf3(%rip), %rax     # 0x66d40
movq 0x408(%rax), %rax
movl $0x500, %ecx            # imm = 0x500
xorl %edx, %edx
movq 0x2f4ce(%rip), %rdi     # 0x66530
cvtsi2ss %edx, %xmm0
movq (%rdi), %rsi
movss 0x20(%rsi), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x37092
movq (%r14), %rsi
movq %rax, (%rsi,%rcx)
andl $0x0, 0x304(%rax)
addq $0x9140, %rax           # imm = 0x9140
addq $0x4a8, %rcx            # imm = 0x4A8
incl %edx
jmp 0x37062
movq 0x2fa6f(%rip), %rax     # 0x66b08
movl 0x8c(%rax), %eax
movq (%r14), %r14
movq 0x2fcf7(%rip), %rcx     # 0x66da0
movss 0x4(%rcx), %xmm0
movss %xmm0, 0x4(%rsp)
xorl %r15d, %r15d
testl %eax, %eax
cmovlel %r15d, %eax
imulq $0x4a8, %rax, %r12      # imm = 0x4A8
leaq 0x1d056(%rip), %rbx     # 0x54121
cmpq %r15, %r12
je 0x37114
cmpl $0x0, 0x60(%r14,%r15)
je 0x3710b
movq 0x138(%r14,%r15), %rdi
testq %rdi, %rdi
je 0x3710b
movq %rbx, %rsi
callq 0x18bc0
testl %eax, %eax
jne 0x3710b
movss 0x2fc(%r14,%r15), %xmm0
addss 0x4(%rsp), %xmm0
movss %xmm0, 0x200(%r14,%r15)
addq $0x4a8, %r15            # imm = 0x4A8
jmp 0x370cb
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x185e0
movq 0x38(%r13), %rcx
leaq 0x1cf94(%rip), %rdi     # 0x540ca
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rcx