// File: FreeSlave[P]vhlt/ripent/ripent.cpp
// Function: ReadTextures(char const*)
static void		ReadTextures(const char *name)
{
	char wadfilename[_MAX_PATH];
	FILE *wadfile;
#ifdef ZHLT_DEFAULTEXTENSION_FIX
	safe_snprintf(wadfilename, _MAX_PATH, "%s.wad", name);
#else
    safe_strncpy(wadfilename, name, _MAX_PATH);
    StripExtension(wadfilename);
    DefaultExtension(wadfilename, ".wad");
#endif
	wadfile = SafeOpenRead (wadfilename);
	Log("\nReading %s.\n", wadfilename);

    char texfilename[_MAX_PATH];
	FILE *texfile;
#ifdef ZHLT_DEFAULTEXTENSION_FIX
	safe_snprintf(texfilename, _MAX_PATH, "%s.tex", name);
#else
    safe_strncpy(texfilename, name, _MAX_PATH);
    StripExtension(texfilename);
    DefaultExtension(texfilename, ".tex");
#endif
	if (!g_textureparse)
	{
		wadinfo_t header;
		int wadofs = sizeof(wadinfo_t);
		SafeRead (wadfile, &header, wadofs);
		((dmiptexlump_t*)g_dtexdata)->nummiptex = header.numlumps;
		int dataofs = (int)(intptr_t)&((dmiptexlump_t*)NULL)->dataofs[((dmiptexlump_t*)g_dtexdata)->nummiptex];
		g_texdatasize = header.infotableofs - wadofs + dataofs;

		SafeRead (wadfile, (byte *)g_dtexdata + dataofs, g_texdatasize - dataofs);
		
		lumpinfo_t *info;
		info = (lumpinfo_t *)malloc (header.numlumps * sizeof (lumpinfo_t));
		hlassume (info != NULL, assume_NoMemory);
		SafeRead (wadfile, info, header.numlumps * sizeof(lumpinfo_t));

		for (int i = 0; i < header.numlumps; i++)
		{
			((dmiptexlump_t*)g_dtexdata)->dataofs[i] = info[i].filepos - wadofs + dataofs;
		}

		free (info);
	}
	else
	{
		texfile = SafeOpenRead (texfilename);
		Log("\nReading %s.\n", texfilename);

		wadinfo_t header;
		SafeRead (wadfile, &header, sizeof(wadinfo_t));
		fseek (wadfile, header.infotableofs, SEEK_SET);

		lumpinfo_t *info;
		info = (lumpinfo_t *)malloc (header.numlumps * sizeof (lumpinfo_t));
		hlassume (info != NULL, assume_NoMemory);
		SafeRead (wadfile, info, header.numlumps * sizeof(lumpinfo_t));

		int nummiptex = 0;
		if (skipspace (texfile), fscanf (texfile, "%d", &nummiptex) != 1)
			Error ("File read failure");
		((dmiptexlump_t*)g_dtexdata)->nummiptex = nummiptex;
		g_texdatasize = (byte *)(&((dmiptexlump_t*)g_dtexdata)->dataofs[nummiptex]) - g_dtexdata;

		for (int itex = 0; itex < nummiptex; ++itex)
		{
			int len;
			if (skipspace (texfile), fscanf (texfile, "[%d]", &len) != 1)
				Error ("File read failure");
			if (len < 0)
			{
				((dmiptexlump_t*)g_dtexdata)->dataofs[itex] = -1;
			}
			else
			{
				char name[16];
				if (len > 15)
					Error ("Texture name is too long");
				memset (name, '\0', 16);
				SafeRead (texfile, name, len);
				((dmiptexlump_t*)g_dtexdata)->dataofs[itex] = g_texdatasize;
				miptex_t *tex = (miptex_t*)(g_dtexdata + g_texdatasize);
				int j;
				for (j = 0; j < header.numlumps; ++j)
					if (!strcasecmp (name, info[j].name))
						break;
				if (j == header.numlumps)
				{
					int w, h;
					if (skipspace (texfile), fscanf (texfile, "%d", &w) != 1)
						Error ("File read failure");
					if (skipspace (texfile), fscanf (texfile, "%d", &h) != 1)
						Error ("File read failure");
					g_texdatasize += sizeof(miptex_t);
					hlassume(g_texdatasize < g_max_map_miptex, assume_MAX_MAP_MIPTEX);
					memset (tex, 0, sizeof(miptex_t));
					strcpy (tex->name, name);
					tex->width = w;
					tex->height = h;
					for (int k = 0; k < MIPLEVELS; k++)
						tex->offsets[k] = 0;
				}
				else
				{
					fseek (wadfile, info[j].filepos, SEEK_SET);
					g_texdatasize += info[j].disksize;
					hlassume(g_texdatasize < g_max_map_miptex, assume_MAX_MAP_MIPTEX);
					SafeRead (wadfile, tex, info[j].disksize);
				}
			}
			skipline (texfile);
		}

		fclose (texfile);
		free (info);
	}
	fclose (wadfile);
}

// Assembly:
subq $0x2098, %rsp           # imm = 0x2098
movq %rdi, 0x2090(%rsp)
leaq 0x1090(%rsp), %rdi
movq 0x2090(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x6409(%rip), %rdx      # 0x18e04
movb $0x0, %al
callq 0x8350
leaq 0x1090(%rsp), %rdi
callq 0x99f0
movq %rax, 0x1088(%rsp)
leaq 0x1090(%rsp), %rsi
leaq 0x639a(%rip), %rdi      # 0x18dc0
movb $0x0, %al
callq 0x9f40
leaq 0x80(%rsp), %rdi
movq 0x2090(%rsp), %rcx
movl $0x1000, %esi           # imm = 0x1000
leaq 0x63c2(%rip), %rdx      # 0x18e0b
movb $0x0, %al
callq 0x8350
testb $0x1, 0x208ed4b(%rip)   # 0x20a17a2
jne 0x12b8f
movl $0xc, 0x68(%rsp)
movq 0x1088(%rsp), %rdi
movl 0x68(%rsp), %edx
leaq 0x6c(%rsp), %rsi
callq 0x9a50
movl 0x70(%rsp), %ecx
leaq 0x8138a2(%rip), %rax    # 0x826328
movq (%rax), %rax
movl %ecx, (%rax)
leaq 0x813896(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq (%rax), %rcx
xorl %eax, %eax
addq $0x4, %rax
shlq $0x2, %rcx
addq %rcx, %rax
movl %eax, 0x64(%rsp)
movl 0x74(%rsp), %ecx
subl 0x68(%rsp), %ecx
addl 0x64(%rsp), %ecx
leaq 0x813868(%rip), %rax    # 0x826324
movl %ecx, (%rax)
movq 0x1088(%rsp), %rdi
leaq 0x81385b(%rip), %rax    # 0x826328
movq (%rax), %rsi
movslq 0x64(%rsp), %rax
addq %rax, %rsi
leaq 0x813845(%rip), %rax    # 0x826324
movl (%rax), %edx
subl 0x64(%rsp), %edx
callq 0x9a50
movslq 0x70(%rsp), %rdi
shlq $0x5, %rdi
callq 0x3370
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xad70
movq 0x1088(%rsp), %rdi
movq 0x58(%rsp), %rsi
movslq 0x70(%rsp), %rax
shlq $0x5, %rax
movl %eax, %edx
callq 0x9a50
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x70(%rsp), %eax
jge 0x12b80
movq 0x58(%rsp), %rax
movslq 0x54(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %edx
subl 0x68(%rsp), %edx
addl 0x64(%rsp), %edx
leaq 0x8137c1(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x54(%rsp), %rcx
movl %edx, 0x4(%rax,%rcx,4)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x12b3b
movq 0x58(%rsp), %rdi
callq 0x3310
jmp 0x12fb5
leaq 0x80(%rsp), %rdi
callq 0x99f0
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rsi
leaq 0x6210(%rip), %rdi      # 0x18dc0
movb $0x0, %al
callq 0x9f40
movq 0x1088(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movl $0xc, %edx
callq 0x9a50
movq 0x1088(%rsp), %rdi
movslq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x32e0
movslq 0x4c(%rsp), %rdi
shlq $0x5, %rdi
callq 0x3370
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xad70
movq 0x1088(%rsp), %rdi
movq 0x40(%rsp), %rsi
movslq 0x4c(%rsp), %rax
shlq $0x5, %rax
movl %eax, %edx
callq 0x9a50
movl $0x0, 0x3c(%rsp)
movq 0x78(%rsp), %rdi
callq 0x13fd0
movq 0x78(%rsp), %rdi
leaq 0x250a(%rip), %rsi      # 0x15153
leaq 0x3c(%rsp), %rdx
movb $0x0, %al
callq 0x34d0
cmpl $0x1, %eax
je 0x12c68
leaq 0x3ca8(%rip), %rdi      # 0x16909
movb $0x0, %al
callq 0xa3f0
movl 0x3c(%rsp), %ecx
leaq 0x8136b5(%rip), %rax    # 0x826328
movq (%rax), %rax
movl %ecx, (%rax)
leaq 0x8136a9(%rip), %rax    # 0x826328
movq (%rax), %rax
addq $0x4, %rax
movslq 0x3c(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
leaq 0x81368f(%rip), %rcx    # 0x826328
movq (%rcx), %rcx
subq %rcx, %rax
movl %eax, %ecx
leaq 0x81367c(%rip), %rax    # 0x826324
movl %ecx, (%rax)
movl $0x0, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x3c(%rsp), %eax
jge 0x12fa1
movq 0x78(%rsp), %rdi
callq 0x13fd0
movq 0x78(%rsp), %rdi
leaq 0x613c(%rip), %rsi      # 0x18e12
leaq 0x34(%rsp), %rdx
movb $0x0, %al
callq 0x34d0
cmpl $0x1, %eax
je 0x12cf5
leaq 0x3c1b(%rip), %rdi      # 0x16909
movb $0x0, %al
callq 0xa3f0
cmpl $0x0, 0x34(%rsp)
jge 0x12d18
leaq 0x813625(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x38(%rsp), %rcx
movl $0xffffffff, 0x4(%rax,%rcx,4) # imm = 0xFFFFFFFF
jmp 0x12f87
cmpl $0xf, 0x34(%rsp)
jle 0x12d2d
leaq 0x60f1(%rip), %rdi      # 0x18e17
movb $0x0, %al
callq 0xa3f0
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3170
movq 0x78(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movl 0x34(%rsp), %edx
callq 0x9a50
leaq 0x8135cc(%rip), %rax    # 0x826324
movl (%rax), %edx
leaq 0x8135c7(%rip), %rax    # 0x826328
movq (%rax), %rax
movslq 0x38(%rsp), %rcx
movl %edx, 0x4(%rax,%rcx,4)
leaq 0x8135b4(%rip), %rax    # 0x826328
movq (%rax), %rax
leaq 0x8135a6(%rip), %rcx    # 0x826324
movslq (%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x14(%rsp)
movl 0x14(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0x12dd0
leaq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
movslq 0x14(%rsp), %rax
shlq $0x5, %rax
addq %rax, %rsi
addq $0x10, %rsi
callq 0x32c0
cmpl $0x0, %eax
jne 0x12dc1
jmp 0x12dd0
jmp 0x12dc3
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x12d91
movl 0x14(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jne 0x12ef1
movq 0x78(%rsp), %rdi
callq 0x13fd0
movq 0x78(%rsp), %rdi
leaq 0x235f(%rip), %rsi      # 0x15153
leaq 0x10(%rsp), %rdx
movb $0x0, %al
callq 0x34d0
cmpl $0x1, %eax
je 0x12e13
leaq 0x3afd(%rip), %rdi      # 0x16909
movb $0x0, %al
callq 0xa3f0
movq 0x78(%rsp), %rdi
callq 0x13fd0
movq 0x78(%rsp), %rdi
leaq 0x232a(%rip), %rsi      # 0x15153
leaq 0xc(%rsp), %rdx
movb $0x0, %al
callq 0x34d0
cmpl $0x1, %eax
je 0x12e48
leaq 0x3ac8(%rip), %rdi      # 0x16909
movb $0x0, %al
callq 0xa3f0
leaq 0x8134d5(%rip), %rax    # 0x826324
movslq (%rax), %rax
addq $0x28, %rax
movl %eax, %ecx
leaq 0x8134c5(%rip), %rax    # 0x826324
movl %ecx, (%rax)
leaq 0x8134bc(%rip), %rax    # 0x826324
movl (%rax), %eax
leaq 0xa42f(%rip), %rcx      # 0x1d2a0
cmpl (%rcx), %eax
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x25, %esi
callq 0xad70
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x3170
movq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3270
movl 0x10(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x10(%rax)
movl 0xc(%rsp), %ecx
movq 0x18(%rsp), %rax
movl %ecx, 0x14(%rax)
movl $0x0, 0x8(%rsp)
cmpl $0x4, 0x8(%rsp)
jge 0x12eec
movq 0x18(%rsp), %rax
movslq 0x8(%rsp), %rcx
movl $0x0, 0x18(%rax,%rcx,4)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0x12ec6
jmp 0x12f85
movq 0x1088(%rsp), %rdi
movq 0x40(%rsp), %rax
movslq 0x14(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq (%rax), %rsi
xorl %edx, %edx
callq 0x32e0
movq 0x40(%rsp), %rax
movslq 0x14(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
leaq 0x8133f5(%rip), %rax    # 0x826324
addl (%rax), %ecx
leaq 0x8133ec(%rip), %rax    # 0x826324
movl %ecx, (%rax)
leaq 0x8133e3(%rip), %rax    # 0x826324
movl (%rax), %eax
leaq 0xa356(%rip), %rcx      # 0x1d2a0
cmpl (%rcx), %eax
setl %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x25, %esi
callq 0xad70
movq 0x1088(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x40(%rsp), %rax
movslq 0x14(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %edx
callq 0x9a50
jmp 0x12f87
movq 0x78(%rsp), %rdi
callq 0x13ff0
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x12cb2
movq 0x78(%rsp), %rdi
callq 0x3290
movq 0x40(%rsp), %rdi
callq 0x3310
movq 0x1088(%rsp), %rdi
callq 0x3290
addq $0x2098, %rsp           # imm = 0x2098
retq
nopw (%rax,%rax)