// File: 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:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movl %edx, 0x24(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rax
movl 0x10(%rax), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x10(%rsp), %rax
movl 0x14(%rax), %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x30(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x42e0
movq 0x30(%rsp), %rax
movb $0x0, 0xf(%rax)
movq 0x30(%rsp), %rax
cmpl $0x0, 0x10(%rax)
jle 0x44bde
movq 0x30(%rsp), %rax
cmpl $0x0, 0x14(%rax)
jle 0x44bde
movq 0x30(%rsp), %rax
movl 0x10(%rax), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x44bde
movq 0x30(%rsp), %rax
movl 0x14(%rax), %eax
movl $0x10, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
je 0x44c07
movq 0x30(%rsp), %rsi
movq 0x30(%rsp), %rax
movl 0x10(%rax), %edx
movq 0x30(%rsp), %rax
movl 0x14(%rax), %ecx
leaq 0x29aac(%rip), %rdi     # 0x6e6a6
movl $0x10, %r8d
movb $0x0, %al
callq 0xb580
movl $0x0, 0x4(%rsp)
movl $0x0, 0x20(%rsp)
cmpl $0x4, 0x20(%rsp)
jge 0x44c80
movq 0x28(%rsp), %rax
movslq 0x20(%rsp), %rcx
movl 0x18(%rax,%rcx,4), %eax
movl 0x4(%rsp), %ecx
addl $0x28, %ecx
cmpl %ecx, %eax
je 0x44c4a
movq 0x30(%rsp), %rsi
leaq 0x29a9a(%rip), %rdi     # 0x6e6dd
movb $0x0, %al
callq 0xb580
movq 0x30(%rsp), %rax
movl 0x10(%rax), %eax
movl 0x20(%rsp), %ecx
sarl %cl, %eax
movq 0x30(%rsp), %rcx
movl 0x14(%rcx), %edx
movl 0x20(%rsp), %ecx
sarl %cl, %edx
movl %edx, %ecx
imull %ecx, %eax
addl 0x4(%rsp), %eax
movl %eax, 0x4(%rsp)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x44c17
movl 0x24(%rsp), %eax
movl 0x4(%rsp), %ecx
addl $0x28, %ecx
addl $0x2, %ecx
addl $0x300, %ecx            # imm = 0x300
cmpl %ecx, %eax
jge 0x44cab
movq 0x30(%rsp), %rsi
leaq 0x29a61(%rip), %rdi     # 0x6e705
movb $0x0, %al
callq 0xb580
movq 0x8(%rsp), %rax
movslq 0x4(%rsp), %rcx
addq $0x28, %rcx
movzwl (%rax,%rcx), %eax
cmpl $0x100, %eax            # imm = 0x100
je 0x44cd7
movq 0x30(%rsp), %rsi
leaq 0x29a53(%rip), %rdi     # 0x6e723
movb $0x0, %al
callq 0xb580
movq 0x30(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x30(%rsp), %rcx
imull 0x14(%rcx), %eax
movslq %eax, %rdi
callq 0x4430
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x18(%rax)
movq 0x30(%rsp), %rax
cmpq $0x0, 0x18(%rax)
setne %al
movzbl %al, %edi
andl $0x1, %edi
movl $0x1, %esi
callq 0xbf00
movl $0x0, 0x20(%rsp)
movl 0x20(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x44da8
movl $0x0, 0x1c(%rsp)
movl 0x1c(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x44d96
movq 0x8(%rsp), %rax
movl 0x20(%rsp), %ecx
movq 0x30(%rsp), %rdx
imull 0x10(%rdx), %ecx
movslq %ecx, %rcx
addq $0x28, %rcx
movslq 0x1c(%rsp), %rdx
addq %rdx, %rcx
movb (%rax,%rcx), %dl
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x20(%rsp), %ecx
movq 0x30(%rsp), %rsi
imull 0x10(%rsi), %ecx
addl 0x1c(%rsp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x44d37
jmp 0x44d98
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x44d21
movl $0x0, 0x20(%rsp)
cmpl $0x100, 0x20(%rsp)      # imm = 0x100
jge 0x44e2a
movl $0x0, 0x1c(%rsp)
cmpl $0x3, 0x1c(%rsp)
jge 0x44e1b
movq 0x8(%rsp), %rax
movslq 0x4(%rsp), %rcx
addq $0x28, %rcx
addq $0x2, %rcx
imull $0x3, 0x20(%rsp), %edx
movslq %edx, %rdx
addq %rdx, %rcx
movslq 0x1c(%rsp), %rdx
addq %rdx, %rcx
movb (%rax,%rcx), %dl
movq 0x30(%rsp), %rax
addq $0x20, %rax
movslq 0x20(%rsp), %rcx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movslq 0x1c(%rsp), %rcx
movb %dl, (%rax,%rcx)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x44dc2
jmp 0x44e1d
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x44db0
addq $0x38, %rsp
retq
nop