// File: /workspace/github_cpp_makefile_star2_O3/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, 0x82a744(%rip)    # 0x8314b4
movl $0x0, %r14d
je 0x6d82
movq 0x82a739(%rip), %rax    # 0x8314b8
movl (%rax), %r14d
cmpl $0x0, 0x1b8a68b(%rip)   # 0x1b91414
jle 0x6ddd
xorl %ebx, %ebx
leaq 0x1b8a696(%rip), %r15   # 0x1b9142a
xorl %ebp, %ebp
movswq (%r15), %r12
leaq (%r12,%r12,4), %rax
leaq 0x118a66b(%rip), %rcx   # 0x1191410
movl 0x20(%rcx,%rax,8), %edi
cmpl $-0x1, %edi
je 0x6dca
callq 0x6c59
cmpl $-0x1, %eax
je 0x6dbb
movl %eax, %r12d
movswl (%r15), %eax
cmpl %eax, %r12d
je 0x6dca
movw %r12w, (%r15)
incl %ebp
incq %rbx
movslq 0x1b8a640(%rip), %rax   # 0x1b91414
addq $0x14, %r15
cmpq %rax, %rbx
jl 0x6d96
movl %ebp, 0x4(%rsp)
movslq 0x118a624(%rip), %rdi   # 0x119140c
callq 0x4420
movq %rax, %rbx
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
movl 0x118a602(%rip), %r13d  # 0x119140c
testl %r13d, %r13d
jle 0x6e1c
movq %rbx, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x41c0
movl %r14d, %r15d
movslq 0x1b8a5ee(%rip), %rax   # 0x1b91414
testq %rax, %rax
jle 0x6e63
shlq $0x2, %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
leaq 0x1b8a5e4(%rip), %rdx   # 0x1b91420
movswl 0xa(%rcx,%rdx), %esi
testl %esi, %esi
sets %dil
cmpl %esi, %r13d
setle %r8b
orb %dil, %r8b
jne 0x6e5a
movslq %esi, %rsi
movb $0x1, (%rbx,%rsi)
addq $0x14, %rcx
cmpq %rcx, %rax
jne 0x6e3c
movl %r13d, %eax
sarl $0x1f, %eax
andl %r13d, %eax
movl %r13d, %ebp
movl %eax, 0x8(%rsp)
subl %eax, %ebp
movq %rbx, 0x10(%rsp)
addq %r13, %rbx
decq %rbx
leaq (,%r13,4), %rax
addq %r13, %rax
leaq 0x118a57e(%rip), %rcx   # 0x1191410
leaq (%rcx,%rax,8), %r12
addq $-0x8, %r12
xorl %ecx, %ecx
cmpl %ecx, %ebp
je 0x6eda
movl %ecx, %r14d
cmpb $0x0, (%rbx)
jne 0x6ed2
movl (%r12), %edi
testl %edi, %edi
js 0x6ed2
cmpl %r15d, %edi
jge 0x6ed2
callq 0x6c59
leal 0x1(%r14), %ecx
decq %rbx
addq $-0x28, %r12
cmpl $-0x1, %eax
jne 0x6e9c
subl %ecx, %r13d
incl %r13d
jmp 0x6ed5
subl %r14d, %r13d
movl %r13d, %eax
jmp 0x6ee1
movl %ebp, %r14d
movl 0x8(%rsp), %eax
movl %eax, 0x118a525(%rip)   # 0x119140c
movq 0x10(%rsp), %rdi
callq 0x4390
movslq %r15d, %r12
movq %r12, %rdi
callq 0x4420
movq %rax, %r13
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
testl %r12d, %r12d
jle 0x6f24
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x41c0
movslq 0x118a4e1(%rip), %rax   # 0x119140c
testq %rax, %rax
jle 0x6f5f
shlq $0x3, %rax
leaq (%rax,%rax,4), %rax
xorl %ecx, %ecx
leaq 0x118a4cf(%rip), %rdx   # 0x1191410
movslq 0x20(%rcx,%rdx), %rdx
testq %rdx, %rdx
js 0x6f56
cmpl %r15d, %edx
jge 0x6f56
movb $0x1, (%r13,%rdx)
addq $0x28, %rcx
cmpq %rcx, %rax
jne 0x6f3a
testl %r15d, %r15d
jle 0x7045
movq %r13, %rbx
decq %rbx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r12d, %r12d
cmpb $0x0, (%rbx,%r15)
jne 0x6fa0
leal (%r15,%rbp), %edi
callq 0x6c59
cmpl $-0x1, %eax
je 0x6fa0
incq %r12
decq %rbx
decq %rbp
cmpl %r12d, %r15d
jne 0x6f76
xorl %ebx, %ebx
movl %r15d, %r12d
jmp 0x6fa6
movl %r15d, %ebx
subl %r12d, %ebx
movq %r13, %rdi
callq 0x4390
cmpl %r15d, %ebx
jge 0x7050
movq 0x82a4fa(%rip), %rbp    # 0x8314b8
movslq (%rbp), %rax
movq %rax, 0x8(%rsp)
leaq 0x4(,%rax,4), %rsi
addq %rbp, %rsi
movslq %ebx, %r15
leaq 0x4(,%r15,4), %rdi
addq %rbp, %rdi
movq %rdi, 0x10(%rsp)
movl (%rdi), %eax
addl %ebp, %eax
subl %esi, %eax
movslq %eax, %r13
movq %r13, %rdx
callq 0x4520
addl 0x10(%rsp), %r13d
subl 0x82a4b6(%rip), %r13d   # 0x8314b8
movl %r13d, 0x82a4ab(%rip)   # 0x8314b4
movl %ebx, (%rbp)
testl %ebx, %ebx
jle 0x7050
addq $0x4, %rbp
movq 0x8(%rsp), %rax
shlq $0x2, %rax
shlq $0x2, %r15
subl %eax, %r15d
movl %ebx, %eax
xorl %ecx, %ecx
movl 0x4(%rsp), %esi
movl (%rbp,%rcx,4), %edx
testl %edx, %edx
js 0x703b
addl %r15d, %edx
movl %edx, (%rbp,%rcx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x702c
jmp 0x7054
movq %r13, %rdi
callq 0x4390
xorl %r12d, %r12d
movl 0x4(%rsp), %esi
testl %esi, %esi
jg 0x7071
testl %r14d, %r14d
jne 0x7071
testl %r12d, %r12d
jne 0x7071
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x15477(%rip), %rdi     # 0x1c4ef
movl %r14d, %edx
movl %r12d, %ecx
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9e76