// File: /workspace/github_cpp_makefile_star2_O3/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 $0x2058, %rsp           # imm = 0x2058
leaq 0x817926(%rip), %rax    # 0x8314b4
movl $0x0, (%rax)
callq 0x8478
movsd %xmm0, (%rsp)
callq 0x191d8
callq 0x199d2
callq 0x8478
subsd (%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x82c1(%rip), %rdi      # 0x21e7f
xorl %eax, %eax
callq 0xa5b7
callq 0x8478
movsd %xmm0, (%rsp)
cmpl $0x0, 0x61b4386(%rip)   # 0x61cdf5c
jle 0x19c3f
leaq 0x61b4381(%rip), %rbx   # 0x61cdf60
xorl %r14d, %r14d
leaq 0x61b3b57(%rip), %r15   # 0x61cd740
movq %rbx, %rdi
callq 0x196de
testq %rax, %rax
je 0x19c1e
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 (%r15,%rax,8), %rax
incl 0x1004(%rax)
jmp 0x19c2c
movq $0x0, (%rbx)
movl $0x0, 0x20(%rbx)
incq %r14
movslq 0x61b4326(%rip), %rax   # 0x61cdf5c
addq $0x24, %rbx
cmpq %rax, %r14
jl 0x19be9
callq 0x8478
subsd (%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x8267(%rip), %rdi      # 0x21ebc
xorl %eax, %eax
callq 0xa5b7
movb $0x0, 0x50(%rsp)
movl 0x61b3ed9(%rip), %eax   # 0x61cdb40
testl %eax, %eax
jle 0x19e07
leaq 0x61b3aca(%rip), %r12   # 0x61cd740
leaq 0x8262(%rip), %rbx      # 0x21edf
leaq 0x2532(%rip), %r13      # 0x1c1b6
leaq 0x826b(%rip), %r14      # 0x21ef6
xorl %ebp, %ebp
movq (%r12), %r15
cmpb $0x0, 0x1000(%r15)
jne 0x19d0b
cmpl $0x0, 0x1004(%r15)
jg 0x19cb1
leaq 0x61b39a5(%rip), %rcx   # 0x61cd651
cmpb $0x0, (%rcx)
jne 0x19d0b
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x9e76
movl 0x1004(%r15), %esi
movl 0x1008(%r15), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x61b427e(%rip), %xmm1 # 0x61cdf5c
divsd %xmm1, %xmm0
mulsd 0x23ce(%rip), %xmm0     # 0x1c0b8
cmpl $0x1, %esi
movq %r13, %rdx
leaq 0x3fb2(%rip), %rax      # 0x1dca9
cmoveq %rax, %rdx
movq %r14, %rdi
movb $0x1, %al
callq 0x9e76
movl 0x61b3e35(%rip), %eax   # 0x61cdb40
incq %rbp
movslq %eax, %rcx
addq $0x8, %r12
cmpq %rcx, %rbp
jl 0x19c8d
testl %eax, %eax
jle 0x19e07
leaq 0x61b3a13(%rip), %r14   # 0x61cd740
leaq 0x1050(%rsp), %r15
leaq 0x50(%rsp), %r12
xorl %ebx, %ebx
movq (%r14), %rbp
cmpb $0x1, 0x1000(%rbp)
jne 0x19df4
cmpl $0x0, 0x1004(%rbp)
jg 0x19d65
leaq 0x61b38f5(%rip), %rcx   # 0x61cd651
cmpb $0x0, (%rcx)
jne 0x19df4
leaq 0x81d4(%rip), %rdi      # 0x21f40
movq %rbp, %rsi
xorl %eax, %eax
callq 0x9e76
movl 0x1004(%rbp), %esi
movl 0x1008(%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x61b41c8(%rip), %xmm1 # 0x61cdf5c
divsd %xmm1, %xmm0
mulsd 0x2318(%rip), %xmm0     # 0x1c0b8
cmpl $0x1, %esi
movq %r13, %rdx
leaq 0x3efc(%rip), %rax      # 0x1dca9
cmoveq %rax, %rdx
leaq 0x813e(%rip), %rdi      # 0x21ef6
movb $0x1, %al
callq 0x9e76
movq %rbp, %rdi
movq %r15, %rsi
callq 0x87f2
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r15, %rsi
callq 0x8b00
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
leaq 0x816a(%rip), %rsi      # 0x21f53
callq 0x8b00
movl 0x61b3d4c(%rip), %eax   # 0x61cdb40
incq %rbx
movslq %eax, %rcx
addq $0x8, %r14
cmpq %rcx, %rbx
jl 0x19d3c
leaq 0x3e9a(%rip), %rdi      # 0x1dca8
xorl %eax, %eax
callq 0x9e76
cmpb $0x0, 0x50(%rsp)
je 0x19e31
leaq 0x260d(%rip), %rdi      # 0x1c430
leaq 0x50(%rsp), %rsi
xorl %eax, %eax
callq 0x9e76
jmp 0x19e3f
leaq 0x257a(%rip), %rdi      # 0x1c3b2
xorl %eax, %eax
callq 0x9e76
leaq 0x200560a(%rip), %rdi   # 0x201f450
leaq 0x6b39(%rip), %rsi      # 0x20986
leaq 0x50(%rsp), %rdx
callq 0x757d
callq 0x8478
movsd %xmm0, (%rsp)
movslq 0x61b40f4(%rip), %rsi   # 0x61cdf5c
leaq 0x61b40f1(%rip), %rbx   # 0x61cdf60
leaq 0x3c5(%rip), %rcx       # 0x1a23b
movl $0x24, %edx
movq %rbx, %rdi
callq 0x4600
leaq 0x1177582(%rip), %r15   # 0x119140c
cmpl $0x0, (%r15)
leaq 0x61d80db(%rip), %r14   # 0x61f1f70
jle 0x19edf
leaq 0x1177572(%rip), %r12   # 0x1191410
addq $0x20, %r12
xorl %ebp, %ebp
movl (%r12), %r13d
testl %r13d, %r13d
setns %al
cmpl %r13d, 0x61d80ab(%rip)  # 0x61f1f60
setg %cl
andb %al, %cl
movzbl %cl, %edi
xorl %esi, %esi
callq 0xa91d
movslq %r13d, %rax
movq (%r14,%rax,8), %rdi
callq 0x19abd
movl %eax, (%r12)
incl %ebp
addq $0x28, %r12
cmpl (%r15), %ebp
jl 0x19ea4
callq 0xb551
movslq 0x61d8075(%rip), %r15   # 0x61f1f60
testq %r15, %r15
jle 0x19f09
shlq $0x3, %r15
xorl %r12d, %r12d
movq (%r12,%r14), %rdi
callq 0x4390
addq $0x8, %r12
cmpq %r12, %r15
jne 0x19ef7
movl $0x0, 0x61d804d(%rip)   # 0x61f1f60
callq 0xb562
callq 0x8478
subsd (%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x8027(%rip), %rdi      # 0x21f55
xorl %r15d, %r15d
xorl %eax, %eax
callq 0xa5b7
callq 0x8478
movsd %xmm0, 0x38(%rsp)
leaq 0x81756e(%rip), %rax    # 0x8314b8
movq (%rax), %rbp
movslq 0x61b4008(%rip), %r12   # 0x61cdf5c
movl %r12d, (%rbp)
leaq 0x3b29(%rip), %rdx      # 0x1da88
leaq 0xe3ea(%rip), %rcx      # 0x28350
leaq 0x50(%rsp), %r14
movl $0x1000, %esi           # imm = 0x1000
movq %r14, %rdi
xorl %eax, %eax
callq 0x8a47
movq %r14, %rdi
callq 0x99c0
movq %rax, %r14
movslq 0x61b3fd0(%rip), %rdi   # 0x61cdf5c
shlq $0x5, %rdi
callq 0x4420
xorl %edi, %edi
movq %rax, 0x28(%rsp)
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa91d
movq $0x33444157, 0x1050(%rsp) # imm = 0x33444157
movl $0xc, %esi
movq %r14, 0x10(%rsp)
movq %r14, %rdi
xorl %edx, %edx
callq 0x4350
testl %eax, %eax
je 0x19fdf
leaq 0x39f3(%rip), %rdi      # 0x1d9cb
xorl %eax, %eax
callq 0xa164
leaq 0x4(,%r12,4), %r13
addq %rbp, %r13
cmpl $0x0, 0x61b3f6b(%rip)   # 0x61cdf5c
jle 0x1a142
leaq 0x4(%rbp), %rax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
xorl %r12d, %r12d
movl $0x0, 0xc(%rsp)
movq %rbp, 0x30(%rsp)
movl %r13d, %eax
subl %ebp, %eax
movl %eax, 0x4(%rbp,%r12,4)
leaq 0x817494(%rip), %rax    # 0x8314b8
movl (%rax), %ecx
leaq 0xe303(%rip), %rax      # 0x28330
addl (%rax), %ecx
subl %r13d, %ecx
movq %rbx, %rdi
movq %r13, %rsi
leaq 0x24(%rsp), %rdx
leaq 0x48(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x1982c
movl %eax, 0x18(%rsp)
movq 0x48(%rsp), %r14
testq %r14, %r14
movq %rbx, (%rsp)
je 0x1a0c3
movslq %r15d, %r15
movq %r15, %rbp
shlq $0x5, %r15
movq 0x10(%rsp), %rdi
callq 0x4030
movq 0x28(%rsp), %rcx
movl %eax, (%rcx,%r15)
movl 0x20(%rsp), %edx
movl %edx, 0x4(%rcx,%r15)
movl 0x8(%rbx), %eax
movl %eax, 0x8(%rcx,%r15)
movl 0xc(%rbx), %eax
movl %eax, 0xc(%rcx,%r15)
movupd 0x10(%rbx), %xmm0
movupd %xmm0, 0x10(%rcx,%r15)
movq %rbp, %r15
incl %r15d
movl %r15d, 0x1054(%rsp)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x9a7c
movq %r14, %rdi
callq 0x4390
movl 0x18(%rsp), %r14d
testl %r14d, %r14d
je 0x1a0f4
movl 0xc(%rsp), %ecx
addl 0x24(%rsp), %ecx
xorl %edi, %edi
leaq 0xe252(%rip), %rax      # 0x28330
movl %ecx, 0xc(%rsp)
cmpl (%rax), %ecx
setl %dil
movl $0x25, %esi
callq 0xa91d
jmp 0x1a101
movq 0x40(%rsp), %rax
movl $0xffffffff, (%rax,%r12,4) # imm = 0xFFFFFFFF
movq (%rsp), %rbx
movq 0x30(%rsp), %rbp
movslq %r14d, %rax
addq %rax, %r13
incq %r12
movslq 0x61b3e42(%rip), %rax   # 0x61cdf5c
addq $0x24, %rbx
cmpq %rax, %r12
jl 0x1a013
xorps %xmm0, %xmm0
cvtsi2ssl 0xc(%rsp), %xmm0
shll $0x5, %r15d
mulss 0x7b8c(%rip), %xmm0     # 0x21cc8
cvtss2sd %xmm0, %xmm0
jmp 0x1a146
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0x817366(%rip), %rax    # 0x8314b8
subl (%rax), %r13d
leaq 0x817358(%rip), %rax    # 0x8314b4
movl %r13d, (%rax)
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4030
movl %eax, 0x1058(%rsp)
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movl %r15d, %edx
callq 0x9a7c
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4350
testl %eax, %eax
je 0x1a1a1
leaq 0x3831(%rip), %rdi      # 0x1d9cb
xorl %eax, %eax
callq 0xa164
leaq 0x1050(%rsp), %rsi
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movl $0xc, %edx
callq 0x9a7c
movq %rbx, %rdi
callq 0x42f0
testl %eax, %eax
je 0x1a1d5
leaq 0x37fd(%rip), %rdi      # 0x1d9cb
xorl %eax, %eax
callq 0xa164
callq 0x8478
leaq 0xe14f(%rip), %rax      # 0x28330
cvtsi2ssl (%rax), %xmm1
movsd %xmm0, 0x18(%rsp)
mulss 0x7ad5(%rip), %xmm1     # 0x21cc8
cvtss2sd %xmm1, %xmm1
leaq 0x7d7b(%rip), %rdi      # 0x21f79
movsd (%rsp), %xmm0
movb $0x2, %al
callq 0x9e76
movsd 0x18(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x7d87(%rip), %rdi      # 0x21fa9
xorl %eax, %eax
callq 0xa5b7
addq $0x2058, %rsp           # imm = 0x2058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq