// File: /workspace/github_c_makefile_star2_O1/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 $0x428, %rsp            # imm = 0x428
xorl %r12d, %r12d
cmpl $0x2, (%rdi)
cmovel %r12d, %ecx
cmpl $0xff, %edx
ja 0x5f248
movl %edx, %r14d
movq %rdi, %rbx
movl %esi, %ebp
movq 0x48(%rdi), %rax
xorl %r15d, %r15d
testq %rax, %rax
sete %r9b
je 0x5ef78
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x1, %esi
xorl %r10d, %r10d
movq %r10, %rdx
movzbl (%rax), %r8d
cmpl %r14d, %r8d
jne 0x5ee71
cmpl %ebp, 0x4(%rax)
jne 0x5ee71
cmpl %ecx, 0x8(%rax)
je 0x5f1d9
testl %r8d, %r8d
jne 0x5ee8e
cmpl %ebp, 0x4(%rax)
jne 0x5ee8e
cmpl %ecx, 0x8(%rax)
jne 0x5ee8e
movl $0x5, %r9d
movq %rax, %r10
cmpl $0x5, %edi
jl 0x5ef09
cmpl %r14d, %r8d
jne 0x5ee9d
cmpl $0x0, 0x4(%rax)
je 0x5ef1f
testl %r8d, %r8d
jne 0x5eeb5
cmpl $0x0, 0x4(%rax)
je 0x5ef3b
testl %r8d, %r8d
je 0x5ef57
cmpl %r14d, %r8d
jne 0x5eee5
cmpl $0x0, 0x4(%rax)
sete %r9b
testl %edi, %edi
setle %r11b
andb %r9b, %r11b
movq %rdx, %r10
cmovneq %rax, %r10
movl %edi, %r9d
cmovnel %esi, %r9d
testl %r8d, %r8d
jne 0x5ef09
testb %r11b, %r11b
je 0x5eeea
jmp 0x5ef09
testl %r8d, %r8d
jne 0x5ef03
cmpl $0x0, 0x4(%rax)
sete %r8b
testl %edi, %edi
sets %r9b
testb %r9b, %r8b
cmovneq %rax, %rdx
cmovnel %r15d, %edi
movq %rdx, %r10
movl %edi, %r9d
movq 0x38(%rax), %rax
movl %r9d, %edi
testq %rax, %rax
sete %r9b
jne 0x5ee57
jmp 0x5ef73
cmpl %ecx, 0x8(%rax)
jne 0x5ee9d
movl $0x4, %r9d
movq %rax, %r10
cmpl $0x4, %edi
jl 0x5ef09
jmp 0x5ee9d
cmpl %ecx, 0x8(%rax)
jne 0x5eeac
movl $0x3, %r9d
movq %rax, %r10
cmpl $0x3, %edi
jl 0x5ef09
jmp 0x5eeac
cmpl %ebp, 0x4(%rax)
jne 0x5eeb5
movl $0x2, %r9d
movq %rax, %r10
cmpl $0x2, %edi
jl 0x5ef09
jmp 0x5eeb5
movq %r10, %rdx
jmp 0x5ef7a
xorl %edx, %edx
xorl %r13d, %r13d
testb $0x1, %r9b
je 0x5efd8
testq %rdx, %rdx
je 0x5efa5
movl %r14d, %eax
leaq 0x5e8c9e(%rip), %rcx    # 0x647c30
movzbl 0x3(%rcx,%rax,4), %eax
leaq (%rdx,%rax,8), %r13
addq $0x18, %r13
movq 0x10(%rdx), %r15
jmp 0x5efd8
xorl %eax, %eax
cmpl %ebp, 0x50(%rbx)
cmovbel %eax, %ebp
movslq %ebp, %rax
movl %r14d, %ecx
leaq 0x5e8c76(%rip), %rdx    # 0x647c30
movzbl 0x3(%rdx,%rcx,4), %ecx
leaq (%rcx,%rax,4), %r13
shlq $0x3, %r13
addq 0x18(%rbx), %r13
shll $0x6, %eax
movslq %eax, %r15
addq 0x90(%rbx), %r15
cmpb $0x0, (%r15)
je 0x5f245
movq (%r13), %r12
testq %r12, %r12
je 0x5effb
cmpl $0x0, (%r12)
jne 0x5f248
testq %r12, %r12
jne 0x5f067
movl %r14d, %eax
leaq 0x5e8c2b(%rip), %rcx    # 0x647c30
movq %r13, 0x8(%rsp)
movzbl 0x3(%rcx,%rax,4), %r13d
leaq 0x10(%rsp), %rdi
xorl %r12d, %r12d
movl $0x418, %edx            # imm = 0x418
xorl %esi, %esi
callq 0x62f0
leaq 0x10(%rsp), %rax
movl %r13d, 0x14(%rax)
movq 0x8(%rsp), %r13
leaq 0x28(%rsp), %rdi
movl $0x3ff, %edx            # imm = 0x3FF
movq %r15, %rsi
callq 0x6190
leaq 0x28(%rsp), %rax
movb $0x0, 0x3ff(%rax)
leaq 0x10(%rsp), %rdi
callq 0x644ee
movq %rax, (%r13)
testq %rax, %rax
je 0x5f248
movq (%r13), %rsi
cmpl $0x0, (%rsi)
je 0x5f1f9
cmpl $0x0, 0x54(%rbx)
jne 0x5f129
movq (%r13), %rax
movl 0x8(%rax), %ecx
movl 0x10(%rax), %edx
cmpl %edx, %ecx
jne 0x5f098
movl 0xc(%rax), %esi
cmpl 0x14(%rax), %esi
je 0x5f124
cmpl $0x3, (%rbx)
jne 0x5f124
cmpl $0x0, 0xac(%rbx)
jle 0x5f124
cvtsi2ss %edx, %xmm0
cvtsi2ss %ecx, %xmm2
cvtsi2ssl 0x14(%rax), %xmm1
cvtsi2ssl 0xc(%rax), %xmm3
divss %xmm2, %xmm0
divss %xmm3, %xmm1
xorl %eax, %eax
movq 0xc8(%rbx), %rcx
movq %rax, %rsi
shlq $0x7, %rsi
movslq 0x50(%rcx,%rsi), %rdx
testq %rdx, %rdx
jle 0x5f115
addq %rsi, %rcx
incq %rdx
movq 0x68(%rcx), %rsi
movss -0x10(%rsi,%rdx,8), %xmm2
mulss %xmm0, %xmm2
movss %xmm2, -0x10(%rsi,%rdx,8)
movq 0x68(%rcx), %rsi
movss -0xc(%rsi,%rdx,8), %xmm2
mulss %xmm1, %xmm2
movss %xmm2, -0xc(%rsi,%rdx,8)
decq %rdx
cmpq $0x1, %rdx
jg 0x5f0e4
incq %rax
movslq 0xac(%rbx), %rcx
cmpq %rcx, %rax
jl 0x5f0c6
incl %ebp
movl %ebp, 0x54(%rbx)
leaq 0x59a610(%rip), %rbx    # 0x5f9740
leaq 0x4544d(%rip), %r14     # 0xa4584
movslq (%r14), %rax
shlq $0x4, %rax
leaq 0x453db(%rip), %r15     # 0xa4520
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 0x11e0d3(%rip), %rax    # 0x17d250
movss 0x14(%rax), %xmm0
ucomiss 0x16e0b(%rip), %xmm0    # 0x75f94
jbe 0x5f1a9
leaq 0x59a7f2(%rip), %rax    # 0x5f9984
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
jmp 0x5f248
movl %r14d, %ecx
leaq 0x5e8a4d(%rip), %rsi    # 0x647c30
movzbl 0x3(%rsi,%rcx,4), %ecx
leaq (%rax,%rcx,8), %r13
addq $0x18, %r13
movq 0x10(%rax), %r15
jmp 0x5ef7d
movl %r14d, %eax
leaq 0x5e8a2d(%rip), %rcx    # 0x647c30
movzbl 0x3(%rcx,%rax,4), %ecx
movq %r15, %rdi
movl $0x1, %edx
callq 0x64591
testl %eax, %eax
je 0x5f25d
leaq 0x4536c(%rip), %rcx     # 0xa458c
cmpl $0x0, (%rcx)
jle 0x5f245
movl %eax, %edi
callq 0x65363
leaq 0x1adf5(%rip), %rdi     # 0x7a028
xorl %r12d, %r12d
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x24cb4
jmp 0x5f248
xorl %r12d, %r12d
movq %r12, %rax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r13), %rax
movl 0x4(%rax), %eax
shrl $0x3, %eax
andl $0x1, %eax
movl %eax, 0x30(%rbx)
jmp 0x5f074