// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x2a015(%rip), %rsi     # 0x7414c
callq 0x196a0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x4a161
movq 0x384b2(%rip), %rax     # 0x82600
movq 0x38(%rax), %rcx
movq -0x8(%rbp), %rsi
leaq 0x26d64(%rip), %rdi     # 0x70ec1
movb $0x0, %al
callq *%rcx
movl $0x2fe, %edi            # imm = 0x2FE
movq 0x38493(%rip), %rax     # 0x82600
callq *0x118(%rax)
movq 0x383de(%rip), %rax     # 0x82558
movq (%rax), %rdi
movq 0x38bbc(%rip), %rax     # 0x82d40
movslq 0x614(%rax), %rax
imulq $0x4a8, %rax, %rdx      # imm = 0x4A8
xorl %esi, %esi
callq 0x188f0
movq 0x38390(%rip), %rax     # 0x82530
movq (%rax), %rax
movss 0x25d41(%rip), %xmm0    # 0x6feec
addss 0x20(%rax), %xmm0
cvttss2si %xmm0, %ecx
movq 0x3894d(%rip), %rax     # 0x82b08
movl %ecx, 0x8c(%rax)
movq -0x18(%rbp), %rcx
leaq -0x1c(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x18430
movq %rax, -0x38(%rbp)
movslq -0x1c(%rbp), %rax
cmpq $0x4a8, %rax            # imm = 0x4A8
je 0x4a209
movq -0x18(%rbp), %rdi
callq 0x18600
leaq 0x2abda(%rip), %rdi     # 0x74dd2
movq 0x38401(%rip), %rcx     # 0x82600
movb $0x0, %al
callq *0x38(%rcx)
jmp 0x4a442
movq -0x18(%rbp), %rcx
leaq -0x28(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x18430
movq %rax, -0x38(%rbp)
movq 0x383d5(%rip), %rax     # 0x82600
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %rsi
movq 0x3844e(%rip), %rax     # 0x82688
subq %rax, %rsi
leaq 0x2abaf(%rip), %rdi     # 0x74df3
movb $0x0, %al
callq *%rcx
movq -0x18(%rbp), %rdi
callq 0x19730
movq -0x18(%rbp), %rcx
leaq -0xc(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x18430
movq %rax, -0x38(%rbp)
cmpq $0x1, -0x38(%rbp)
je 0x4a28f
movq -0x18(%rbp), %rdi
callq 0x18600
leaq 0x2ab85(%rip), %rdi     # 0x74e08
movq 0x38376(%rip), %rcx     # 0x82600
movb $0x0, %al
callq *0x38(%rcx)
cmpl $-0x1, -0xc(%rbp)
jne 0x4a297
jmp 0x4a312
movl -0xc(%rbp), %eax
movq 0x38867(%rip), %rcx     # 0x82b08
cmpl 0x8c(%rcx), %eax
jl 0x4a2bc
movl -0xc(%rbp), %ecx
addl $0x1, %ecx
movq 0x38852(%rip), %rax     # 0x82b08
movl %ecx, 0x8c(%rax)
movq 0x38295(%rip), %rax     # 0x82558
movq (%rax), %rax
movslq -0xc(%rbp), %rcx
imulq $0x4a8, %rcx, %rcx      # imm = 0x4A8
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x186a0
movq -0x30(%rbp), %rdi
addq $0x68, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x188f0
movq 0x38300(%rip), %rax     # 0x82600
movq 0x90(%rax), %rax
movq -0x30(%rbp), %rdi
callq *%rax
jmp 0x4a251
movq -0x18(%rbp), %rdi
callq 0x18600
movl $0x0, -0x1c(%rbp)
cvtsi2ssl -0x1c(%rbp), %xmm1
movq 0x38202(%rip), %rax     # 0x82530
movq (%rax), %rax
movss 0x20(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x4a39d
movq 0x38216(%rip), %rax     # 0x82558
movq (%rax), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x4a8, %rcx, %rcx      # imm = 0x4A8
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq 0x389dd(%rip), %rax     # 0x82d40
movq 0x408(%rax), %rcx
movslq -0x1c(%rbp), %rax
imulq $0x9140, %rax, %rax     # imm = 0x9140
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movl $0x0, 0x304(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4a322
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq 0x3875a(%rip), %rcx     # 0x82b08
cmpl 0x8c(%rcx), %eax
jge 0x4a442
movq 0x38197(%rip), %rax     # 0x82558
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x4a8, %rcx, %rcx      # imm = 0x4A8
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x60(%rax)
jne 0x4a3e2
jmp 0x4a434
movq -0x30(%rbp), %rax
cmpq $0x0, 0x138(%rax)
je 0x4a432
movq -0x30(%rbp), %rax
movq 0x138(%rax), %rdi
leaq 0x2aa27(%rip), %rsi     # 0x74e29
callq 0x18bf0
cmpl $0x0, %eax
jne 0x4a430
movq 0x3898d(%rip), %rax     # 0x82da0
movss 0x4(%rax), %xmm0
movq -0x30(%rbp), %rax
addss 0x2fc(%rax), %xmm0
movq -0x30(%rbp), %rax
movss %xmm0, 0x200(%rax)
jmp 0x4a432
jmp 0x4a434
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4a3a4
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)