// File: /workspace/github_cpp_makefile_star2_O2/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
andq $0x0, (%r8)
orl $-0x1, (%r9)
andl $0x0, (%rdx)
movq %rsi, %r12
movq %rdi, %rbx
movslq (%rdi), %rsi
testq %rsi, %rsi
je 0x171c8
movq %r9, %r14
movq %r8, %r15
movl %ecx, %ebp
movq %rdx, %r13
movslq 0x20(%rbx), %rax
leaq 0x61b4a25(%rip), %rcx   # 0x61cbb60
movq (%rcx,%rax,8), %rdi
xorl %edx, %edx
callq 0x4370
testl %eax, %eax
je 0x17168
movl (%rbx), %esi
leaq 0x7ba6(%rip), %rdi      # 0x1ecf9
xorl %eax, %eax
callq 0x90f1
leaq 0x37c8(%rip), %rdi      # 0x1a929
xorl %eax, %eax
callq 0x8d7b
movl 0x4(%rbx), %eax
movl %eax, (%r13)
movslq 0x20(%rbx), %rax
leaq 0x61b45d6(%rip), %rcx   # 0x61cb750
movq (%rcx,%rax,8), %rax
cmpb $0x1, 0x1000(%rax)
jne 0x171e4
xorl %edi, %edi
cmpl $0x28, %ebp
setge %dil
pushq $0x25
popq %rsi
callq 0x9517
movslq 0x20(%rbx), %rax
leaq 0x61b49bd(%rip), %r13   # 0x61cbb60
movq (%r13,%rax,8), %rdi
pushq $0x28
popq %rbp
movq %r12, %rsi
movl %ebp, %edx
callq 0x85bb
xorl %eax, %eax
cmpq $0x4, %rax
je 0x17229
andl $0x0, 0x18(%r12,%rax,4)
incq %rax
jmp 0x171b7
addq $0x10, %rbx
leaq 0x7b50(%rip), %rdi      # 0x1ed23
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8d7b
jmp 0x17285
leaq 0x10(%rbx), %rdx
leaq 0x7b1e(%rip), %rsi      # 0x1ed0d
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x9291
xorl %edi, %edi
cmpl %ebp, 0x4(%rbx)
setle %dil
pushq $0x25
popq %rsi
callq 0x9517
movslq 0x20(%rbx), %rax
leaq 0x61b494b(%rip), %rcx   # 0x61cbb60
movq (%rcx,%rax,8), %rdi
movl 0x4(%rbx), %edx
movq %r12, %rsi
callq 0x85bb
movl 0x4(%rbx), %ebp
jmp 0x17285
movslq 0x4(%rbx), %rdi
callq 0x4450
movq %rax, (%r15)
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x9517
movslq 0x20(%rbx), %rax
movq (%r13,%rax,8), %rdi
movslq (%rbx), %rsi
xorl %edx, %edx
callq 0x4370
testl %eax, %eax
je 0x1726b
leaq 0x36c5(%rip), %rdi      # 0x1a929
xorl %eax, %eax
callq 0x8d7b
movslq 0x20(%rbx), %rax
movq (%r13,%rax,8), %rdi
movq (%r15), %rsi
movl 0x4(%rbx), %edx
callq 0x85bb
movl 0x4(%rbx), %eax
movl %eax, (%r14)
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq