// File: FreeSlave[P]vhlt/hlcsg/qcsg.cpp
// Function: ReuseModel()
void     ReuseModel ()
{
	int i;
	for (i = g_numentities - 1; i >= 1; i--) // so it won't affect the remaining entities in the loop when we move this entity backward
	{
		const char *name = ValueForKey (&g_entities[i], "zhlt_usemodel");
		if (!*name)
		{
			continue;
		}
		int j;
		for (j = 1; j < g_numentities; j++)
		{
			if (*ValueForKey (&g_entities[j], "zhlt_usemodel"))
			{
				continue;
			}
			if (!strcmp (name, ValueForKey (&g_entities[j], "targetname")))
			{
				break;
			}
		}
		if (j == g_numentities)
		{
			if (!strcasecmp (name, "null"))
			{
				SetKeyValue (&g_entities[i], "model", "");
				continue;
			}
			Error ("zhlt_usemodel: can not find target entity '%s', or that entity is also using 'zhlt_usemodel'.\n", name);
		}
		SetKeyValue (&g_entities[i], "model", ValueForKey (&g_entities[j], "model"));
		if (j > i)
		{
			// move this entity backward
			// to prevent precache error in case of .mdl/.spr and wrong result of EntityForModel in case of map model
			entity_t tmp;
			tmp = g_entities[i];
			memmove (&g_entities[i], &g_entities[i + 1], ((j + 1) - (i + 1)) * sizeof (entity_t));
			g_entities[j] = tmp;
		}
	}
}

// Assembly:
subq $0x58, %rsp
leaq 0x200d049(%rip), %rax   # 0x202d4a4
movl (%rax), %eax
subl $0x1, %eax
movl %eax, 0x54(%rsp)
cmpl $0x1, 0x54(%rsp)
jl 0x20685
movslq 0x54(%rsp), %rax
leaq 0x200d035(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0xdbcf(%rip), %rsi      # 0x2e058
callq 0x7ff0
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
cmpb $0x0, (%rax)
jne 0x204a2
jmp 0x20675
movl $0x1, 0x44(%rsp)
movl 0x44(%rsp), %eax
leaq 0x200cfef(%rip), %rcx   # 0x202d4a4
cmpl (%rcx), %eax
jge 0x2052e
movslq 0x44(%rsp), %rax
leaq 0x200cfeb(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0xdb85(%rip), %rsi      # 0x2e058
callq 0x7ff0
cmpb $0x0, (%rax)
je 0x204df
jmp 0x2051e
movq 0x48(%rsp), %rax
movq %rax, 0x10(%rsp)
movslq 0x44(%rsp), %rax
leaq 0x200cfbb(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0xa1a1(%rip), %rsi      # 0x2a6a4
callq 0x7ff0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x4530
cmpl $0x0, %eax
jne 0x2051c
jmp 0x2052e
jmp 0x2051e
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x204aa
movl 0x44(%rsp), %eax
leaq 0x200cf6b(%rip), %rcx   # 0x202d4a4
cmpl (%rcx), %eax
jne 0x20591
movq 0x48(%rsp), %rdi
leaq 0xd7b2(%rip), %rsi      # 0x2dcfb
callq 0x4410
cmpl $0x0, %eax
jne 0x2057e
movslq 0x54(%rsp), %rax
leaq 0x200cf51(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0xdaf3(%rip), %rsi      # 0x2e060
leaq 0xb7c5(%rip), %rdx      # 0x2bd39
callq 0x8070
jmp 0x20675
movq 0x48(%rsp), %rsi
leaq 0xe43b(%rip), %rdi      # 0x2e9c5
movb $0x0, %al
callq 0xba80
movslq 0x54(%rsp), %rcx
leaq 0x200cf13(%rip), %rax   # 0x202d4b0
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movslq 0x44(%rsp), %rax
leaq 0x200cefb(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0xda9d(%rip), %rsi      # 0x2e060
callq 0x7ff0
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0xda89(%rip), %rsi      # 0x2e060
callq 0x8070
movl 0x44(%rsp), %eax
cmpl 0x54(%rsp), %eax
jle 0x20673
movslq 0x54(%rsp), %rax
leaq 0x200ceba(%rip), %rsi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rsi
leaq 0x18(%rsp), %rdi
movl $0x28, %edx
callq 0x4310
movslq 0x54(%rsp), %rax
leaq 0x200ce98(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
movl 0x54(%rsp), %eax
addl $0x1, %eax
cltq
leaq 0x200ce81(%rip), %rsi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rsi
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl 0x54(%rsp), %ecx
addl $0x1, %ecx
subl %ecx, %eax
cltq
imulq $0x28, %rax, %rdx
callq 0x4600
movslq 0x44(%rsp), %rax
leaq 0x200ce53(%rip), %rdi   # 0x202d4b0
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq 0x18(%rsp), %rsi
movl $0x28, %edx
callq 0x4310
jmp 0x20675
movl 0x54(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x20464
addq $0x58, %rsp
retq
nopw (%rax,%rax)