// File: /workspace/github_cpp_makefile_star2_O2/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 0x61b4f91(%rip), %rsi   # 0x61cbf60
movslq 0x61b4f92(%rip), %rdx   # 0x61cbf68
leaq -0x2e(%rip), %r8        # 0x16faf
pushq $0x24
popq %rcx
callq 0x4410
testq %rax, %rax
je 0x170a1
movq %rax, %r14
movq 0x61b4f68(%rip), %r13   # 0x61cbf60
addq $0x10, %rbx
movq %rax, %r12
movq %r12, %r15
addq $-0x24, %r12
cmpq %r13, %r12
jb 0x1701b
leaq -0x14(%r15), %rdi
movq %rbx, %rsi
callq 0x4480
testl %eax, %eax
je 0x16fff
movslq 0x61b4f46(%rip), %rax   # 0x61cbf68
imulq $0x24, %rax, %rax
addq %rax, %r13
addq $0x34, %r14
leaq -0x10(%r14), %r12
cmpq %r13, %r12
jae 0x17049
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4480
addq $0x24, %r14
testl %eax, %eax
je 0x1702d
xorl %ebx, %ebx
leaq 0x61b46fe(%rip), %rax   # 0x61cb750
cmpq %r12, %r15
jae 0x170f0
testq %rbx, %rbx
je 0x17073
movslq 0x20(%r15), %rcx
movslq 0x20(%rbx), %rdx
cmpl %edx, %ecx
jne 0x1707c
movl (%r15), %ecx
cmpl (%rbx), %ecx
jge 0x17076
movq %r15, %rbx
addq $0x24, %r15
jmp 0x17052
movq (%rax,%rcx,8), %rsi
movq (%rax,%rdx,8), %rdi
movb 0x1000(%rsi), %sil
cmpb 0x1000(%rdi), %sil
jne 0x1709a
cmpl %edx, %ecx
jl 0x17073
jmp 0x17076
testb %sil, %sil
je 0x17073
jmp 0x17076
addq $0x10, %rbx
leaq 0x7bed(%rip), %rdi      # 0x1ec99
movq %rbx, %rsi
xorl %eax, %eax
callq 0x90f1
leaq 0x5b4c(%rip), %rsi      # 0x1cc09
movq %rbx, %rdi
callq 0x4480
testl %eax, %eax
je 0x170e0
leaq 0x613e(%rip), %rsi      # 0x1d20e
movq %rbx, %rdi
callq 0x4480
testl %eax, %eax
je 0x170e0
xorl %ebx, %ebx
jmp 0x170f0
leaq 0x7bd8(%rip), %rdi      # 0x1ecbf
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x89dc
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq