// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlcsg/textures.cpp
// Function: WriteMiptex()
void            WriteMiptex()
{
    int             len, texsize, totaltexsize = 0;
    byte*           data;
    dmiptexlump_t*  l;
    double          start, end;

    g_texdatasize = 0;

    start = I_FloatTime();
    {
        if (!TEX_InitFromWad())
            return;

        AddAnimatingTextures();
    }
    end = I_FloatTime();
    Verbose("TEX_InitFromWad & AddAnimatingTextures elapsed time = %ldms\n", (long)(end - start));

    start = I_FloatTime();
    {
        int             i;

        for (i = 0; i < nummiptex; i++)
        {
            lumpinfo_t*     found;

            found = FindTexture(miptex + i);
            if (found)
            {
                miptex[i] = *found;
#ifdef HLCSG_AUTOWAD_NEW
				texwadpathes[found->iTexFile]->usedtextures++;
#endif
            }
            else
            {
                miptex[i].iTexFile = miptex[i].filepos = miptex[i].disksize = 0;
            }
        }
    }
    end = I_FloatTime();
    Verbose("FindTextures elapsed time = %ldms\n", (long)(end - start));

#ifdef HLCSG_AUTOWAD_NEW
	// Now we have filled lumpinfo for each miptex and the number of used textures for each wad.
	{
		char szTmpWad[MAX_VAL];
		int i;

		szTmpWad[0] = 0;
		for (i = 0; i < nTexFiles; i++)
		{
			wadpath_t *currentwad = texwadpathes[i];
			if (!currentwad->usedbymap && (currentwad->usedtextures > 0 || !g_bWadAutoDetect))
			{
				Log ("Including Wadfile: %s\n", currentwad->path);
				double percused = (double)currentwad->usedtextures / (double)nummiptex * 100;
				Log (" - Contains %i used texture%s, %2.2f percent of map (%d textures in wad)\n",
					 currentwad->usedtextures, currentwad->usedtextures == 1? "": "s", percused, currentwad->totaltextures);
			}
		}
		for (i = 0; i < nTexFiles; i++)
		{
			wadpath_t *currentwad = texwadpathes[i];
			if (currentwad->usedbymap && (currentwad->usedtextures > 0 || !g_bWadAutoDetect))
			{
				Log ("Using Wadfile: %s\n", currentwad->path);
				double percused = (double)currentwad->usedtextures / (double)nummiptex * 100;
				Log (" - Contains %i used texture%s, %2.2f percent of map (%d textures in wad)\n",
					 currentwad->usedtextures, currentwad->usedtextures == 1? "": "s", percused, currentwad->totaltextures);
	#ifdef HLCSG_STRIPWADPATH
				char tmp[_MAX_PATH];
				ExtractFile (currentwad->path, tmp);
				safe_strncat (szTmpWad, tmp, MAX_VAL);
	#else
				safe_strncat (szTmpWad, currentwad->path, MAX_VAL);
	#endif
				safe_strncat (szTmpWad, ";", MAX_VAL);
			}
		}
		
	#ifdef HLCSG_CHART_FIX
		Log("\n");
		if (*szTmpWad)
		{
			Log ("Wad files required to run the map: \"%s\"\n", szTmpWad);
		}
		else
		{
			Log ("Wad files required to run the map: (None)\n");
		}
	#endif
		SetKeyValue(&g_entities[0], "wad", szTmpWad);
	}

#endif
    start = I_FloatTime();
    {
        int             i;
        texinfo_t*      tx = g_texinfo;

        // Sort them FIRST by wadfile and THEN by name for most efficient loading in the engine.
        qsort((void*)miptex, (size_t) nummiptex, sizeof(miptex[0]), lump_sorter_by_wad_and_name);

        // Sleazy Hack 104 Pt 2 - After sorting the miptex array, reset the texinfos to point to the right miptexs
        for (i = 0; i < g_numtexinfo; i++, tx++)
        {
#ifdef HLCSG_TEXMAP64_FIX
            char*          miptex_name = texmap_retrieve(tx->miptex);
#else
            char*          miptex_name = texmap64_retrieve(tx->miptex);
#endif

            tx->miptex = FindMiptex(miptex_name);

#ifndef HLCSG_TEXMAP64_FIX
            // Free up the originally strdup()'ed miptex_name
            free(miptex_name);
#endif
        }
#ifdef HLCSG_TEXMAP64_FIX
		texmap_clear ();
#endif
    }
    end = I_FloatTime();
    Verbose("qsort(miptex) elapsed time = %ldms\n", (long)(end - start));

    start = I_FloatTime();
    {
        int             i;

        // Now setup to get the miptex data (or just the headers if using -wadtextures) from the wadfile
        l = (dmiptexlump_t*)g_dtexdata;
        data = (byte*) & l->dataofs[nummiptex];
        l->nummiptex = nummiptex;
#ifdef ZHLT_NOWADDIR
		char writewad_name[_MAX_PATH];
		FILE *writewad_file;
		int writewad_maxlumpinfos;
		typedef struct
		{
			int             filepos;
			int             disksize;
			int             size;
			char            type;
			char            compression;
			char            pad1, pad2;
			char            name[MAXWADNAME];
		} dlumpinfo_t;
		dlumpinfo_t *writewad_lumpinfos;
		wadinfo_t writewad_header;
		safe_snprintf (writewad_name, _MAX_PATH, "%s.wa_", g_Mapname);
		writewad_file = SafeOpenWrite (writewad_name);
		writewad_maxlumpinfos = nummiptex;
		writewad_lumpinfos = (dlumpinfo_t *)malloc (writewad_maxlumpinfos * sizeof (dlumpinfo_t));
		hlassume (writewad_lumpinfos != NULL, assume_NoMemory);
		writewad_header.identification[0] = 'W';
		writewad_header.identification[1] = 'A';
		writewad_header.identification[2] = 'D';
		writewad_header.identification[3] = '3';
		writewad_header.numlumps = 0;
		if (fseek (writewad_file, sizeof (wadinfo_t), SEEK_SET))
			Error ("File write failure");
#endif
        for (i = 0; i < nummiptex; i++)
        {
            l->dataofs[i] = data - (byte*) l;
#ifdef ZHLT_NOWADDIR
			byte *writewad_data;
			int writewad_datasize;
			len = LoadLump (miptex + i, data, &texsize
#ifdef HLCSG_FILEREADFAILURE_FIX
							, &g_dtexdata[g_max_map_miptex] - data
#endif
							, writewad_data, writewad_datasize);
			if (writewad_data)
			{
				dlumpinfo_t *writewad_lumpinfo = &writewad_lumpinfos[writewad_header.numlumps];
				writewad_lumpinfo->filepos = ftell (writewad_file);
				writewad_lumpinfo->disksize = writewad_datasize;
				writewad_lumpinfo->size = miptex[i].size;
				writewad_lumpinfo->type = miptex[i].type;
				writewad_lumpinfo->compression = miptex[i].compression;
				writewad_lumpinfo->pad1 = miptex[i].pad1;
				writewad_lumpinfo->pad2 = miptex[i].pad2;
				memcpy (writewad_lumpinfo->name, miptex[i].name, MAXWADNAME);
				writewad_header.numlumps++;
				SafeWrite (writewad_file, writewad_data, writewad_datasize);
				free (writewad_data);
			}
#else
            len = LoadLump(miptex + i, data, &texsize
#ifdef HLCSG_FILEREADFAILURE_FIX
							, &g_dtexdata[g_max_map_miptex] - data
#endif
							);
#endif

            if (!len)
            {
                l->dataofs[i] = -1;                        // didn't find the texture
            }
            else
            {
                totaltexsize += texsize;

                hlassume(totaltexsize < g_max_map_miptex, assume_MAX_MAP_MIPTEX);
            }
            data += len;
        }
        g_texdatasize = data - g_dtexdata;
#ifdef ZHLT_NOWADDIR
		writewad_header.infotableofs = ftell (writewad_file);
		SafeWrite (writewad_file, writewad_lumpinfos, writewad_header.numlumps * sizeof (dlumpinfo_t));
		if (fseek (writewad_file, 0, SEEK_SET))
			Error ("File write failure");
		SafeWrite (writewad_file, &writewad_header, sizeof (wadinfo_t));
		if (fclose (writewad_file))
			Error ("File write failure");
#endif
    }
    end = I_FloatTime();
    Log("Texture usage is at %1.2f mb (of %1.2f mb MAX)\n", (float)totaltexsize / (1024 * 1024),
        (float)g_max_map_miptex / (1024 * 1024));
    Verbose("LoadLump() elapsed time = %ldms\n", (long)(end - start));
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2068, %rsp           # imm = 0x2068
leaq 0x81805d(%rip), %rax    # 0x82f4c4
andl $0x0, (%rax)
callq 0x7554
movsd %xmm0, 0x8(%rsp)
callq 0x16af4
callq 0x17296
callq 0x7554
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x78d9(%rip), %rdi      # 0x1ed6f
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x91d2
leaq 0x61b4ac9(%rip), %rbx   # 0x61cbf70
callq 0x7554
movsd %xmm0, 0x8(%rsp)
leaq 0x61b4297(%rip), %r12   # 0x61cb750
movslq 0x61b4aac(%rip), %rax   # 0x61cbf6c
cmpq %rax, %r14
jge 0x1750b
movq %rbx, %rdi
callq 0x16fbc
testq %rax, %rax
je 0x174fa
movl 0x20(%rax), %ecx
movl %ecx, 0x20(%rbx)
movupd (%rax), %xmm0
movupd 0x10(%rax), %xmm1
movupd %xmm1, 0x10(%rbx)
movupd %xmm0, (%rbx)
movslq 0x20(%rax), %rax
movq (%r12,%rax,8), %rax
incl 0x1004(%rax)
jmp 0x17502
andq $0x0, (%rbx)
andl $0x0, 0x20(%rbx)
incq %r14
addq $0x24, %rbx
jmp 0x174b9
callq 0x7554
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x788a(%rip), %rdi      # 0x1edac
xorl %eax, %eax
callq 0x91d2
movb $0x0, 0x60(%rsp)
movl 0x61b461c(%rip), %eax   # 0x61cbb50
leaq 0x7894(%rip), %rbx      # 0x1edcf
leaq 0x1bdc(%rip), %r13      # 0x1911e
leaq 0x789d(%rip), %r14      # 0x1ede6
xorl %ebp, %ebp
movslq %eax, %rcx
cmpq %rcx, %rbp
jge 0x175db
movq (%r12), %r15
cmpb $0x0, 0x1000(%r15)
jne 0x175cf
cmpl $0x0, 0x1004(%r15)
jg 0x1757b
leaq 0x61b40eb(%rip), %rcx   # 0x61cb661
cmpb $0x0, (%rcx)
jne 0x175cf
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x89dc
movl 0x1004(%r15), %esi
movl 0x1008(%r15), %ecx
cvtsi2sd %esi, %xmm0
cvtsi2sdl 0x61b49ca(%rip), %xmm1 # 0x61cbf6c
divsd %xmm1, %xmm0
mulsd 0x1aca(%rip), %xmm0     # 0x19078
cmpl $0x1, %esi
movq %r13, %rdx
leaq 0x365e(%rip), %rax      # 0x1ac19
cmoveq %rax, %rdx
movq %r14, %rdi
movb $0x1, %al
callq 0x89dc
movl 0x61b4581(%rip), %eax   # 0x61cbb50
incq %rbp
addq $0x8, %r12
jmp 0x1754b
leaq 0x61b416e(%rip), %r14   # 0x61cb750
leaq 0x1060(%rsp), %r15
leaq 0x60(%rsp), %r12
xorl %ebx, %ebx
movslq %eax, %rcx
cmpq %rcx, %rbx
jge 0x176bb
movq (%r14), %rbp
cmpb $0x1, 0x1000(%rbp)
jne 0x176af
cmpl $0x0, 0x1004(%rbp)
jg 0x17626
leaq 0x61b4044(%rip), %rcx   # 0x61cb661
cmpb $0x0, (%rcx)
jne 0x176af
leaq 0x7803(%rip), %rdi      # 0x1ee30
movq %rbp, %rsi
xorl %eax, %eax
callq 0x89dc
movl 0x1004(%rbp), %esi
movl 0x1008(%rbp), %ecx
cvtsi2sd %esi, %xmm0
cvtsi2sdl 0x61b491d(%rip), %xmm1 # 0x61cbf6c
divsd %xmm1, %xmm0
mulsd 0x1a1d(%rip), %xmm0     # 0x19078
cmpl $0x1, %esi
movq %r13, %rdx
leaq 0x35b1(%rip), %rax      # 0x1ac19
cmoveq %rax, %rdx
leaq 0x7773(%rip), %rdi      # 0x1ede6
movb $0x1, %al
callq 0x89dc
movq %rbp, %rdi
movq %r15, %rsi
callq 0x77a2
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r15, %rsi
callq 0x7984
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
leaq 0x779f(%rip), %rsi      # 0x1ee43
callq 0x7984
movl 0x61b44a1(%rip), %eax   # 0x61cbb50
incq %rbx
addq $0x8, %r14
jmp 0x175f1
leaq 0x3556(%rip), %rdi      # 0x1ac18
xorl %eax, %eax
callq 0x89dc
cmpb $0x0, 0x60(%rsp)
je 0x176e5
leaq 0x1cc1(%rip), %rdi      # 0x19398
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x89dc
jmp 0x176f3
leaq 0x1c2e(%rip), %rdi      # 0x1931a
xorl %eax, %eax
callq 0x89dc
leaq 0x2005d66(%rip), %rdi   # 0x201d460
leaq 0x6175(%rip), %rsi      # 0x1d876
leaq 0x60(%rsp), %rdx
callq 0x69e6
callq 0x7554
movsd %xmm0, 0x8(%rsp)
movslq 0x61b484f(%rip), %rsi   # 0x61cbf6c
leaq 0x61b484c(%rip), %rbx   # 0x61cbf70
leaq 0x391(%rip), %rcx       # 0x17abc
pushq $0x24
popq %rdx
movq %rbx, %rdi
callq 0x4620
leaq 0x1177ce3(%rip), %r14   # 0x118f420
addq $0x20, %r14
xorl %ebp, %ebp
leaq 0x1177cd2(%rip), %r15   # 0x118f41c
cmpl (%r15), %ebp
jge 0x1776a
movl (%r14), %edi
callq 0x17ad2
movq %rax, %rdi
callq 0x1739c
movl %eax, (%r14)
incl %ebp
addq $0x28, %r14
jmp 0x1774a
callq 0xa058
movl 0x61d87fa(%rip), %r14d  # 0x61eff70
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
shlq $0x3, %r14
leaq 0x61d87f5(%rip), %r12   # 0x61eff80
cmpq %r15, %r14
je 0x1779f
movq (%r15,%r12), %rdi
callq 0x43b0
addq $0x8, %r15
jmp 0x1778b
andl $0x0, 0x61d87ca(%rip)   # 0x61eff70
callq 0xa069
callq 0x7554
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x7683(%rip), %rdi      # 0x1ee45
movq $0x0, 0x18(%rsp)
xorl %eax, %eax
callq 0x91d2
callq 0x7554
movsd %xmm0, 0x48(%rsp)
leaq 0x817ce4(%rip), %rax    # 0x82f4c8
movq (%rax), %rax
movslq 0x61b477e(%rip), %r15   # 0x61cbf6c
movq %rax, 0x38(%rsp)
movl %r15d, (%rax)
leaq 0x31fb(%rip), %rdx      # 0x1a9f8
leaq 0xeb5c(%rip), %rcx      # 0x26360
leaq 0x60(%rsp), %r14
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
xorl %eax, %eax
callq 0x78cb
movq %r14, %rdi
callq 0x852f
movq %rax, %r14
movslq 0x61b4742(%rip), %rdi   # 0x61cbf6c
shlq $0x5, %rdi
callq 0x4450
xorl %edi, %edi
movq %rax, 0x30(%rsp)
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x9517
movq $0x33444157, 0x1060(%rsp) # imm = 0x33444157
pushq $0xc
popq %rsi
movq %r14, 0x10(%rsp)
movq %r14, %rdi
xorl %edx, %edx
callq 0x4370
testl %eax, %eax
je 0x17879
leaq 0x30c9(%rip), %rdi      # 0x1a93b
xorl %eax, %eax
callq 0x8d7b
movq 0x38(%rsp), %rax
leaq 0x4(%rax), %rcx
movq %rcx, 0x50(%rsp)
leaq (%rax,%r15,4), %r13
addq $0x4, %r13
leaq 0xeaaa(%rip), %r15      # 0x26340
xorl %edx, %edx
xorl %ebp, %ebp
movslq 0x61b46cb(%rip), %rax   # 0x61cbf6c
cmpq %rax, %rdx
jge 0x179c4
movl %r13d, %eax
movq 0x38(%rsp), %rcx
subl %ecx, %eax
movq %rdx, 0x40(%rsp)
movl %eax, 0x4(%rcx,%rdx,4)
leaq 0x817c04(%rip), %rax    # 0x82f4c8
movl (%rax), %ecx
addl (%r15), %ecx
subl %r13d, %ecx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x2c(%rsp), %rdx
leaq 0x58(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x170fd
movl %eax, 0x24(%rsp)
movq 0x58(%rsp), %r14
testq %r14, %r14
movq %rbx, 0x8(%rsp)
je 0x17974
movq %rbx, %r12
movl %ebp, 0x20(%rsp)
movq 0x18(%rsp), %rbp
movslq %ebp, %rbx
shlq $0x5, %rbx
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x4030
movq 0x30(%rsp), %rcx
movl %eax, (%rcx,%rbx)
movl 0x28(%rsp), %edx
movl %edx, 0x4(%rcx,%rbx)
movl 0x8(%r12), %eax
movl %eax, 0x8(%rcx,%rbx)
movl 0xc(%r12), %eax
movl %eax, 0xc(%rcx,%rbx)
movupd 0x10(%r12), %xmm0
movupd %xmm0, 0x10(%rcx,%rbx)
incl %ebp
movq %rbp, 0x18(%rsp)
movl %ebp, 0x1064(%rsp)
movl 0x20(%rsp), %ebp
movq %r15, %rdi
leaq 0xe9dc(%rip), %r15      # 0x26340
movq %r14, %rsi
callq 0x85e9
movq %r14, %rdi
callq 0x43b0
movl 0x24(%rsp), %r14d
testl %r14d, %r14d
je 0x1799f
addl 0x2c(%rsp), %ebp
xorl %edi, %edi
cmpl (%r15), %ebp
setl %dil
pushq $0x25
popq %rsi
callq 0x9517
movq 0x8(%rsp), %rbx
movq 0x40(%rsp), %rdx
jmp 0x179b2
movq 0x50(%rsp), %rax
movq 0x40(%rsp), %rdx
orl $-0x1, (%rax,%rdx,4)
movq 0x8(%rsp), %rbx
movslq %r14d, %rax
addq %rax, %r13
incq %rdx
addq $0x24, %rbx
jmp 0x1789a
leaq 0x817afd(%rip), %rax    # 0x82f4c8
subl (%rax), %r13d
leaq 0x817aef(%rip), %rax    # 0x82f4c4
movl %r13d, (%rax)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4030
movl %eax, 0x1068(%rsp)
movq 0x18(%rsp), %rdx
shll $0x5, %edx
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0x85e9
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4370
testl %eax, %eax
je 0x17a1f
leaq 0x2f23(%rip), %rdi      # 0x1a93b
xorl %eax, %eax
callq 0x8d7b
leaq 0x1060(%rsp), %rsi
pushq $0xc
popq %rdx
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x85e9
movq %rbx, %rdi
callq 0x4300
testl %eax, %eax
je 0x17a51
leaq 0x2ef1(%rip), %rdi      # 0x1a93b
xorl %eax, %eax
callq 0x8d7b
callq 0x7554
movsd %xmm0, 0x8(%rsp)
cvtsi2ss %ebp, %xmm0
movss 0x713c(%rip), %xmm1     # 0x1eba4
cvtsi2ssl (%r15), %xmm2
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
mulss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm1
leaq 0x73e5(%rip), %rdi      # 0x1ee69
movb $0x2, %al
callq 0x89dc
movsd 0x8(%rsp), %xmm0
subsd 0x48(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x73f6(%rip), %rdi      # 0x1ee99
xorl %eax, %eax
callq 0x91d2
addq $0x2068, %rsp           # imm = 0x2068
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq