// File: /workspace/github_cpp_makefile_star2_O1/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 0x61b42de(%rip), %rsi   # 0x61cdf60
movslq 0x61b42df(%rip), %rdx   # 0x61cdf68
leaq -0x2e(%rip), %r8        # 0x19c62
movl $0x24, %ecx
callq 0x4400
testq %rax, %rax
je 0x19d6b
movq %rax, %r14
movq 0x61b42b3(%rip), %r12   # 0x61cdf60
addq $0x10, %rbx
movq %rax, %r13
movq %r13, %r15
addq $-0x24, %r13
cmpq %r12, %r13
jb 0x19cd0
leaq -0x14(%r15), %rdi
movq %rbx, %rsi
callq 0x4450
testl %eax, %eax
je 0x19cb4
movslq 0x61b4291(%rip), %rax   # 0x61cdf68
leaq (%rax,%rax,8), %rax
leaq (%r12,%rax,4), %r13
movq %r14, %r12
addq $0x24, %r14
cmpq %r13, %r14
jae 0x19cfc
leaq 0x34(%r12), %rdi
movq %rbx, %rsi
callq 0x4450
testl %eax, %eax
je 0x19cdf
leaq 0x24(%r12), %rax
cmpq %rax, %r15
jae 0x19da6
xorl %ecx, %ecx
leaq 0x61b3a3d(%rip), %rax   # 0x61cd750
testq %rcx, %rcx
je 0x19d2b
movslq 0x20(%r15), %rsi
movslq 0x20(%rcx), %rdi
cmpl %edi, %esi
jne 0x19d2f
movl (%r15), %edx
cmpl (%rcx), %edx
jmp 0x19d48
movb $0x1, %dl
jmp 0x19d50
movq (%rax,%rsi,8), %rdx
movq (%rax,%rdi,8), %r8
movb 0x1000(%rdx), %dl
cmpb 0x1000(%r8), %dl
jne 0x19d4d
cmpl %edi, %esi
setl %dl
jmp 0x19d50
xorb $0x1, %dl
movq %r15, %rbx
testb %dl, %dl
jne 0x19d5a
movq %rcx, %rbx
leaq 0x24(%r15), %rdx
movq %rbx, %rcx
cmpq %r12, %r15
movq %rdx, %r15
jb 0x19d13
jmp 0x19dba
addq $0x10, %rbx
leaq 0x7ff7(%rip), %rdi      # 0x21d6d
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa612
leaq 0x5ef2(%rip), %rsi      # 0x1fc79
movq %rbx, %rdi
callq 0x4450
testl %eax, %eax
je 0x19daa
leaq 0x6524(%rip), %rsi      # 0x202be
movq %rbx, %rdi
callq 0x4450
testl %eax, %eax
je 0x19daa
xorl %ebx, %ebx
jmp 0x19dba
leaq 0x7fe2(%rip), %rdi      # 0x21d93
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x9fb2
movq %rbx, %rax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq