// File: /workspace/github_cpp_makefile_star2_O1/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 0x42b0
movb $0x0, 0xf(%rbx)
movl 0x10(%rbx), %edx
testl %edx, %edx
jle 0x22acd
movl 0x14(%rbx), %eax
testl %eax, %eax
jle 0x22acd
orl %edx, %eax
andl $0xf, %eax
je 0x22ae7
movl 0x14(%rbx), %ecx
leaq 0x19cb1(%rip), %rdi     # 0x3c788
movq %rbx, %rsi
movl $0x10, %r8d
xorl %eax, %eax
callq 0xa01c
leaq 0x19cd1(%rip), %r15     # 0x3c7bf
xorl %r12d, %r12d
xorl %r13d, %r13d
leal 0x28(%r13), %eax
cmpl %eax, 0x18(%r14,%r12,4)
je 0x22b0c
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa01c
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 0x22af4
leal 0x32a(%r13), %eax
cmpl %ebp, %eax
jle 0x22b44
leaq 0x19cad(%rip), %rdi     # 0x3c7e7
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa01c
movslq %r13d, %r15
cmpw $0x100, 0x28(%r14,%r15) # imm = 0x100
je 0x22b62
leaq 0x19cad(%rip), %rdi     # 0x3c805
movq %rbx, %rsi
xorl %eax, %eax
callq 0xa01c
addq %r14, %r15
movslq 0x10(%rbx), %rax
movslq 0x14(%rbx), %rdi
imulq %rax, %rdi
callq 0x4410
movq %rax, 0x18(%rbx)
xorl %edi, %edi
testq %rax, %rax
setne %dil
movl $0x1, %esi
callq 0xa7c9
cmpl $0x0, 0x14(%rbx)
jle 0x22bcb
addq $0x28, %r14
xorl %eax, %eax
movl 0x10(%rbx), %edx
testl %edx, %edx
jle 0x22bc4
xorl %ecx, %ecx
imull %eax, %edx
movslq %edx, %rdx
leaq (%r14,%rdx), %rsi
movb (%rcx,%rsi), %sil
addq 0x18(%rbx), %rdx
movb %sil, (%rcx,%rdx)
incq %rcx
movslq 0x10(%rbx), %rdx
cmpq %rdx, %rcx
jl 0x22ba2
incl %eax
cmpl 0x14(%rbx), %eax
jl 0x22b99
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 0x22bd7
incq %rax
addq $0x3, %r15
addq $0x3, %rbx
cmpq $0x100, %rax            # imm = 0x100
jne 0x22bd5
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq