// File: FreeSlave[P]vhlt/hlcsg/textures.cpp
// Function: LoadLump(lumpinfo_t const*, unsigned char*, int*, int, unsigned char*&, int&)
int             LoadLump(const lumpinfo_t* const source, byte* dest, int* texsize
#ifdef HLCSG_FILEREADFAILURE_FIX
						, int dest_maxsize
#endif
#ifdef ZHLT_NOWADDIR
						, byte *&writewad_data, int &writewad_datasize
#endif
						)
{
#ifdef ZHLT_NOWADDIR
	writewad_data = NULL;
	writewad_datasize = -1;
#endif
    //Log("** PnFNFUNC: LoadLump\n");

    *texsize = 0;
    if (source->filepos)
    {
        if (fseek(texfiles[source->iTexFile], source->filepos, SEEK_SET))
        {
            Warning("fseek to %d failed\n", source->filepos);
#ifdef HLCSG_FILEREADFAILURE_FIX
			Error ("File read failure");
#endif
        }
        *texsize = source->disksize;

#ifdef HLCSG_AUTOWAD_NEW
		if (texwadpathes[source->iTexFile]->usedbymap)
#else
        bool wadinclude = false;
        std::map< int, bool >::iterator it;
        it = s_WadIncludeMap.find(source->iTexFile);
        if (it != s_WadIncludeMap.end())
        {
            wadinclude = it->second;
        }

        // Should we just load the texture header w/o the palette & bitmap?
        if (g_wadtextures && !wadinclude)
#endif
        {
            // Just read the miptex header and zero out the data offsets.
            // We will load the entire texture from the WAD at engine runtime
            int             i;
            miptex_t*       miptex = (miptex_t*)dest;
#ifdef HLCSG_FILEREADFAILURE_FIX
			hlassume ((int)sizeof (miptex_t) <= dest_maxsize, assume_MAX_MAP_MIPTEX);
#endif
            SafeRead(texfiles[source->iTexFile], dest, sizeof(miptex_t));

            for (i = 0; i < MIPLEVELS; i++)
                miptex->offsets[i] = 0;
#ifdef ZHLT_NOWADDIR
			writewad_data = (byte *)malloc (source->disksize);
			hlassume (writewad_data != NULL, assume_NoMemory);
			if (fseek (texfiles[source->iTexFile], source->filepos, SEEK_SET))
				Error ("File read failure");
			SafeRead (texfiles[source->iTexFile], writewad_data, source->disksize);
			writewad_datasize = source->disksize;
#endif
            return sizeof(miptex_t);
        }
        else
        {
			Developer(DEVELOPER_LEVEL_MESSAGE,"Including texture %s\n",source->name);
            // Load the entire texture here so the BSP contains the texture
#ifdef HLCSG_FILEREADFAILURE_FIX
			hlassume (source->disksize <= dest_maxsize, assume_MAX_MAP_MIPTEX);
#endif
            SafeRead(texfiles[source->iTexFile], dest, source->disksize);
            return source->disksize;
        }
    }

#ifdef HLCSG_ERROR_MISSINGTEXTURE
	Error("::LoadLump() texture %s not found!", source->name);
#else
    Warning("::LoadLump() texture %s not found!", source->name);
#endif
    return 0;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movl %ecx, 0x24(%rsp)
movq %r8, 0x18(%rsp)
movq %r9, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq $0x0, (%rax)
movq 0x10(%rsp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rax
movl $0x0, (%rax)
movq 0x38(%rsp), %rax
cmpl $0x0, (%rax)
je 0x27580
movq 0x38(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b486f(%rip), %rax   # 0x61dbbf0
movq (%rax,%rcx,8), %rdi
movq 0x38(%rsp), %rax
movslq (%rax), %rsi
xorl %edx, %edx
callq 0x4440
cmpl $0x0, %eax
je 0x273bc
movq 0x38(%rsp), %rax
movl (%rax), %esi
leaq 0x8afc(%rip), %rdi      # 0x2fea3
movb $0x0, %al
callq 0xbef0
leaq 0x4564(%rip), %rdi      # 0x2b919
movb $0x0, %al
callq 0xba80
movq 0x38(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x28(%rsp), %rax
movl %ecx, (%rax)
movq 0x38(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b4405(%rip), %rax   # 0x61db7e0
movq (%rax,%rcx,8), %rax
testb $0x1, 0x1000(%rax)
je 0x27511
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movl $0x28, %eax
cmpl 0x24(%rsp), %eax
setle %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x25, %esi
callq 0xc400
movq 0x38(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b47cf(%rip), %rax   # 0x61dbbf0
movq (%rax,%rcx,8), %rdi
movq 0x30(%rsp), %rsi
movl $0x28, %edx
callq 0xb0e0
movl $0x0, 0xc(%rsp)
cmpl $0x4, 0xc(%rsp)
jge 0x27461
movq (%rsp), %rax
movslq 0xc(%rsp), %rcx
movl $0x0, 0x18(%rax,%rcx,4)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x2743c
movq 0x38(%rsp), %rax
movslq 0x4(%rax), %rdi
callq 0x4500
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq %rcx, (%rax)
movq 0x18(%rsp), %rax
cmpq $0x0, (%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xc400
movq 0x38(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b474a(%rip), %rax   # 0x61dbbf0
movq (%rax,%rcx,8), %rdi
movq 0x38(%rsp), %rax
movslq (%rax), %rsi
xorl %edx, %edx
callq 0x4440
cmpl $0x0, %eax
je 0x274cc
leaq 0x4454(%rip), %rdi      # 0x2b919
movb $0x0, %al
callq 0xba80
movq 0x38(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b4714(%rip), %rax   # 0x61dbbf0
movq (%rax,%rcx,8), %rdi
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edx
callq 0xb0e0
movq 0x38(%rsp), %rax
movl 0x4(%rax), %ecx
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
movl $0x28, 0x44(%rsp)
jmp 0x2759f
movq 0x38(%rsp), %rdx
addq $0x10, %rdx
movl $0x3, %edi
leaq 0x8991(%rip), %rsi      # 0x2feb7
movb $0x0, %al
callq 0xc0f0
movq 0x38(%rsp), %rax
movl 0x4(%rax), %eax
cmpl 0x24(%rsp), %eax
setle %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x25, %esi
callq 0xc400
movq 0x38(%rsp), %rax
movslq 0x20(%rax), %rcx
leaq 0x61b4694(%rip), %rax   # 0x61dbbf0
movq (%rax,%rcx,8), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rax
movl 0x4(%rax), %edx
callq 0xb0e0
movq 0x38(%rsp), %rax
movl 0x4(%rax), %eax
movl %eax, 0x44(%rsp)
jmp 0x2759f
movq 0x38(%rsp), %rsi
addq $0x10, %rsi
leaq 0x893d(%rip), %rdi      # 0x2fecd
movb $0x0, %al
callq 0xba80
movl $0x0, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax,%rax)