// File: FreeSlave[P]vhlt/hlrad/loadtextures.cpp
// Function: LoadTextureFromWad(radtexture_t*, miptex_s const*)
void LoadTextureFromWad (radtexture_t *tex, const miptex_t *header)
{
	tex->width = header->width;
	tex->height = header->height;
	strcpy (tex->name, header->name);
	tex->name[16 - 1] = '\0';
	wadfile_t *wad;
	for (wad = g_wadfiles; wad; wad = wad->next)
	{
		lumpinfo_t temp, *found;
		strcpy (temp.name, tex->name);
		found = (lumpinfo_t *)bsearch (&temp, wad->lumpinfos, wad->numlumps, sizeof (lumpinfo_t), lump_sorter_by_name);
		if (found)
		{
			Developer (DEVELOPER_LEVEL_MESSAGE, "Texture '%s': found in '%s'.\n", tex->name, wad->path);
			if (found->type != 67 || found->compression != 0)
				continue;
			if (found->disksize < (int)sizeof (miptex_t) || found->filepos < 0 || found->filepos + found->disksize > wad->filesize)
			{
				Warning ("Texture '%s': invalid texture data in '%s'.", tex->name, wad->path);
				continue;
			}
			miptex_t *mt = (miptex_t *)malloc (found->disksize);
			hlassume (mt != NULL, assume_NoMemory);
			if (fseek (wad->file, found->filepos, SEEK_SET))
				Error ("File read failure");
			SafeRead (wad->file, mt, found->disksize);
			if (!TerminatedString(mt->name, 16))
			{
				Warning("Texture '%s': invalid texture data in '%s'.", tex->name, wad->path);
				free (mt);
				continue;
			}
			Developer (DEVELOPER_LEVEL_MESSAGE, "Texture '%s': name '%s', width %d, height %d.\n", tex->name, mt->name, mt->width, mt->height);
			if (strcasecmp (mt->name, tex->name))
			{
				Warning("Texture '%s': texture name '%s' differs from its reference name '%s' in '%s'.", tex->name, mt->name, tex->name, wad->path);
			}
			LoadTexture (tex, mt, found->disksize);
			free (mt);
			break;
		}
	}
	if (!wad)
	{
		Warning ("Texture '%s': texture is not found in wad files.", tex->name);
		DefaultTexture (tex, tex->name);
		return;
	}
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x38(%rsp), %rax
movl 0x10(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x38(%rsp), %rax
movl 0x14(%rax), %ecx
movq 0x40(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x42e0
movq 0x40(%rsp), %rax
movb $0x0, 0xf(%rax)
movq 0x5262b5b(%rip), %rax   # 0x52a79d8
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x30(%rsp)
je 0x450be
leaq 0x10(%rsp), %rdi
addq $0x10, %rdi
movq 0x40(%rsp), %rsi
callq 0x42e0
movq 0x30(%rsp), %rax
movq 0x1018(%rax), %rsi
movq 0x30(%rsp), %rax
movslq 0x1014(%rax), %rdx
leaq 0x10(%rsp), %rdi
movl $0x20, %ecx
leaq -0x7ba(%rip), %r8       # 0x44710
callq 0x4400
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0x450aa
movq 0x40(%rsp), %rdx
movq 0x30(%rsp), %rcx
addq $0x8, %rcx
movl $0x3, %edi
leaq 0x29850(%rip), %rsi     # 0x6e74a
movb $0x0, %al
callq 0xbbf0
movq 0x8(%rsp), %rax
movsbl 0xc(%rax), %eax
cmpl $0x43, %eax
jne 0x44f1d
movq 0x8(%rsp), %rax
movsbl 0xd(%rax), %eax
cmpl $0x0, %eax
je 0x44f22
jmp 0x450ac
movq 0x8(%rsp), %rax
cmpl $0x28, 0x4(%rax)
jl 0x44f53
movq 0x8(%rsp), %rax
cmpl $0x0, (%rax)
jl 0x44f53
movq 0x8(%rsp), %rax
movl (%rax), %eax
movq 0x8(%rsp), %rcx
addl 0x4(%rcx), %eax
movq 0x30(%rsp), %rcx
cmpl 0x1010(%rcx), %eax
jle 0x44f74
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq $0x8, %rdx
leaq 0x29800(%rip), %rdi     # 0x6e768
movb $0x0, %al
callq 0xb9f0
jmp 0x450ac
movq 0x8(%rsp), %rax
movslq 0x4(%rax), %rdi
callq 0x4430
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movq 0x30(%rsp), %rax
movq 0x1008(%rax), %rdi
movq 0x8(%rsp), %rax
movslq (%rax), %rsi
xorl %edx, %edx
callq 0x4370
cmpl $0x0, %eax
je 0x44fcc
leaq 0x26914(%rip), %rdi     # 0x6b8d9
movb $0x0, %al
callq 0xb580
movq 0x30(%rsp), %rax
movq 0x1008(%rax), %rdi
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
movl 0x4(%rax), %edx
callq 0xabe0
movq (%rsp), %rdi
movl $0x10, %esi
callq 0x96a0
testb $0x1, %al
jne 0x45025
movq 0x40(%rsp), %rsi
movq 0x30(%rsp), %rdx
addq $0x8, %rdx
leaq 0x29758(%rip), %rdi     # 0x6e768
movb $0x0, %al
callq 0xb9f0
movq (%rsp), %rdi
callq 0x43a0
jmp 0x450ac
movq 0x40(%rsp), %rdx
movq (%rsp), %rcx
movq (%rsp), %rax
movl 0x10(%rax), %r8d
movq (%rsp), %rax
movl 0x14(%rax), %r9d
movl $0x3, %edi
leaq 0x2974a(%rip), %rsi     # 0x6e794
movb $0x0, %al
callq 0xbbf0
movq (%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x4350
cmpl $0x0, %eax
je 0x45089
movq 0x40(%rsp), %rsi
movq (%rsp), %rdx
movq 0x40(%rsp), %rcx
movq 0x30(%rsp), %r8
addq $0x8, %r8
leaq 0x29741(%rip), %rdi     # 0x6e7c3
movb $0x0, %al
callq 0xb9f0
movq 0x40(%rsp), %rdi
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
movl 0x4(%rax), %edx
callq 0x44b40
movq (%rsp), %rdi
callq 0x43a0
jmp 0x450be
jmp 0x450ac
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
jmp 0x44e82
cmpq $0x0, 0x30(%rsp)
jne 0x450e8
movq 0x40(%rsp), %rsi
leaq 0x2973f(%rip), %rdi     # 0x6e811
movb $0x0, %al
callq 0xb9f0
movq 0x40(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x44a10
addq $0x48, %rsp
retq
nopl (%rax)