// File: /workspace/github_cpp_makefile_star2_O1/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 $0x18, %rsp
xorl %ebp, %ebp
cmpl $0x0, 0x82a6e0(%rip)    # 0x8314c4
movl $0x0, %r12d
je 0x6df6
movq 0x82a6d5(%rip), %rax    # 0x8314c8
movl (%rax), %r12d
cmpl $0x0, 0x1b8a627(%rip)   # 0x1b91424
leaq 0x118a61c(%rip), %r13   # 0x1191420
jle 0x6e52
xorl %ebx, %ebx
leaq 0x1b8a62b(%rip), %r14   # 0x1b9143a
xorl %ebp, %ebp
movswq (%r14), %r15
leaq (%r15,%r15,4), %rax
movl 0x20(%r13,%rax,8), %edi
cmpl $-0x1, %edi
je 0x6e30
callq 0x6ccd
cmpl $-0x1, %eax
je 0x6e30
movl %eax, %r15d
movswl (%r14), %eax
cmpl %eax, %r15d
je 0x6e3f
movw %r15w, (%r14)
incl %ebp
incq %rbx
movslq 0x1b8a5db(%rip), %rax   # 0x1b91424
addq $0x14, %r14
cmpq %rax, %rbx
jl 0x6e11
movslq 0x118a5c3(%rip), %rdi   # 0x119141c
callq 0x4430
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movl 0x118a5a1(%rip), %r15d  # 0x119141c
testl %r15d, %r15d
jle 0x6e8d
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x41c0
movslq 0x1b8a590(%rip), %rax   # 0x1b91424
testq %rax, %rax
jle 0x6ed1
shlq $0x2, %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
leaq 0x1b8a586(%rip), %rdx   # 0x1b91430
movswl 0xa(%rcx,%rdx), %esi
testl %esi, %esi
sets %dil
cmpl %esi, %r15d
setle %r8b
orb %dil, %r8b
jne 0x6ec8
movslq %esi, %rsi
movb $0x1, (%rbx,%rsi)
addq $0x14, %rcx
cmpq %rcx, %rax
jne 0x6eaa
movl %ebp, 0x14(%rsp)
movl %r15d, %eax
sarl $0x1f, %eax
andl %r15d, %eax
movl %eax, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
addq %r15, %rbx
decq %rbx
leaq (%r15,%r15,4), %rax
leaq -0x8(,%rax,8), %rbp
addq %r13, %rbp
xorl %edx, %edx
testl %r15d, %r15d
jle 0x6f56
cmpb $0x0, (%rbx)
jne 0x6f3a
movl (%rbp), %edi
testl %edi, %edi
setns %al
cmpl %r12d, %edi
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6f3a
movq %r13, %r14
movl %edx, %r13d
callq 0x6ccd
movl %r13d, %edx
movq %r14, %r13
xorl %ecx, %ecx
cmpl $-0x1, %eax
setne %cl
addl %ecx, %edx
jmp 0x6f3c
xorl %ecx, %ecx
decl %r15d
decq %rbx
addq $-0x28, %rbp
testb %cl, %cl
jne 0x6efe
movl %edx, 0x4(%rsp)
incl %r15d
movl %r15d, %eax
jmp 0x6f5e
movl %edx, 0x4(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x118a4b8(%rip)   # 0x119141c
movq 0x8(%rsp), %rdi
callq 0x43a0
movslq %r12d, %rdi
callq 0x4430
movq %rax, %r15
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movl %r12d, %r14d
testl %r12d, %r12d
jle 0x6fa1
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x41c0
movslq 0x118a474(%rip), %rax   # 0x119141c
testq %rax, %rax
jle 0x6fe2
shlq $0x3, %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
movl 0x20(%rcx,%r13), %edx
testl %edx, %edx
setns %sil
cmpl %r12d, %edx
setl %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x6fd9
movl %edx, %edx
movb $0x1, (%r15,%rdx)
addq $0x28, %rcx
cmpq %rcx, %rax
jne 0x6fb7
testl %r12d, %r12d
jle 0x701e
leaq (%r14,%r15), %rbx
decq %rbx
xorl %r14d, %r14d
movl %r12d, %r13d
cmpb $0x0, (%rbx,%r14)
jne 0x7019
leal -0x1(%r13), %edi
callq 0x6ccd
cmpl $-0x1, %eax
je 0x7019
decq %r14
decl %r13d
jne 0x6ff4
xorl %r13d, %r13d
movl %r12d, %r14d
jmp 0x7024
negl %r14d
jmp 0x7024
xorl %r14d, %r14d
movl %r12d, %r13d
movq %r15, %rdi
callq 0x43a0
cmpl %r12d, %r13d
jge 0x70ad
movq 0x82a490(%rip), %rbx    # 0x8314c8
movslq (%rbx), %rax
movq %rax, 0x8(%rsp)
leaq (%rbx,%rax,4), %rsi
addq $0x4, %rsi
movslq %r13d, %rbp
leaq (%rbx,%rbp,4), %r15
addq $0x4, %r15
movl (%r15), %eax
addl %ebx, %eax
subl %esi, %eax
movslq %eax, %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0x4530
addl %r15d, %r12d
subl 0x82a456(%rip), %r12d   # 0x8314c8
movl %r12d, 0x82a44b(%rip)   # 0x8314c4
movl %r13d, (%rbx)
testl %r13d, %r13d
jle 0x70ad
addq $0x4, %rbx
movq 0x8(%rsp), %rax
shlq $0x2, %rax
shlq $0x2, %rbp
subl %eax, %ebp
movl %r13d, %eax
xorl %ecx, %ecx
movl (%rbx,%rcx,4), %edx
testl %edx, %edx
js 0x70a5
addl %ebp, %edx
movl %edx, (%rbx,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x7099
movl 0x14(%rsp), %esi
testl %esi, %esi
movl 0x4(%rsp), %edx
jg 0x70d1
testl %edx, %edx
jg 0x70d1
testl %r14d, %r14d
jne 0x70d1
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1541f(%rip), %rdi     # 0x1c4f7
movl %r14d, %ecx
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9fb2