// File: /workspace/github_c_makefile_star2_O2/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 %r14d, %r14d
cmpl $0x2, (%rdi)
cmovel %r14d, %ecx
cmpl $0xff, %edx
ja 0x56bc0
movq %rdi, %rbx
movl %esi, %ebp
leaq 0x48(%rdi), %rax
pushq $-0x1
popq %r9
pushq $0x2
popq %r10
xorl %r11d, %r11d
pushq $0x1
popq %rsi
movq (%rax), %rax
movl %edx, %r13d
testq %rax, %rax
je 0x56933
movzbl (%rax), %r12d
cmpl %edx, %r12d
jne 0x5684c
cmpl %ebp, 0x4(%rax)
jne 0x5683a
cmpl %ecx, 0x8(%rax)
je 0x56987
testl %r12d, %r12d
je 0x56855
movb $0x1, %dil
cmpl %edx, %r12d
je 0x56877
jmp 0x5691e
testl %r12d, %r12d
jne 0x5691e
cmpl %ebp, 0x4(%rax)
jne 0x56870
cmpl %ecx, 0x8(%rax)
jne 0x56870
movq %rax, %r13
pushq $0x5
popq %r15
cmpl $0x5, %r9d
jl 0x56924
cmpl %edx, %r12d
jne 0x5688f
xorl %edi, %edi
cmpl $0x0, 0x4(%rax)
je 0x568db
testb %dil, %dil
je 0x5688f
movb $0x1, %r8b
cmpl %edx, %r12d
je 0x568ae
jmp 0x5691e
movl 0x4(%rax), %edi
testl %edi, %edi
je 0x568ef
cmpl %ebp, %edi
jne 0x568a6
movq %rax, %r13
movl %r10d, %r15d
cmpl $0x2, %r9d
jl 0x56924
cmpl %edx, %r12d
jne 0x56903
xorl %r8d, %r8d
movl 0x4(%rax), %edi
testl %edi, %edi
sete %r15b
testl %r9d, %r9d
setle %r12b
andb %r15b, %r12b
movq %r11, %r13
cmovneq %rax, %r13
movl %r9d, %r15d
cmovnel %esi, %r15d
testb %r8b, %r8b
jne 0x56924
testb %r12b, %r12b
je 0x56906
jmp 0x56924
cmpl %ecx, 0x8(%rax)
jne 0x5687d
movq %rax, %r13
pushq $0x4
popq %r15
cmpl $0x4, %r9d
jl 0x56924
jmp 0x5687d
cmpl %ecx, 0x8(%rax)
jne 0x56896
movq %rax, %r13
pushq $0x3
popq %r15
cmpl $0x3, %r9d
jl 0x56924
jmp 0x56896
movl 0x4(%rax), %edi
testl %edi, %edi
sete %dil
testl %r9d, %r9d
sets %r8b
testb %r8b, %dil
cmovneq %rax, %r11
cmovnel %r14d, %r9d
movq %r11, %r13
movl %r9d, %r15d
addq $0x38, %rax
movq %r13, %r11
movl %r15d, %r9d
jmp 0x56814
testq %r11, %r11
je 0x56953
leaq 0x5eb2e1(%rip), %rax    # 0x641c20
movzbl 0x3(%rax,%r13,4), %ecx
leaq (%r11,%rcx,8), %r12
addq $0x18, %r12
movq 0x10(%r11), %r15
jmp 0x569a0
xorl %eax, %eax
cmpl %ebp, 0x50(%rbx)
cmoval %ebp, %eax
leaq 0x5eb2be(%rip), %rcx    # 0x641c20
movzbl 0x3(%rcx,%r13,4), %ecx
leal (%rcx,%rax,4), %edx
movslq %edx, %r12
shlq $0x3, %r12
addq 0x18(%rbx), %r12
movl %eax, %ebp
shll $0x6, %eax
movslq %eax, %r15
addq 0x90(%rbx), %r15
jmp 0x569a0
leaq 0x5eb292(%rip), %rcx    # 0x641c20
movzbl 0x3(%rcx,%r13,4), %ecx
leaq (%rax,%rcx,8), %r12
addq $0x18, %r12
movq 0x10(%rax), %r15
cmpb $0x0, (%r15)
je 0x56a57
movq (%r12), %r14
testq %r14, %r14
je 0x569bf
cmpl $0x0, (%r14)
jne 0x56bc0
jmp 0x56a09
movzbl %cl, %eax
xorps %xmm0, %xmm0
leaq 0x18(%rsp), %r14
movaps %xmm0, -0x18(%r14)
andl $0x0, -0x8(%r14)
movl %eax, -0x4(%r14)
movl $0x3ff, %edx            # imm = 0x3FF
movq %r14, %rdi
movq %r15, %rsi
callq 0x6190
movb $0x0, 0x3ff(%r14)
movq %rsp, %rdi
callq 0x5b7b0
movq %rax, (%r12)
testq %rax, %rax
je 0x56a57
movq %rax, %r14
cmpl $0x0, (%rax)
jne 0x56a6f
leaq 0x5eb210(%rip), %rax    # 0x641c20
movzbl 0x3(%rax,%r13,4), %ecx
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x5b84a
testl %eax, %eax
je 0x56a5f
leaq 0x47b5d(%rip), %rcx     # 0x9e58c
cmpl $0x0, (%rcx)
jle 0x56a57
movl %eax, %edi
callq 0x5c526
leaq 0x197de(%rip), %rdi     # 0x70220
xorl %r14d, %r14d
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22427
jmp 0x56bc0
xorl %r14d, %r14d
jmp 0x56bc0
movq (%r12), %rax
movl 0x4(%rax), %eax
shrl $0x3, %eax
andl $0x1, %eax
movl %eax, 0x30(%rbx)
cmpl $0x0, 0x54(%rbx)
jne 0x56b15
movq (%r12), %rsi
movl 0x8(%rsi), %eax
movl 0x10(%rsi), %ecx
cmpl %ecx, %eax
jne 0x56a93
movl 0xc(%rsi), %edx
movl 0x14(%rsi), %esi
cmpl %esi, %edx
jne 0x56a99
jmp 0x56b10
movl 0xc(%rsi), %edx
movl 0x14(%rsi), %esi
cmpl $0x3, (%rbx)
jne 0x56b10
cvtsi2ss %ecx, %xmm0
cvtsi2ss %eax, %xmm2
cvtsi2ss %esi, %xmm1
cvtsi2ss %edx, %xmm3
divss %xmm2, %xmm0
divss %xmm3, %xmm1
xorl %eax, %eax
movslq 0xac(%rbx), %rcx
cmpq %rcx, %rax
jge 0x56b10
movq 0xc8(%rbx), %rdx
movq %rax, %rsi
shlq $0x7, %rsi
leaq (%rdx,%rsi), %rcx
movl 0x50(%rdx,%rsi), %edx
testl %edx, %edx
jle 0x56b0b
movq 0x68(%rcx), %rsi
movss -0x8(%rsi,%rdx,8), %xmm2
mulss %xmm0, %xmm2
movss %xmm2, -0x8(%rsi,%rdx,8)
movq 0x68(%rcx), %rsi
movss -0x4(%rsi,%rdx,8), %xmm2
mulss %xmm1, %xmm2
movss %xmm2, -0x4(%rsi,%rdx,8)
decq %rdx
jmp 0x56ada
incq %rax
jmp 0x56ab8
incl %ebp
movl %ebp, 0x54(%rbx)
leaq 0x59cc24(%rip), %rbx    # 0x5f3740
leaq 0x47a61(%rip), %r14     # 0x9e584
movslq (%r14), %rax
shlq $0x4, %rax
leaq 0x479ef(%rip), %r15     # 0x9e520
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 0x1206e7(%rip), %rax    # 0x177250
movss 0x14(%rax), %xmm0
ucomiss 0x1545f(%rip), %xmm0    # 0x6bfd4
jbe 0x56b92
leaq 0x59ce06(%rip), %rax    # 0x5f3984
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 (%r12), %r14
movq %r14, %rax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq