// File: FreeSlave[P]vhlt/hlcsg/textures.cpp
// Function: FindTexture(lumpinfo_t const*)
lumpinfo_t*     FindTexture(const lumpinfo_t* const source)
{
    //Log("** PnFNFUNC: FindTexture\n");

    lumpinfo_t*     found = NULL;

    found = (lumpinfo_t*)bsearch(source, (void*)lumpinfo, (size_t) nTexLumps, sizeof(lumpinfo[0]), lump_sorter_by_name);
    if (!found)
    {
        Warning("::FindTexture() texture %s not found!", source->name);
        if (!strcmp(source->name, "NULL")
#ifdef HLCSG_PASSBULLETSBRUSH
			|| !strcmp (source->name, "SKIP")
#endif
			)
        {
            Log("Are you sure you included zhlt.wad in your wadpath list?\n");
        }
    }

#ifdef HLCSG_AUTOWAD_NEW
	if (found)
	{
		// get the first and last matching lump
		lumpinfo_t *first = found;
		lumpinfo_t *last = found;
		while (first - 1 >= lumpinfo && lump_sorter_by_name (first - 1, source) == 0)
		{
			first = first - 1;
		}
		while (last + 1 < lumpinfo + nTexLumps && lump_sorter_by_name (last + 1, source) == 0)
		{
			last = last + 1;
		}
		// find the best matching lump
		lumpinfo_t *best = NULL;
		for (found = first; found < last + 1; found++)
		{
			bool better = false;
			if (best == NULL)
			{
				better = true;
			}
			else if (found->iTexFile != best->iTexFile)
			{
				wadpath_t *found_wadpath = texwadpathes[found->iTexFile];
				wadpath_t *best_wadpath = texwadpathes[best->iTexFile];
				if (found_wadpath->usedbymap != best_wadpath->usedbymap)
				{
					better = !found_wadpath->usedbymap; // included wad is better
				}
				else
				{
					better = found->iTexFile < best->iTexFile; // upper in the wad list is better
				}
			}
			else if (found->filepos != best->filepos)
			{
				better = found->filepos < best->filepos; // when there are several lumps with the same name in one wad file
			}

			if (better)
			{
				best = found;
			}
		}
		found = best;
	}
#endif
    return found;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq $0x0, 0x38(%rsp)
movq 0x40(%rsp), %rdi
movq 0x61b4f72(%rip), %rsi   # 0x61dbff0
movslq 0x61b4f73(%rip), %rdx   # 0x61dbff8
movl $0x24, %ecx
leaq -0x71(%rip), %r8        # 0x27020
callq 0x44d0
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0x270fe
movq 0x40(%rsp), %rsi
addq $0x10, %rsi
leaq 0x8d90(%rip), %rdi      # 0x2fe43
movb $0x0, %al
callq 0xbef0
movq 0x40(%rsp), %rdi
addq $0x10, %rdi
leaq 0x6cef(%rip), %rsi      # 0x2ddb9
callq 0x4530
cmpl $0x0, %eax
je 0x270ee
movq 0x40(%rsp), %rdi
addq $0x10, %rdi
leaq 0x72ec(%rip), %rsi      # 0x2e3d0
callq 0x4530
cmpl $0x0, %eax
jne 0x270fc
leaq 0x8d74(%rip), %rdi      # 0x2fe69
movb $0x0, %al
callq 0xb5d0
jmp 0x270fe
cmpq $0x0, 0x38(%rsp)
je 0x27308
movq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rcx
addq $-0x24, %rcx
xorl %eax, %eax
cmpq 0x61b4ec0(%rip), %rcx   # 0x61dbff0
movb %al, 0x7(%rsp)
jb 0x27153
movq 0x30(%rsp), %rdi
addq $-0x24, %rdi
movq 0x40(%rsp), %rsi
callq 0x27020
cmpl $0x0, %eax
sete %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x2715d
jmp 0x2716d
movq 0x30(%rsp), %rax
addq $-0x24, %rax
movq %rax, 0x30(%rsp)
jmp 0x2711e
jmp 0x2716f
movq 0x28(%rsp), %rcx
addq $0x24, %rcx
movq 0x61b4e71(%rip), %rdx   # 0x61dbff0
movslq 0x61b4e72(%rip), %rax   # 0x61dbff8
imulq $0x24, %rax, %rax
addq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, 0x6(%rsp)
jae 0x271b5
movq 0x28(%rsp), %rdi
addq $0x24, %rdi
movq 0x40(%rsp), %rsi
callq 0x27020
cmpl $0x0, %eax
sete %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %al
testb $0x1, %al
jne 0x271bf
jmp 0x271cf
movq 0x28(%rsp), %rax
addq $0x24, %rax
movq %rax, 0x28(%rsp)
jmp 0x2716f
movq $0x0, 0x20(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq 0x28(%rsp), %rcx
addq $0x24, %rcx
cmpq %rcx, %rax
jae 0x272fe
movb $0x0, 0x1f(%rsp)
cmpq $0x0, 0x20(%rsp)
jne 0x27210
movb $0x1, 0x1f(%rsp)
jmp 0x272d8
movq 0x38(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0x20(%rcx), %eax
je 0x272ad
movq 0x38(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b45aa(%rip), %rax   # 0x61db7e0
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b4591(%rip), %rax   # 0x61db7e0
movq (%rax,%rcx,8), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movb 0x1000(%rax), %al
andb $0x1, %al
movzbl %al, %eax
movq 0x8(%rsp), %rcx
movb 0x1000(%rcx), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
je 0x27292
movq 0x10(%rsp), %rax
movb 0x1000(%rax), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x1f(%rsp)
jmp 0x272ab
movq 0x38(%rsp), %rax
movl 0x20(%rax), %eax
movq 0x20(%rsp), %rcx
cmpl 0x20(%rcx), %eax
setl %al
andb $0x1, %al
movb %al, 0x1f(%rsp)
jmp 0x272d6
movq 0x38(%rsp), %rax
movl (%rax), %eax
movq 0x20(%rsp), %rcx
cmpl (%rcx), %eax
je 0x272d4
movq 0x38(%rsp), %rax
movl (%rax), %eax
movq 0x20(%rsp), %rcx
cmpl (%rcx), %eax
setl %al
andb $0x1, %al
movb %al, 0x1f(%rsp)
jmp 0x272d6
jmp 0x272d8
testb $0x1, 0x1f(%rsp)
je 0x272e9
movq 0x38(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x272eb
movq 0x38(%rsp), %rax
addq $0x24, %rax
movq %rax, 0x38(%rsp)
jmp 0x271e2
movq 0x20(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
addq $0x48, %rsp
retq
nopw %cs:(%rax,%rax)