// File: /workspace/github_cpp_makefile_star2_O2/FreeSlave[P]vhlt/hlrad/loadtextures.cpp
// Function: LoadTexture(radtexture_t*, miptex_s const*, int)
void LoadTexture (radtexture_t *tex, const miptex_t *mt, int size)
{
	int i, j;
	const miptex_t *header = mt;
	const byte *data = (const byte *)mt;
	tex->width = header->width;
	tex->height = header->height;
	strcpy (tex->name, header->name);
	tex->name[16 - 1] = '\0';
	if (tex->width <= 0 || tex->height <= 0 ||
		tex->width % (2 * 1 << (MIPLEVELS - 1)) != 0 || tex->height % (2 * (1 << (MIPLEVELS - 1))) != 0)
	{
		Error ("Texture '%s': dimension (%dx%d) is not multiple of %d.", tex->name, tex->width, tex->height, 2 * (1 << (MIPLEVELS - 1)));
	}
	int mipsize;
	for (mipsize = 0, i = 0; i < MIPLEVELS; i++)
	{
		if ((int)mt->offsets[i] != (int)sizeof (miptex_t) + mipsize)
		{
			Error ("Texture '%s': unexpected miptex offset.", tex->name);
		}
		mipsize += (tex->width >> i) * (tex->height >> i);
	}
	if (size < (int)sizeof (miptex_t) + mipsize + 2 + 256 * 3)
	{
		Error ("Texture '%s': no enough data.", tex->name);
	}
	if (*(unsigned short *)&data[sizeof (miptex_t) + mipsize] != 256)
	{
		Error ("Texture '%s': palette size is not 256.", tex->name);
	}
	tex->canvas = (byte *)malloc (tex->width * tex->height);
	hlassume (tex->canvas != NULL, assume_NoMemory);
	for (i = 0; i < tex->height; i++)
	{
		for (j = 0; j < tex->width; j++)
		{
			tex->canvas[i * tex->width + j] = data[sizeof (miptex_t) + i * tex->width + j];
		}
	}
	for (i = 0; i < 256; i++)
	{
		for (j = 0; j < 3; j++)
		{
			tex->palette[i][j] = data[sizeof (miptex_t) + mipsize + 2 + i * 3 + j];
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movl 0x10(%rsi), %eax
movl %eax, 0x10(%rdi)
movl 0x14(%rsi), %eax
movl %eax, 0x14(%rdi)
callq 0x42a0
movb $0x0, 0xf(%rbx)
movl 0x10(%rbx), %edx
movl 0x14(%rbx), %ecx
testl %edx, %edx
jle 0x1eec2
testl %ecx, %ecx
jle 0x1eec2
movl %ecx, %eax
orl %edx, %eax
andl $0xf, %eax
je 0x1eed7
leaq 0x177c7(%rip), %rdi     # 0x36690
pushq $0x10
popq %r8
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8b2f
leaq 0x177e9(%rip), %r15     # 0x366c7
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq $0x4, %r12
je 0x1ef1d
leal 0x28(%r13), %eax
cmpl %eax, 0x18(%r14,%r12,4)
je 0x1ef02
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8b2f
movl 0x10(%rbx), %eax
movl 0x14(%rbx), %edx
movl %r12d, %ecx
sarl %cl, %eax
sarl %cl, %edx
imull %eax, %edx
addl %r13d, %edx
incq %r12
movl %edx, %r13d
jmp 0x1eee4
leal 0x32a(%r13), %eax
cmpl %ebp, %eax
jle 0x1ef39
leaq 0x177c0(%rip), %rdi     # 0x366ef
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8b2f
movslq %r13d, %r15
cmpw $0x100, 0x28(%r14,%r15) # imm = 0x100
je 0x1ef57
leaq 0x177c0(%rip), %rdi     # 0x3670d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x8b2f
addq %r14, %r15
movslq 0x10(%rbx), %rax
movslq 0x14(%rbx), %rdi
imulq %rax, %rdi
callq 0x4420
movq %rax, 0x18(%rbx)
xorl %edi, %edi
testq %rax, %rax
setne %dil
pushq $0x1
popq %rsi
callq 0x92cb
addq $0x28, %r14
xorl %eax, %eax
cmpl 0x14(%rbx), %eax
jge 0x1efba
xorl %ecx, %ecx
movslq 0x10(%rbx), %rdx
cmpq %rdx, %rcx
jge 0x1efb6
imull %eax, %edx
movslq %edx, %rdx
leaq (%r14,%rdx), %rsi
movb (%rcx,%rsi), %sil
movq 0x18(%rbx), %rdi
addl %ecx, %edx
movslq %edx, %rdx
movb %sil, (%rdi,%rdx)
incq %rcx
jmp 0x1ef8d
incl %eax
jmp 0x1ef86
addq $0x2a, %r15
addq $0x20, %rbx
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x1efed
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x1efe0
movb (%r15,%rcx), %dl
movb %dl, (%rbx,%rcx)
incq %rcx
jmp 0x1efce
incq %rax
addq $0x3, %r15
addq $0x3, %rbx
jmp 0x1efc4
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq