// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/mdsprite.c
// Function: mdloadskin
PTMHead * mdloadskin (md2model *m, int number, int pal, int surf)
{
	int i, err = 0;
	char *skinfile = 0, fn[BMAX_PATH+65];
	PTMHead **tex = 0;
	mdskinmap_t *sk, *skzero = 0;
    PTMIdent id;

	if (m->mdnum == 2) {
		surf = 0;
	}

	if ((unsigned)pal >= (unsigned)MAXPALOOKUPS) {
		return 0;
	}

	i = -1;
	for (sk = m->skinmap; sk; sk = sk->next) {
		if ((int)sk->palette == pal && sk->skinnum == number && sk->surfnum == surf) {
			tex = &sk->tex[ hictinting[pal].f ];
			skinfile = sk->fn;
			strcpy(fn,skinfile);
			//buildprintf("Using exact match skin (pal=%d,skinnum=%d,surfnum=%d) %s\n",pal,number,surf,skinfile);
			break;
		}
		//If no match, give highest priority to number, then pal.. (Parkar's request, 02/27/2005)
		else if (((int)sk->palette ==   0) && (sk->skinnum == number) && (sk->surfnum == surf) && (i < 5)) { i = 5; skzero = sk; }
		else if (((int)sk->palette == pal) && (sk->skinnum ==      0) && (sk->surfnum == surf) && (i < 4)) { i = 4; skzero = sk; }
		else if (((int)sk->palette ==   0) && (sk->skinnum ==      0) && (sk->surfnum == surf) && (i < 3)) { i = 3; skzero = sk; }
		else if (((int)sk->palette ==   0) && (sk->skinnum == number) && (i < 2)) { i = 2; skzero = sk; }
		else if (((int)sk->palette == pal) && (sk->skinnum ==      0) && (i < 1)) { i = 1; skzero = sk; }
		else if (((int)sk->palette ==   0) && (sk->skinnum ==      0) && (i < 0)) { i = 0; skzero = sk; }
	}
	if (!sk) {
		if (skzero) {
			tex = &skzero->tex[ hictinting[pal].f ];
			skinfile = skzero->fn;
			strcpy(fn,skinfile);
			//buildprintf("Using def skin 0,0 as fallback, pal=%d\n", pal);
		} else {
			if ((unsigned)number >= (unsigned)m->numskins) {
				number = 0;
			}
			tex = &m->tex[ number * (HICEFFECTMASK+1) + hictinting[pal].f ];
			skinfile = m->skinfn + number*64;
			strcpy(fn,m->basepath);
			strcat(fn,skinfile);
			//buildprintf("Using MD2/MD3 skin (%d) %s, pal=%d\n",number,skinfile,pal);
		}
	}

	if (!skinfile[0]) {
		return 0;
	}

	if (*tex && (*tex)->glpic) {
		// texture already loaded
		return *tex;
	}

	if (!(*tex)) {
		// no PTMHead referenced yet at *tex
		md_initident(&id, skinfile, hictinting[pal].f);
		*tex = PTM_GetHead(&id);
		if (!(*tex)) {
			return 0;
		}
	}

	if (!(*tex)->glpic) {
		// no texture loaded in the PTMHead yet
		if ((err = PTM_LoadTextureFile(skinfile, *tex, PTH_CLAMPED, hictinting[pal].f))) {
			if (polymosttexverbosity >= 1) {
				const char * errstr = PTM_GetLoadTextureFileErrorString(err);
				buildprintf("MDSprite: %s %s\n",
						   skinfile, errstr);
			}
			return 0;
		}
		m->usesalpha = (((*tex)->flags & PTH_HASALPHA) == PTH_HASALPHA);
	}

	if (!m->skinloaded)
	{
		if ((*tex)->sizx != (*tex)->tsizx || (*tex)->sizy != (*tex)->tsizy)
		{
			float fx, fy;
			fx = ((float)(*tex)->tsizx)/((float)(*tex)->sizx);
			fy = ((float)(*tex)->tsizy)/((float)(*tex)->sizy);
			if (m->mdnum == 2)
			{
				//FIXME correct uvs for non-2^x textures
			}
			else if (m->mdnum == 3)
			{
				md3model *m3 = (md3model *)m;
				md3surf_t *s;
				int surfi;
				for (surfi=0;surfi<m3->head.numsurfs;surfi++)
				{
					s = &m3->head.surfs[surfi];
					for(i=s->numverts-1;i>=0;i--)
					{
						s->uv[i].u *= fx;
						s->uv[i].v *= fy;
					}
				}
			}
		}
		m->skinloaded = 1+number;
	}

	glfunc.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,glfiltermodes[gltexfiltermode].mag);
	glfunc.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,glfiltermodes[gltexfiltermode].min);
#ifdef GL_EXT_texture_filter_anisotropic
	if (glinfo.maxanisotropy > 1.0)
		glfunc.glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,glanisotropy);
#endif
	glfunc.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
	glfunc.glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

	return (*tex);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
xorl %r12d, %r12d
cmpl $0x2, (%rdi)
cmovel %r12d, %ecx
cmpl $0xff, %edx
ja 0x612b6
movl %edx, %r14d
movq %rdi, %rbx
movl %esi, %ebp
movq 0x48(%rdi), %rax
testq %rax, %rax
je 0x61013
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %edx, %edx
movl $0x1, %esi
xorl %r10d, %r10d
movq %r10, %r8
movzbl (%rax), %r9d
cmpl %r14d, %r9d
jne 0x60efb
cmpl %ebp, 0x4(%rax)
jne 0x60ee9
cmpl %ecx, 0x8(%rax)
je 0x61116
testl %r9d, %r9d
je 0x60f04
movb $0x1, %r15b
cmpl %r14d, %r9d
je 0x60f28
jmp 0x60fdb
testl %r9d, %r9d
jne 0x60fdb
cmpl %ebp, 0x4(%rax)
jne 0x60f20
cmpl %ecx, 0x8(%rax)
jne 0x60f20
movl $0x5, %r11d
movq %rax, %r10
cmpl $0x5, %edi
jl 0x60fe1
cmpl %r14d, %r9d
jne 0x60f40
xorl %r15d, %r15d
cmpl $0x0, 0x4(%rax)
je 0x60f96
testb %r15b, %r15b
je 0x60f40
movb $0x1, %r15b
cmpl %r14d, %r9d
je 0x60f68
jmp 0x60fdb
movl 0x4(%rax), %r15d
testl %r15d, %r15d
je 0x60fab
cmpl %ebp, %r15d
jne 0x60f60
movl $0x2, %r11d
movq %rax, %r10
cmpl $0x2, %edi
jl 0x60fe1
cmpl %r14d, %r9d
jne 0x60fc0
xorl %r15d, %r15d
movl 0x4(%rax), %r9d
testl %r9d, %r9d
sete %r10b
testl %edi, %edi
setle %r12b
andb %r10b, %r12b
movq %r8, %r10
cmovneq %rax, %r10
movl %edi, %r11d
cmovnel %esi, %r11d
testb %r15b, %r15b
jne 0x60fe1
testb %r12b, %r12b
je 0x60fc4
jmp 0x60fe1
cmpl %ecx, 0x8(%rax)
jne 0x60f2e
movl $0x4, %r11d
movq %rax, %r10
cmpl $0x4, %edi
jl 0x60fe1
jmp 0x60f2e
cmpl %ecx, 0x8(%rax)
jne 0x60f49
movl $0x3, %r11d
movq %rax, %r10
cmpl $0x3, %edi
jl 0x60fe1
jmp 0x60f49
movl 0x4(%rax), %r9d
testl %r9d, %r9d
sete %r9b
testl %edi, %edi
sets %r10b
testb %r10b, %r9b
cmovneq %rax, %r8
cmovnel %edx, %edi
movq %r8, %r10
movl %edi, %r11d
movq 0x38(%rax), %rax
movl %r11d, %edi
testq %rax, %rax
jne 0x60ecf
testq %r10, %r10
je 0x61013
movl %r14d, %eax
leaq 0x5e7c20(%rip), %rcx    # 0x648c20
movzbl 0x3(%rcx,%rax,4), %ecx
leaq (%r10,%rcx,8), %r13
addq $0x18, %r13
movq 0x10(%r10), %r15
jmp 0x61047
xorl %eax, %eax
cmpl %ebp, 0x50(%rbx)
cmoval %ebp, %eax
movl %r14d, %ecx
leaq 0x5e7bfb(%rip), %rdx    # 0x648c20
movzbl 0x3(%rdx,%rcx,4), %ecx
leal (%rcx,%rax,4), %edx
movslq %edx, %r13
shlq $0x3, %r13
addq 0x18(%rbx), %r13
movl %eax, %ebp
shll $0x6, %eax
movslq %eax, %r15
addq 0x90(%rbx), %r15
cmpb $0x0, (%r15)
je 0x6110e
movq (%r13), %r12
testq %r12, %r12
je 0x61067
cmpl $0x0, (%r12)
jne 0x612b6
jmp 0x610bc
movzbl %cl, %eax
xorps %xmm0, %xmm0
leaq 0x18(%rsp), %r12
movaps %xmm0, -0x18(%r12)
movl $0x0, -0x8(%r12)
movl %eax, -0x4(%r12)
movl $0x3ff, %edx            # imm = 0x3FF
movq %r12, %rdi
movq %r15, %rsi
callq 0x6190
movb $0x0, 0x3ff(%r12)
movq %rsp, %rdi
callq 0x6624c
movq %rax, (%r13)
testq %rax, %rax
je 0x6110e
movq %rax, %r12
cmpl $0x0, (%rax)
jne 0x61146
movl %r14d, %eax
leaq 0x5e7b5a(%rip), %rcx    # 0x648c20
movzbl 0x3(%rcx,%rax,4), %ecx
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x662f6
testl %eax, %eax
je 0x61136
leaq 0x444a6(%rip), %rcx     # 0xa558c
cmpl $0x0, (%rcx)
jle 0x6110e
movl %eax, %edi
callq 0x67086
leaq 0x1a3e7(%rip), %rdi     # 0x7b4e0
xorl %r12d, %r12d
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x25221
jmp 0x612b6
xorl %r12d, %r12d
jmp 0x612b6
movl %r14d, %ecx
leaq 0x5e7b00(%rip), %rdx    # 0x648c20
movzbl 0x3(%rdx,%rcx,4), %ecx
leaq (%rax,%rcx,8), %r13
addq $0x18, %r13
movq 0x10(%rax), %r15
jmp 0x61047
movq (%r13), %rax
movl 0x4(%rax), %eax
shrl $0x3, %eax
andl $0x1, %eax
movl %eax, 0x30(%rbx)
cmpl $0x0, 0x54(%rbx)
jne 0x61208
movq (%r13), %rcx
movl 0x8(%rcx), %eax
movl 0x10(%rcx), %edx
cmpl %edx, %eax
jne 0x6116d
movl 0xc(%rcx), %esi
movl 0x14(%rcx), %edi
cmpl %edi, %esi
jne 0x61173
jmp 0x61203
movl 0xc(%rcx), %esi
movl 0x14(%rcx), %edi
cmpl $0x3, (%rbx)
jne 0x61203
movl 0xac(%rbx), %ecx
testl %ecx, %ecx
jle 0x61203
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
cvtsi2ss %eax, %xmm2
cvtsi2ss %edi, %xmm1
cvtsi2ss %esi, %xmm3
divss %xmm2, %xmm0
divss %xmm3, %xmm1
xorl %eax, %eax
movq 0xc8(%rbx), %rdx
movq %rax, %rdi
shlq $0x7, %rdi
movslq 0x50(%rdx,%rdi), %rsi
testq %rsi, %rsi
jle 0x611f8
addq %rdi, %rdx
incq %rsi
movq 0x68(%rdx), %rcx
movss -0x10(%rcx,%rsi,8), %xmm2
mulss %xmm0, %xmm2
movss %xmm2, -0x10(%rcx,%rsi,8)
movq 0x68(%rdx), %rcx
movss -0xc(%rcx,%rsi,8), %xmm2
mulss %xmm1, %xmm2
movss %xmm2, -0xc(%rcx,%rsi,8)
decq %rsi
cmpq $0x1, %rsi
ja 0x611c1
movl 0xac(%rbx), %ecx
incq %rax
movslq %ecx, %rdx
cmpq %rdx, %rax
jl 0x611a3
incl %ebp
movl %ebp, 0x54(%rbx)
leaq 0x599531(%rip), %rbx    # 0x5fa740
leaq 0x4436e(%rip), %r14     # 0xa5584
movslq (%r14), %rax
shlq $0x4, %rax
leaq 0x442fc(%rip), %r15     # 0xa5520
movl 0xc(%r15,%rax), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
callq *0xe8(%rbx)
movslq (%r14), %rax
shlq $0x4, %rax
movl 0x8(%r15,%rax), %edx
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
callq *0xe8(%rbx)
leaq 0x11cff4(%rip), %rax    # 0x17e250
movss 0x14(%rax), %xmm0
ucomiss 0x15eac(%rip), %xmm0    # 0x77114
jbe 0x61288
leaq 0x599713(%rip), %rax    # 0x5fa984
xorps %xmm0, %xmm0
cvtsi2ssl (%rax), %xmm0
movl $0xde1, %edi            # imm = 0xDE1
movl $0x84fe, %esi           # imm = 0x84FE
callq *0xe0(%rbx)
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl $0x2901, %edx           # imm = 0x2901
callq *0xe8(%rbx)
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl $0x2901, %edx           # imm = 0x2901
callq *0xe8(%rbx)
movq (%r13), %r12
movq %r12, %rax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq