// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r12
movq %rdi, %rbx
movq $0x0, (%r8)
movl $0xffffffff, (%r9)      # imm = 0xFFFFFFFF
movl $0x0, (%rdx)
movslq (%rdi), %rsi
testq %rsi, %rsi
je 0x19efc
movq %r9, %r14
movq %r8, %r15
movl %ecx, %ebp
movq %rdx, %r13
movslq 0x20(%rbx), %rax
leaq 0x61b3d52(%rip), %rcx   # 0x61cdb60
movq (%rcx,%rax,8), %rdi
xorl %edx, %edx
callq 0x4360
testl %eax, %eax
je 0x19e3b
movl (%rbx), %esi
leaq 0x7fa7(%rip), %rdi      # 0x21dcd
xorl %eax, %eax
callq 0xa612
leaq 0x3b95(%rip), %rdi      # 0x1d9c9
xorl %eax, %eax
callq 0xa2a0
movl 0x4(%rbx), %eax
movl %eax, (%r13)
movslq 0x20(%rbx), %rax
leaq 0x61b3903(%rip), %rcx   # 0x61cd750
movq (%rcx,%rax,8), %rax
cmpb $0x1, 0x1000(%rax)
jne 0x19f15
xorl %edi, %edi
cmpl $0x28, %ebp
setge %dil
movl $0x25, %esi
callq 0xaa4d
movslq 0x20(%rbx), %rax
leaq 0x61b3ce4(%rip), %r13   # 0x61cdb60
movq (%r13,%rax,8), %rdi
movl $0x28, %ebp
movq %r12, %rsi
movl $0x28, %edx
callq 0x9b8b
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%r12)
movslq 0x4(%rbx), %rdi
callq 0x4430
movq %rax, (%r15)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xaa4d
movslq 0x20(%rbx), %rax
movq (%r13,%rax,8), %rdi
movslq (%rbx), %rsi
xorl %edx, %edx
callq 0x4360
testl %eax, %eax
je 0x19ee0
leaq 0x3af0(%rip), %rdi      # 0x1d9c9
xorl %eax, %eax
callq 0xa2a0
movslq 0x20(%rbx), %rax
movq (%r13,%rax,8), %rdi
movq (%r15), %rsi
movl 0x4(%rbx), %edx
callq 0x9b8b
movl 0x4(%rbx), %eax
movl %eax, (%r14)
jmp 0x19f5c
addq $0x10, %rbx
leaq 0x7ef0(%rip), %rdi      # 0x21df7
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa2a0
jmp 0x19f5c
leaq 0x10(%rbx), %rdx
leaq 0x7ec1(%rip), %rsi      # 0x21de1
movl $0x3, %edi
xorl %eax, %eax
callq 0xa7b2
xorl %edi, %edi
cmpl %ebp, 0x4(%rbx)
setle %dil
movl $0x25, %esi
callq 0xaa4d
movslq 0x20(%rbx), %rax
leaq 0x61b3c16(%rip), %rcx   # 0x61cdb60
movq (%rcx,%rax,8), %rdi
movl 0x4(%rbx), %edx
movq %r12, %rsi
callq 0x9b8b
movl 0x4(%rbx), %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq