// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdi, %rbx
movq 0x61b485f(%rip), %rsi   # 0x61cdf50
movslq 0x61b4860(%rip), %rdx   # 0x61cdf58
leaq -0x2e(%rip), %r8        # 0x196d1
movl $0x24, %ecx
callq 0x43f0
testq %rax, %rax
je 0x197d0
movq %rax, %r14
movq 0x61b4834(%rip), %r12   # 0x61cdf50
addq $0x10, %rbx
movq %rax, %r13
movq %r13, %r15
addq $-0x24, %r13
cmpq %r12, %r13
jb 0x1973f
leaq -0x14(%r15), %rdi
movq %rbx, %rsi
callq 0x4440
testl %eax, %eax
je 0x19723
movslq 0x61b4812(%rip), %rax   # 0x61cdf58
leaq (%rax,%rax,8), %rax
leaq (%r12,%rax,4), %r13
movq %r14, %r12
addq $0x24, %r14
cmpq %r13, %r14
jae 0x1976b
leaq 0x34(%r12), %rdi
movq %rbx, %rsi
callq 0x4440
testl %eax, %eax
je 0x1974e
leaq 0x24(%r12), %rax
cmpq %rax, %r15
jae 0x1980b
xorl %ebx, %ebx
leaq 0x61b3fbe(%rip), %rax   # 0x61cd740
testq %rbx, %rbx
je 0x1979a
movslq 0x20(%r15), %rcx
movslq 0x20(%rbx), %rdx
cmpl %edx, %ecx
jne 0x197ab
movl (%r15), %ecx
cmpl (%rbx), %ecx
jge 0x1979d
movq %r15, %rbx
leaq 0x24(%r15), %rcx
cmpq %r12, %r15
movq %rcx, %r15
jb 0x19782
jmp 0x1981f
movq (%rax,%rcx,8), %rsi
movq (%rax,%rdx,8), %rdi
movb 0x1000(%rsi), %sil
cmpb 0x1000(%rdi), %sil
jne 0x197c9
cmpl %edx, %ecx
jl 0x1979a
jmp 0x1979d
testb %sil, %sil
je 0x1979a
jmp 0x1979d
addq $0x10, %rbx
leaq 0x85ce(%rip), %rdi      # 0x21da9
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa4d6
leaq 0x64ad(%rip), %rsi      # 0x1fc99
movq %rbx, %rdi
callq 0x4440
testl %eax, %eax
je 0x1980f
leaq 0x6adf(%rip), %rsi      # 0x202de
movq %rbx, %rdi
callq 0x4440
testl %eax, %eax
je 0x1980f
xorl %ebx, %ebx
jmp 0x1981f
leaq 0x85b9(%rip), %rdi      # 0x21dcf
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x9e76
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq