// File: /workspace/github_cpp_makefile_star2_O3/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 0x21e47
testl %ecx, %ecx
jle 0x21e47
movl %ecx, %eax
orl %edx, %eax
andl $0xf, %eax
je 0x21e5e
leaq 0x1987a(%rip), %rdi     # 0x3b6c8
movq %rbx, %rsi
movl $0x10, %r8d
xorl %eax, %eax
callq 0x9f34
leaq 0x1989a(%rip), %r15     # 0x3b6ff
xorl %r12d, %r12d
xorl %r13d, %r13d
leal 0x28(%r13), %eax
cmpl %eax, 0x18(%r14,%r12,4)
je 0x21e83
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x9f34
movl 0x10(%rbx), %eax
movl 0x14(%rbx), %edx
movl %r12d, %ecx
sarl %cl, %eax
sarl %cl, %edx
imull %eax, %edx
addl %edx, %r13d
incq %r12
cmpq $0x4, %r12
jne 0x21e6b
leal 0x32a(%r13), %eax
cmpl %ebp, %eax
jle 0x21ebb
leaq 0x19876(%rip), %rdi     # 0x3b727
movq %rbx, %rsi
xorl %eax, %eax
callq 0x9f34
movslq %r13d, %r15
cmpw $0x100, 0x28(%r14,%r15) # imm = 0x100
je 0x21ed9
leaq 0x19876(%rip), %rdi     # 0x3b745
movq %rbx, %rsi
xorl %eax, %eax
callq 0x9f34
addq %r14, %r15
movslq 0x10(%rbx), %rax
movslq 0x14(%rbx), %rdi
imulq %rax, %rdi
callq 0x4400
movq %rax, 0x18(%rbx)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa6ed
movl 0x14(%rbx), %edx
testl %edx, %edx
jle 0x21f45
addq $0x28, %r14
movl 0x10(%rbx), %ecx
xorl %eax, %eax
testl %ecx, %ecx
jle 0x21f3f
xorl %edx, %edx
imull %eax, %ecx
movslq %ecx, %rcx
leaq (%r14,%rcx), %rsi
movb (%rdx,%rsi), %sil
addq 0x18(%rbx), %rcx
movb %sil, (%rdx,%rcx)
incq %rdx
movslq 0x10(%rbx), %rcx
cmpq %rcx, %rdx
jl 0x21f1a
movl 0x14(%rbx), %edx
incl %eax
cmpl %edx, %eax
jl 0x21f14
addq $0x2a, %r15
addq $0x20, %rbx
xorl %eax, %eax
xorl %ecx, %ecx
movb (%r15,%rcx), %dl
movb %dl, (%rbx,%rcx)
incq %rcx
cmpq $0x3, %rcx
jne 0x21f51
incq %rax
addq $0x3, %r15
addq $0x3, %rbx
cmpq $0x100, %rax            # imm = 0x100
jne 0x21f4f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq