// File: 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:
subq $0x88, %rsp
movl $0x0, 0x84(%rsp)
movl $0x0, 0x80(%rsp)
movl $0x0, 0x7c(%rsp)
cmpl $0x0, 0x837dc8(%rip)    # 0x83f514
je 0x775d
movq 0x837dc3(%rip), %rax    # 0x83f518
movl (%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x7765
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x7765
movl 0xc(%rsp), %eax
movl %eax, 0x74(%rsp)
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x1b97cf5(%rip), %eax   # 0x1b9f474
jge 0x77e8
movslq 0x78(%rsp), %rcx
leaq 0x1b97cf3(%rip), %rax   # 0x1b9f480
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
callq 0x6620
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
movq 0x68(%rsp), %rcx
movswl 0xa(%rcx), %ecx
cmpl %ecx, %eax
je 0x77d9
movl 0x64(%rsp), %eax
movw %ax, %cx
movq 0x68(%rsp), %rax
movw %cx, 0xa(%rax)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x77db
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x7775
movslq 0x1197c7d(%rip), %rdi   # 0x119f46c
shlq $0x0, %rdi
callq 0x4500
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xc400
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x1197c44(%rip), %eax   # 0x119f46c
jge 0x7845
movq 0x58(%rsp), %rax
movslq 0x78(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x781e
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x1b97c1d(%rip), %eax   # 0x1b9f474
jge 0x78b2
movslq 0x78(%rsp), %rcx
leaq 0x1b97c1b(%rip), %rax   # 0x1b9f480
imulq $0x14, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movswl 0xa(%rax), %eax
cmpl $0x0, %eax
jl 0x7890
movq 0x50(%rsp), %rax
movswl 0xa(%rax), %eax
cmpl 0x1197bde(%rip), %eax   # 0x119f46c
jl 0x7892
jmp 0x78a5
movq 0x58(%rsp), %rax
movq 0x50(%rsp), %rcx
movswq 0xa(%rcx), %rcx
movb $0x1, (%rax,%rcx)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x784d
movl 0x1197bb4(%rip), %eax   # 0x119f46c
subl $0x1, %eax
movl %eax, 0x78(%rsp)
cmpl $-0x1, 0x78(%rsp)
jle 0x793d
movslq 0x78(%rsp), %rcx
leaq 0x1197b9e(%rip), %rax   # 0x119f470
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
movq 0x58(%rsp), %rax
movslq 0x78(%rsp), %rcx
testb $0x1, (%rax,%rcx)
je 0x78f0
jmp 0x793d
movq 0x48(%rsp), %rax
cmpl $0x0, 0x20(%rax)
jl 0x7909
movq 0x48(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x74(%rsp), %eax
jl 0x790b
jmp 0x793d
movq 0x48(%rsp), %rax
movl 0x20(%rax), %edi
callq 0x7580
cmpl $-0x1, %eax
jne 0x791f
jmp 0x793d
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
movl 0x78(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x78bf
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1197b22(%rip)   # 0x119f46c
movq 0x58(%rsp), %rdi
callq 0x4470
movslq 0x74(%rsp), %rdi
shlq $0x0, %rdi
callq 0x4500
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xc400
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x79ad
movq 0x38(%rsp), %rax
movslq 0x78(%rsp), %rcx
movb $0x0, (%rax,%rcx)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x7988
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x1197aad(%rip), %eax   # 0x119f46c
jge 0x7a13
movslq 0x78(%rsp), %rcx
leaq 0x1197aa3(%rip), %rax   # 0x119f470
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x20(%rax)
jl 0x79f2
movq 0x30(%rsp), %rax
movl 0x20(%rax), %eax
cmpl 0x74(%rsp), %eax
jl 0x79f4
jmp 0x7a06
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rcx
movslq 0x20(%rcx), %rcx
movb $0x1, (%rax,%rcx)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x79b5
movl 0x74(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x78(%rsp)
cmpl $-0x1, 0x78(%rsp)
jle 0x7a5d
movq 0x38(%rsp), %rax
movslq 0x78(%rsp), %rcx
testb $0x1, (%rax,%rcx)
jne 0x7a43
movl 0x78(%rsp), %edi
callq 0x7580
cmpl $-0x1, %eax
jne 0x7a45
jmp 0x7a5d
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
movl 0x78(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x7a1e
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
movq 0x38(%rsp), %rdi
callq 0x4470
movl 0x44(%rsp), %eax
cmpl 0x74(%rsp), %eax
jge 0x7b84
movq 0x837a91(%rip), %rax    # 0x83f518
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
addq $0x4, %rax
movq 0x28(%rsp), %rcx
movslq (%rcx), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x20(%rsp)
movq 0x837a68(%rip), %rax    # 0x83f518
movq 0x28(%rsp), %rcx
movslq 0x44(%rsp), %rdx
movslq 0x4(%rcx,%rdx,4), %rcx
addq %rcx, %rax
movq 0x20(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0x1c(%rsp)
movq 0x28(%rsp), %rax
addq $0x4, %rax
movslq 0x44(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
movslq 0x1c(%rsp), %rdx
callq 0x4600
movq 0x10(%rsp), %rax
movslq 0x1c(%rsp), %rcx
addq %rcx, %rax
movq 0x837a08(%rip), %rcx    # 0x83f518
subq %rcx, %rax
movl %eax, 0x8379fb(%rip)    # 0x83f514
movl 0x44(%rsp), %ecx
movq 0x28(%rsp), %rax
movl %ecx, (%rax)
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
cmpl 0x44(%rsp), %eax
jge 0x7b79
movq 0x28(%rsp), %rax
movslq 0x78(%rsp), %rcx
cmpl $0x0, 0x4(%rax,%rcx,4)
jge 0x7b49
jmp 0x7b6c
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rax
subq %rax, %rsi
movq 0x28(%rsp), %rax
movslq 0x78(%rsp), %rcx
movslq 0x4(%rax,%rcx,4), %rdx
addq %rsi, %rdx
movl %edx, 0x4(%rax,%rcx,4)
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x7b2c
movq 0x28(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x74(%rsp)
cmpl $0x0, 0x84(%rsp)
jg 0x7b9f
cmpl $0x0, 0x80(%rsp)
jg 0x7b9f
cmpl $0x0, 0x7c(%rsp)
jle 0x7bbf
movl 0x84(%rsp), %esi
movl 0x80(%rsp), %edx
movl 0x7c(%rsp), %ecx
leaq 0x22887(%rip), %rdi     # 0x2a43f
movb $0x0, %al
callq 0xb5d0
addq $0x88, %rsp
retq
nopw (%rax,%rax)