// File: FreeSlave[P]vhlt/common/bspfile.cpp
// Function: ParseImplicitTexinfoFromTexture(int)
int	ParseImplicitTexinfoFromTexture (int miptex)
{
	int texinfo;
	int numtextures = g_texdatasize? ((dmiptexlump_t *)g_dtexdata)->nummiptex: 0;
	int offset;
	int size;
	miptex_t *mt;
	char name[16];
	
	if (miptex < 0 || miptex >= numtextures)
	{
		Warning ("ParseImplicitTexinfoFromTexture: internal error: invalid texture number %d.", miptex);
		return -1;
	}
	offset = ((dmiptexlump_t *)g_dtexdata)->dataofs[miptex];
	size = g_texdatasize - offset;
	if (offset < 0 || g_dtexdata + offset < (byte *)&((dmiptexlump_t *)g_dtexdata)->dataofs[numtextures] ||
		size < (int)sizeof (miptex_t))
	{
		return -1;
	}

	mt = (miptex_t *)&g_dtexdata[offset];
	safe_strncpy (name, mt->name, 16);
	
	if (!(strlen (name) >= 6 && !strncasecmp (&name[1], "_rad", 4) && '0' <= name[5] && name[5] <= '9'))
	{
		return -1;
	}

	texinfo = atoi (&name[5]);
	if (texinfo < 0 || texinfo >= g_numtexinfo)
	{
		Warning ("Invalid index of original texinfo: %d parsed from texture name '%s'.", texinfo, name);
		return -1;
	}

	return texinfo;
}

// Assembly:
subq $0x48, %rsp
movl %edi, 0x40(%rsp)
cmpl $0x0, 0x837f85(%rip)    # 0x83f514
je 0x75a0
movq 0x837f80(%rip), %rax    # 0x83f518
movl (%rax), %eax
movl %eax, 0xc(%rsp)
jmp 0x75a8
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x75a8
movl 0xc(%rsp), %eax
movl %eax, 0x38(%rsp)
cmpl $0x0, 0x40(%rsp)
jl 0x75c1
movl 0x40(%rsp), %eax
cmpl 0x38(%rsp), %eax
jl 0x75e0
movl 0x40(%rsp), %esi
leaq 0x22ddd(%rip), %rdi     # 0x2a3a9
movb $0x0, %al
callq 0xbef0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x770f
movq 0x837f31(%rip), %rax    # 0x83f518
movslq 0x40(%rsp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, 0x34(%rsp)
movl 0x837f1a(%rip), %eax    # 0x83f514
subl 0x34(%rsp), %eax
movl %eax, 0x30(%rsp)
cmpl $0x0, 0x34(%rsp)
jl 0x763b
movq 0x837f08(%rip), %rax    # 0x83f518
movslq 0x34(%rsp), %rcx
addq %rcx, %rax
movq 0x837ef9(%rip), %rcx    # 0x83f518
addq $0x4, %rcx
movslq 0x38(%rsp), %rdx
shlq $0x2, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
jb 0x763b
cmpl $0x28, 0x30(%rsp)
jge 0x7648
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x770f
movq 0x837ec9(%rip), %rax    # 0x83f518
movslq 0x34(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x28(%rsp)
leaq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl $0x10, %edx
callq 0x9af0
leaq 0x10(%rsp), %rdi
callq 0x41c0
cmpq $0x6, %rax
jb 0x76b7
leaq 0x10(%rsp), %rdi
addq $0x1, %rdi
leaq 0x22d65(%rip), %rsi     # 0x2a3f5
movl $0x4, %edx
callq 0x44e0
cmpl $0x0, %eax
jne 0x76b7
movsbl 0x15(%rsp), %ecx
movl $0x30, %eax
cmpl %ecx, %eax
jg 0x76b7
movsbl 0x15(%rsp), %eax
cmpl $0x39, %eax
jle 0x76c1
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x770f
leaq 0x10(%rsp), %rdi
addq $0x5, %rdi
callq 0x45d0
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jl 0x76e6
movl 0x3c(%rsp), %eax
cmpl 0x1197d88(%rip), %eax   # 0x119f46c
jl 0x7707
movl 0x3c(%rsp), %esi
leaq 0x10(%rsp), %rdx
leaq 0x22d04(%rip), %rdi     # 0x2a3fa
movb $0x0, %al
callq 0xbef0
movl $0xffffffff, 0x44(%rsp) # imm = 0xFFFFFFFF
jmp 0x770f
movl 0x3c(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax,%rax)