// File: /workspace/github_cpp_makefile_star2_O1/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 0x81739d(%rip), %rax    # 0x8314c4
movl $0x0, (%rax)
callq 0x8598
movsd %xmm0, 0x8(%rsp)
callq 0x19770
callq 0x19f6d
callq 0x8598
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x7cea(%rip), %rdi      # 0x21e43
xorl %eax, %eax
callq 0xa6f3
callq 0x8598
movsd %xmm0, 0x8(%rsp)
cmpl $0x0, 0x61b3dfa(%rip)   # 0x61cdf6c
jle 0x1a1db
leaq 0x61b3df5(%rip), %rbx   # 0x61cdf70
xorl %r14d, %r14d
leaq 0x61b35cb(%rip), %r15   # 0x61cd750
movq %rbx, %rdi
callq 0x19c6f
testq %rax, %rax
je 0x1a1ba
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 0x1a1c8
movq $0x0, (%rbx)
movl $0x0, 0x20(%rbx)
incq %r14
movslq 0x61b3d9a(%rip), %rax   # 0x61cdf6c
addq $0x24, %rbx
cmpq %rax, %r14
jl 0x1a185
callq 0x8598
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x7c8e(%rip), %rdi      # 0x21e80
xorl %eax, %eax
callq 0xa6f3
movb $0x0, 0x1050(%rsp)
cmpl $0x0, 0x61b3948(%rip)   # 0x61cdb50
jle 0x1a2b8
leaq 0x61b353b(%rip), %r12   # 0x61cd750
leaq 0x3a9d(%rip), %r13      # 0x1dcb9
leaq 0x1f9b(%rip), %rbp      # 0x1c1be
leaq 0x7c90(%rip), %r14      # 0x21eba
xorl %ebx, %ebx
movq (%r12), %r15
cmpb $0x0, 0x1000(%r15)
jne 0x1a2a1
cmpl $0x0, 0x1004(%r15)
jg 0x1a250
leaq 0x61b3416(%rip), %rax   # 0x61cd661
cmpb $0x0, (%rax)
jne 0x1a2a1
leaq 0x7c4c(%rip), %rdi      # 0x21ea3
movq %r15, %rsi
xorl %eax, %eax
callq 0x9fb2
movl 0x1004(%r15), %esi
movl 0x1008(%r15), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x61b3ceb(%rip), %xmm1 # 0x61cdf6c
divsd %xmm1, %xmm0
mulsd 0x1e23(%rip), %xmm0     # 0x1c0b0
cmpl $0x1, %esi
movq %rbp, %rdx
cmoveq %r13, %rdx
movq %r14, %rdi
movb $0x1, %al
callq 0x9fb2
incq %rbx
movslq 0x61b38a5(%rip), %rax   # 0x61cdb50
addq $0x8, %r12
cmpq %rax, %rbx
jl 0x1a22c
cmpl $0x0, 0x61b3891(%rip)   # 0x61cdb50
jle 0x1a3ac
leaq 0x61b3484(%rip), %rbx   # 0x61cd750
leaq 0x50(%rsp), %r15
leaq 0x1050(%rsp), %r12
leaq 0x7c37(%rip), %r13      # 0x21f17
xorl %r14d, %r14d
movq (%rbx), %rbp
cmpb $0x1, 0x1000(%rbp)
jne 0x1a395
cmpl $0x0, 0x1004(%rbp)
jg 0x1a30c
leaq 0x61b335e(%rip), %rax   # 0x61cd661
cmpb $0x0, (%rax)
jne 0x1a395
leaq 0x7bf1(%rip), %rdi      # 0x21f04
movq %rbp, %rsi
xorl %eax, %eax
callq 0x9fb2
movl 0x1004(%rbp), %esi
movl 0x1008(%rbp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x61b3c31(%rip), %xmm1 # 0x61cdf6c
divsd %xmm1, %xmm0
mulsd 0x1d69(%rip), %xmm0     # 0x1c0b0
cmpl $0x1, %esi
leaq 0x1e6d(%rip), %rdx      # 0x1c1be
leaq 0x3961(%rip), %rax      # 0x1dcb9
cmoveq %rax, %rdx
leaq 0x7b57(%rip), %rdi      # 0x21eba
movb $0x1, %al
callq 0x9fb2
movq %rbp, %rdi
movq %r15, %rsi
callq 0x892b
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r15, %rsi
callq 0x8c38
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
movq %r13, %rsi
callq 0x8c38
incq %r14
movslq 0x61b37b1(%rip), %rax   # 0x61cdb50
addq $0x8, %rbx
cmpq %rax, %r14
jl 0x1a2e3
leaq 0x3905(%rip), %rdi      # 0x1dcb8
xorl %eax, %eax
callq 0x9fb2
cmpb $0x0, 0x1050(%rsp)
je 0x1a3dc
leaq 0x206d(%rip), %rdi      # 0x1c438
leaq 0x1050(%rsp), %rsi
xorl %eax, %eax
callq 0x9fb2
jmp 0x1a3ea
leaq 0x1fd7(%rip), %rdi      # 0x1c3ba
xorl %eax, %eax
callq 0x9fb2
leaq 0x200506f(%rip), %rdi   # 0x201f460
leaq 0x6569(%rip), %rsi      # 0x20961
leaq 0x1050(%rsp), %rdx
callq 0x75d4
callq 0x8598
movsd %xmm0, 0x8(%rsp)
movslq 0x61b3b55(%rip), %rsi   # 0x61cdf6c
leaq 0x61b3b52(%rip), %rbx   # 0x61cdf70
leaq 0x3ac(%rip), %rcx       # 0x1a7d1
movl $0x24, %edx
movq %rbx, %rdi
callq 0x4620
leaq 0x1176fe3(%rip), %r15   # 0x119141c
cmpl $0x0, (%r15)
leaq 0x61d7b3c(%rip), %r14   # 0x61f1f80
jle 0x1a48e
leaq 0x1176fd3(%rip), %r12   # 0x1191420
addq $0x20, %r12
xorl %ebp, %ebp
movl (%r12), %r13d
testl %r13d, %r13d
setns %al
cmpl %r13d, 0x61d7b0c(%rip)  # 0x61f1f70
setg %cl
andb %al, %cl
movzbl %cl, %edi
xorl %esi, %esi
callq 0xaa4d
movslq %r13d, %rax
movq (%r14,%rax,8), %rdi
callq 0x1a056
movl %eax, (%r12)
incl %ebp
addq $0x28, %r12
cmpl (%r15), %ebp
jl 0x1a453
callq 0xb695
movslq 0x61d7ad6(%rip), %r15   # 0x61f1f70
testq %r15, %r15
jle 0x1a4b8
shlq $0x3, %r15
xorl %r12d, %r12d
movq (%r12,%r14), %rdi
callq 0x43a0
addq $0x8, %r12
cmpq %r12, %r15
jne 0x1a4a6
movl $0x0, 0x61d7aae(%rip)   # 0x61f1f70
callq 0xb6a6
callq 0x8598
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x7a3b(%rip), %rdi      # 0x21f19
xorl %eax, %eax
callq 0xa6f3
callq 0x8598
movsd %xmm0, 0x30(%rsp)
leaq 0x816fd1(%rip), %rax    # 0x8314c8
movq (%rax), %rbp
movslq 0x61b3a6b(%rip), %r14   # 0x61cdf6c
movl %r14d, (%rbp)
leaq 0x358c(%rip), %rdx      # 0x1da98
leaq 0xde4d(%rip), %rcx      # 0x28360
leaq 0x1050(%rsp), %r15
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdi
xorl %eax, %eax
callq 0x8b7f
movq %r15, %rdi
callq 0x9aff
movq %rax, %r15
movslq 0x61b3a30(%rip), %rdi   # 0x61cdf6c
shlq $0x5, %rdi
callq 0x4430
xorl %edi, %edi
movq %rax, 0x28(%rsp)
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movq $0x33444157, 0x50(%rsp) # imm = 0x33444157
movl $0xc, %esi
movq %r15, 0x18(%rsp)
movq %r15, %rdi
xorl %edx, %edx
callq 0x4360
testl %eax, %eax
je 0x1a58c
leaq 0x3456(%rip), %rdi      # 0x1d9db
xorl %eax, %eax
callq 0xa2a0
leaq 0x4(,%r14,4), %r12
addq %rbp, %r12
cmpl $0x0, 0x61b39ce(%rip)   # 0x61cdf6c
jle 0x1a6d8
leaq 0x4(%rbp), %rax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
leaq 0xdd89(%rip), %r15      # 0x28340
movl $0x0, 0x10(%rsp)
movq %rbp, 0x40(%rsp)
movl %r12d, %eax
subl %ebp, %eax
movl %eax, 0x4(%rbp,%r14,4)
leaq 0x816ef3(%rip), %rax    # 0x8314c8
movl (%rax), %ecx
addl (%r15), %ecx
subl %r12d, %ecx
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x24(%rsp), %rdx
leaq 0x48(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x19dc7
movl %eax, 0x8(%rsp)
movq 0x48(%rsp), %r13
testq %r13, %r13
je 0x1a66c
movslq 0x54(%rsp), %r15
shlq $0x5, %r15
movq 0x18(%rsp), %rbp
movq %rbp, %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)
leaq 0xdcf0(%rip), %r15      # 0x28340
incl 0x54(%rsp)
movq %rbp, %rdi
movq 0x40(%rsp), %rbp
movq %r13, %rsi
callq 0x9bbb
movq %r13, %rdi
callq 0x43a0
movl 0x8(%rsp), %r13d
testl %r13d, %r13d
je 0x1a697
movl 0x10(%rsp), %eax
addl 0x24(%rsp), %eax
xorl %edi, %edi
movl %eax, 0x10(%rsp)
cmpl (%r15), %eax
setl %dil
movl $0x25, %esi
callq 0xaa4d
jmp 0x1a6a4
movq 0x38(%rsp), %rax
movl $0xffffffff, (%rax,%r14,4) # imm = 0xFFFFFFFF
movslq %r13d, %rax
addq %rax, %r12
incq %r14
movslq 0x61b38b8(%rip), %rax   # 0x61cdf6c
addq $0x24, %rbx
cmpq %rax, %r14
jl 0x1a5c4
xorps %xmm0, %xmm0
cvtsi2ssl 0x10(%rsp), %xmm0
mulss 0x75ce(%rip), %xmm0     # 0x21ca0
cvtss2sd %xmm0, %xmm0
jmp 0x1a6dc
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x816ddf(%rip), %rax    # 0x8314c8
subl (%rax), %r12d
leaq 0x816dd1(%rip), %rax    # 0x8314c4
movl %r12d, (%rax)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x4030
movl %eax, 0x58(%rsp)
movl 0x54(%rsp), %edx
shll $0x5, %edx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0x9bbb
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4360
testl %eax, %eax
je 0x1a739
leaq 0x32a9(%rip), %rdi      # 0x1d9db
xorl %eax, %eax
callq 0xa2a0
leaq 0x50(%rsp), %rsi
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movl $0xc, %edx
callq 0x9bbb
movq %rbx, %rdi
callq 0x4300
testl %eax, %eax
je 0x1a76a
leaq 0x3278(%rip), %rdi      # 0x1d9db
xorl %eax, %eax
callq 0xa2a0
callq 0x8598
leaq 0xdbca(%rip), %rax      # 0x28340
cvtsi2ssl (%rax), %xmm1
movsd %xmm0, 0x10(%rsp)
mulss 0x7518(%rip), %xmm1     # 0x21ca0
cvtss2sd %xmm1, %xmm1
leaq 0x77aa(%rip), %rdi      # 0x21f3d
movsd 0x8(%rsp), %xmm0
movb $0x2, %al
callq 0x9fb2
movsd 0x10(%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rsi
leaq 0x77b5(%rip), %rdi      # 0x21f6d
xorl %eax, %eax
callq 0xa6f3
addq $0x2058, %rsp           # imm = 0x2058
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq