// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/common/bspfile.cpp
// Function: DeleteEmbeddedLightmaps()
void DeleteEmbeddedLightmaps ()
{
	int countrestoredfaces = 0;
	int countremovedtexinfos = 0;
	int countremovedtextures = 0;
	int i;
	int numtextures = g_texdatasize? ((dmiptexlump_t *)g_dtexdata)->nummiptex: 0;

	// Step 1: parse the original texinfo index stored in each "?_rad*" texture
	//         and restore the texinfo for the faces that have had their lightmap embedded

	for (i = 0; i < g_numfaces; i++)
	{
		dface_t *f = &g_dfaces[i];
		int texinfo;

		texinfo = ParseTexinfoForFace (f);
		if (texinfo != f->texinfo)
		{
			f->texinfo = texinfo;
			countrestoredfaces++;
		}
	}

	// Step 2: remove redundant texinfo
	{
		bool *texinfoused = (bool *)malloc (g_numtexinfo * sizeof (bool));
		hlassume (texinfoused != NULL, assume_NoMemory);

		for (i = 0; i < g_numtexinfo; i++)
		{
			texinfoused[i] = false;
		}
		for (i = 0; i < g_numfaces; i++)
		{
			dface_t *f = &g_dfaces[i];

			if (f->texinfo < 0 || f->texinfo >= g_numtexinfo)
			{
				continue;
			}
			texinfoused[f->texinfo] = true;
		}
		for (i = g_numtexinfo - 1; i > -1; i--)
		{
			texinfo_t *info = &g_texinfo[i];

			if (texinfoused[i])
			{
				break; // still used by a face; should not remove this texinfo
			}
			if (info->miptex < 0 || info->miptex >= numtextures)
			{
				break; // invalid; should not remove this texinfo
			}
			if (ParseImplicitTexinfoFromTexture (info->miptex) == -1)
			{
				break; // not added by hlrad; should not remove this texinfo
			}
			countremovedtexinfos++;
		}
		g_numtexinfo = i + 1; // shrink g_texinfo
		free (texinfoused);
	}

	// Step 3: remove redundant textures
	{
		int numremaining; // number of remaining textures
		bool *textureused = (bool *)malloc (numtextures * sizeof (bool));
		hlassume (textureused != NULL, assume_NoMemory);

		for (i = 0; i < numtextures; i++)
		{
			textureused[i] = false;
		}
		for (i = 0; i < g_numtexinfo; i++)
		{
			texinfo_t *info = &g_texinfo[i];

			if (info->miptex < 0 || info->miptex >= numtextures)
			{
				continue;
			}
			textureused[info->miptex] = true;
		}
		for (i = numtextures - 1; i > -1; i--)
		{
			if (textureused[i] || ParseImplicitTexinfoFromTexture (i) == -1)
			{
				break; // should not remove this texture
			}
			countremovedtextures++;
		}
		numremaining = i + 1;
		free (textureused);

		if (numremaining < numtextures)
		{
			dmiptexlump_t *texdata = (dmiptexlump_t *)g_dtexdata;
			byte *dataaddr = (byte *)&texdata->dataofs[texdata->nummiptex];
			int datasize = (g_dtexdata + texdata->dataofs[numremaining]) - dataaddr;
			byte *newdataaddr = (byte *)&texdata->dataofs[numremaining];
			memmove (newdataaddr, dataaddr, datasize);
			g_texdatasize = (newdataaddr + datasize) - g_dtexdata;
			texdata->nummiptex = numremaining;
			for (i = 0; i < numremaining; i++)
			{
				if (texdata->dataofs[i] < 0) // bad texture
				{
					continue;
				}
				texdata->dataofs[i] += newdataaddr - dataaddr;
			}

			numtextures = texdata->nummiptex;
		}
	}

	if (countrestoredfaces > 0 || countremovedtexinfos > 0 || countremovedtextures > 0)
	{
		Log ("DeleteEmbeddedLightmaps: restored %d faces, removed %d texinfos and %d textures.\n",
				countrestoredfaces, countremovedtexinfos, countremovedtextures);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %r15d, %r15d
cmpl $0x0, 0x8290fd(%rip)    # 0x82f4c4
movl $0x0, %r13d
je 0x63d9
movq 0x8290f2(%rip), %rax    # 0x82f4c8
movl (%rax), %r13d
leaq 0x1b89050(%rip), %r14   # 0x1b8f430
xorl %ebx, %ebx
movslq 0x1b8903b(%rip), %rax   # 0x1b8f424
cmpq %rax, %rbx
jge 0x6410
movq %r14, %rdi
callq 0x5893
movswl 0xa(%r14), %ecx
cmpl %ecx, %eax
je 0x6407
movw %ax, 0xa(%r14)
incl %r15d
incq %rbx
addq $0x14, %r14
jmp 0x63e2
movslq 0x1189005(%rip), %rdi   # 0x118f41c
callq 0x4450
movq %rax, %r14
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x9517
movl 0x1188fe6(%rip), %ecx   # 0x118f41c
xorl %eax, %eax
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
cmpq %rax, %rdx
je 0x6451
movb $0x0, (%r14,%rax)
incq %rax
jmp 0x6442
movl 0x1b88fcd(%rip), %edx   # 0x1b8f424
xorl %eax, %eax
testl %edx, %edx
cmovlel %eax, %edx
imulq $0x14, %rdx, %rdx
leaq 0x1b88fc7(%rip), %rsi   # 0x1b8f430
cmpq %rax, %rdx
je 0x6492
movswl 0xa(%rax,%rsi), %edi
testl %edi, %edi
sets %r8b
cmpl %edi, %ecx
setle %r9b
orb %r8b, %r9b
jne 0x648c
movslq %edi, %rdi
movb $0x1, (%r14,%rdi)
addq $0x14, %rax
jmp 0x6469
movq %r15, 0x20(%rsp)
movl %ecx, %eax
sarl $0x1f, %eax
andl %ecx, %eax
movl %eax, 0x4(%rsp)
imulq $0x28, %rcx, %rax
leaq 0x1188f73(%rip), %r12   # 0x118f420
leaq (%rax,%r12), %rbx
addq $-0x8, %rbx
xorl %edx, %edx
movq %rcx, %r15
movl %edx, %ebp
testl %r15d, %r15d
jle 0x64eb
cmpb $0x0, -0x1(%r14,%r15)
jne 0x64f2
movl (%rbx), %edi
testl %edi, %edi
js 0x64f2
cmpl %r13d, %edi
jge 0x64f2
callq 0x62b7
addq $-0x28, %rbx
leal -0x1(%rbp), %edx
leaq -0x1(%r15), %rcx
cmpl $-0x1, %eax
jne 0x64b7
jmp 0x64f2
movl 0x4(%rsp), %eax
movl %eax, %r15d
movl %r15d, 0x1188f23(%rip)  # 0x118f41c
movq %r14, %rdi
callq 0x43b0
movslq %r13d, %rdi
callq 0x4450
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x9517
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
movl %ebp, %edx
negl %edx
movl %edx, 0x4(%rsp)
cmpq %rax, %rcx
je 0x6542
movb $0x0, (%r15,%rax)
incq %rax
jmp 0x6533
movl 0x1188ed4(%rip), %ecx   # 0x118f41c
xorl %eax, %eax
testl %ecx, %ecx
cmovlel %eax, %ecx
imulq $0x28, %rcx, %rcx
cmpq %rax, %rcx
je 0x6580
movl 0x20(%rax,%r12), %edx
testl %edx, %edx
setns %sil
cmpl %r13d, %edx
setl %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x657a
movl %edx, %edx
movb $0x1, (%r15,%rdx)
addq $0x28, %rax
jmp 0x6553
movl %r13d, %ebx
movl %r13d, %eax
sarl $0x1f, %eax
andl %r13d, %eax
movl %eax, 0x8(%rsp)
xorl %r14d, %r14d
testl %ebx, %ebx
jle 0x65ba
leaq -0x1(%rbx), %r12
movl %r12d, %eax
cmpb $0x0, (%r15,%rax)
jne 0x65c0
movl %r12d, %edi
callq 0x62b7
cmpl $-0x1, %eax
je 0x65c0
decl %r14d
movq %r12, %rbx
jmp 0x6593
movl 0x8(%rsp), %eax
movl %eax, %ebx
movq %r15, %rdi
callq 0x43b0
cmpl %r13d, %ebx
jge 0x665b
movq 0x828ef0(%rip), %rcx    # 0x82f4c8
movq %rcx, 0x18(%rsp)
leaq 0x4(%rcx), %r13
movslq (%rcx), %rax
leaq (%rcx,%rax,4), %rsi
addq $0x4, %rsi
shlq $0x2, %rax
movq %rax, 0x8(%rsp)
movslq %ebx, %r15
leaq (%rcx,%r15,4), %rdi
addq $0x4, %rdi
movq %rdi, 0x10(%rsp)
shlq $0x2, %r15
movl (%rdi), %eax
addl %ecx, %eax
subl %esi, %eax
movslq %eax, %r12
movq %r12, %rdx
callq 0x4550
addl 0x10(%rsp), %r12d
subl 0x828ea2(%rip), %r12d   # 0x82f4c8
movl %r12d, 0x828e97(%rip)   # 0x82f4c4
movq 0x18(%rsp), %rax
movl %ebx, (%rax)
subl 0x8(%rsp), %r15d
xorl %eax, %eax
testl %ebx, %ebx
cmovlel %eax, %ebx
cmpq %rax, %rbx
je 0x665b
movl (%r13,%rax,4), %ecx
testl %ecx, %ecx
js 0x6656
addl %r15d, %ecx
movl %ecx, (%r13,%rax,4)
incq %rax
jmp 0x6640
movq 0x20(%rsp), %rsi
testl %esi, %esi
setg %al
orl %r14d, %ebp
setne %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x6698
negl %r14d
leaq 0x12ddb(%rip), %rdi     # 0x19457
movl 0x4(%rsp), %edx
movl %r14d, %ecx
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x89dc
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq