// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/polymosttex.c
// Function: PTM_LoadTextureFile
int PTM_LoadTextureFile(const char* filename, PTMHead* ptmh, int flags, int effects)
{
	PTTexture tex;
	int filh, picdatalen;
	int y;
	char * picdata = 0;
	PTCacheTile * tdef = 0;
	int writetocache = 0, iscached = 0;

	if (!(flags & PTH_NOCOMPRESS) && glusetexcache && glusetexcompr) {
		iscached = PTCacheHasTile(filename, effects, (flags & PTH_CLAMPED));

		// if the texture exists in the cache but the original file is newer,
		// ignore what's in the cache and overwrite it
		/*if (iscached && filemtime(filename) > filemtime(cacheitem)) {
			iscached = 0;
		}*/

		if (!iscached) {
			writetocache = 1;
		}
	}

	if (iscached) {
		if (ptm_loadcachedtexturefile(filename, ptmh, flags, effects) == 0) {
			return 0;
		}
	}

	filh = kopen4load((char *) filename, 0);
	if (filh < 0) {
		return -1;
	}
	picdatalen = kfilelength(filh);

	picdata = (char *) malloc(picdatalen);
	if (!picdata) {
		kclose(filh);
		return -2;
	}

	if (kread(filh, picdata, picdatalen) != picdatalen) {
		kclose(filh);
		return -3;
	}

	kclose(filh);

	kpgetdim(picdata, picdatalen, (int *) &tex.tsizx, (int *) &tex.tsizy);
	if (tex.tsizx == 0 || tex.tsizy == 0) {
		free(picdata);
		return -4;
	}

	if (!glinfo.texnpot || writetocache) {
		for (tex.sizx = 1; tex.sizx < tex.tsizx; tex.sizx += tex.sizx) ;
		for (tex.sizy = 1; tex.sizy < tex.tsizy; tex.sizy += tex.sizy) ;
	} else {
		tex.sizx = tex.tsizx;
		tex.sizy = tex.tsizy;
	}

	tex.pic = (coltype *) malloc(tex.sizx * tex.sizy * sizeof(coltype));
	if (!tex.pic) {
		return -2;
	}
	memset(tex.pic, 0, tex.sizx * tex.sizy * sizeof(coltype));

	if (kprender(picdata, picdatalen, tex.pic, tex.sizx * sizeof(coltype), tex.sizx, tex.sizy, 0, 0)) {
		free(picdata);
		free(tex.pic);
		return -5;
	}

	free(picdata);
	picdata = 0;

	ptm_applyeffects(&tex, effects);	// updates tex.hasalpha

	ptmh->flags = 0;
	if (tex.hasalpha) {
		ptmh->flags |= PTH_HASALPHA;
	}

	if (! (flags & PTH_CLAMPED) || (flags & PTH_SKYBOX)) { //Duplicate texture pixels (wrapping tricks for non power of 2 texture sizes)
		if (tex.sizx > tex.tsizx) {	//Copy left to right
			coltype * lptr = tex.pic;
			for (y = 0; y < tex.tsizy; y++, lptr += tex.sizx) {
				memcpy(&lptr[tex.tsizx], lptr, (tex.sizx - tex.tsizx) << 2);
			}
		}
		if (tex.sizy > tex.tsizy) {	//Copy top to bottom
			memcpy(&tex.pic[tex.sizx * tex.tsizy], tex.pic, (tex.sizy - tex.tsizy) * tex.sizx << 2);
		}
	}

	tex.rawfmt = GL_BGRA;
	if (!glinfo.bgra) {
		int j;
		for (j = tex.sizx * tex.sizy - 1; j >= 0; j--) {
			swapchar(&tex.pic[j].r, &tex.pic[j].b);
		}
		tex.rawfmt = GL_RGBA;
	}

	ptmh->tsizx = tex.tsizx;
	ptmh->tsizy = tex.tsizy;
	ptmh->sizx  = tex.sizx;
	ptmh->sizy  = tex.sizy;

	if (writetocache) {
		int nmips = 0;
		while (max(1, (tex.sizx >> nmips)) > 1 ||
			   max(1, (tex.sizy >> nmips)) > 1) {
			nmips++;
		}
		nmips++;

		tdef = PTCacheAllocNewTile(nmips);
		tdef->filename = strdup(filename);
		tdef->effects = effects;
		tdef->flags = (flags | ptmh->flags) & (PTH_CLAMPED | PTH_HASALPHA);
	}

	ptm_uploadtexture(ptmh, flags, &tex, tdef);

	if (writetocache) {
		if (polymosttexverbosity >= 2) {
			buildprintf("PolymostTex: writing %s (effects %d, flags %d) to cache\n",
					   tdef->filename, tdef->effects, tdef->flags);
		}
		PTCacheWriteTile(tdef);
		PTCacheFreeTile(tdef);
		tdef = 0;
	}


	if (tex.pic) {
		free(tex.pic);
		tex.pic = 0;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %ecx, %ebp
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %r13
xorl %r12d, %r12d
testb $0x10, %r15b
jne 0x5ba93
leaq 0x42d11(%rip), %rax     # 0x9e588
cmpl $0x0, (%rax)
je 0x5ba93
leaq 0x42cf9(%rip), %rax     # 0x9e580
cmpl $0x0, (%rax)
je 0x5ba93
movl %r15d, %ebx
andl $0x1, %ebx
movq %r13, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x5dd99
testl %eax, %eax
je 0x5b9c5
movq %r13, %rdi
movl %ebp, %esi
movl %ebx, %edx
callq 0x5db1a
testq %rax, %rax
je 0x5ba90
movq %rax, %r12
movl 0x10(%rax), %r9d
cmpl $0x83f3, %r9d           # imm = 0x83F3
je 0x5ba2d
cmpl $0x83f1, %r9d           # imm = 0x83F1
jne 0x5ba45
pushq $0x1
popq %rax
leaq 0x11b966(%rip), %rcx    # 0x177250
cmpb $0x0, 0x1a(%rcx)
je 0x5ba41
cmpl $0x2, 0x42c91(%rip)     # 0x9e58c
jl 0x5b924
movq (%r12), %rsi
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
leaq 0x3022e(%rip), %rdi     # 0x8bb40
movq (%rdi,%rax,8), %r8
leaq 0x15786(%rip), %rdi     # 0x710a3
xorl %eax, %eax
callq 0x22427
movl (%r14), %esi
testl %esi, %esi
jne 0x5b941
leaq 0x597e0e(%rip), %rax    # 0x5f3740
pushq $0x1
popq %rdi
movq %r14, %rsi
callq *0xb8(%rax)
movl (%r14), %esi
movl 0x14(%r12), %eax
movl %eax, 0x10(%r14)
movl 0x18(%r12), %eax
movl %eax, 0x14(%r14)
movl 0x20(%r12), %eax
movl %eax, 0x8(%r14)
movl 0x24(%r12), %eax
movl %eax, 0xc(%r14)
movl 0xc(%r12), %eax
andl $0x8, %eax
movl %eax, 0x4(%r14)
leaq 0x597dc8(%rip), %r14    # 0x5f3740
movl $0xde1, %edi            # imm = 0xDE1
callq *0xc8(%r14)
leaq 0x598011(%rip), %rax    # 0x5f399c
movl (%rax), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
testb $0x20, %r15b
cmovnel %ecx, %eax
leaq 0x597ff6(%rip), %rcx    # 0x5f3998
movb (%rcx), %cl
pushq $0x1
popq %rdx
shll %cl, %edx
imulq $0x18, %rax, %rcx
addq %r12, %rcx
addq $0x24, %rcx
cmpl %edx, -0x4(%rcx)
jg 0x5b9bd
cmpl %edx, (%rcx)
jle 0x5b9cd
incl %eax
addq $0x18, %rcx
jmp 0x5b9b4
movb $0x1, %r12b
jmp 0x5ba93
movl %eax, %r15d
imulq $0x18, %r15, %rax
leaq (%r12,%rax), %r13
addq $0x30, %r13
xorl %ebx, %ebx
leaq (%r15,%rbx), %rax
movslq 0x1c(%r12), %rcx
cmpq %rcx, %rax
jge 0x5ba20
movl 0x10(%r12), %edx
movl -0x10(%r13), %ecx
movl -0xc(%r13), %r8d
movl -0x8(%r13), %eax
movl $0xde1, %edi            # imm = 0xDE1
movl %ebx, %esi
xorl %r9d, %r9d
pushq (%r13)
pushq %rax
callq *0xf0(%r14)
addq $0x10, %rsp
incq %rbx
addq $0x18, %r13
jmp 0x5b9de
movq %r12, %rdi
callq 0x5ddba
jmp 0x5bf26
leaq 0x11b81c(%rip), %rcx    # 0x177250
pushq $0x2
popq %rax
cmpb $0x0, 0x1b(%rcx)
jne 0x5b8f4
xorl %edi, %edi
jmp 0x5ba4a
movb $0x1, %dil
xorl %eax, %eax
cmpl $0x2, 0x42b3b(%rip)     # 0x9e58c
jl 0x5ba88
movq (%r12), %rsi
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
testb %dil, %dil
je 0x5ba6f
leaq 0xda34(%rip), %r8       # 0x694a1
jmp 0x5ba7a
leaq 0x300ca(%rip), %rdi     # 0x8bb40
movq (%rdi,%rax,8), %r8
leaq 0x155d1(%rip), %rdi     # 0x71052
xorl %eax, %eax
callq 0x22427
movq %r12, %rdi
callq 0x5ddba
xorl %r12d, %r12d
movq %r13, %rdi
xorl %esi, %esi
callq 0x1dc36
testl %eax, %eax
js 0x5bb31
movl %eax, %ebx
movl %r12d, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq %r14, 0x30(%rsp)
movl %eax, %edi
callq 0x1e14c
movl %eax, %r14d
movslq %eax, %rdi
callq 0x69a0
pushq $-0x2
popq %r12
testq %rax, %rax
je 0x5bb35
movq %rax, %r13
movl %r15d, 0x24(%rsp)
movl %ebx, %edi
movq %rax, %rsi
movl %r14d, %edx
callq 0x1de3f
movl %eax, %r15d
movl %ebx, %edi
callq 0x1e2db
cmpl %r14d, %r15d
jne 0x5bb3e
leaq 0x10(%rsp), %rdx
leaq 0x14(%rsp), %r15
movq %r13, %rdi
movl %r14d, %esi
movq %r15, %rcx
callq 0x3c30c
movl -0x4(%r15), %ebx
movl (%r15), %eax
testl %ebx, %ebx
sete %cl
testl %eax, %eax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x5bb54
movq %r13, %rdi
callq 0x61c0
pushq $-0x4
jmp 0x5bb40
pushq $-0x1
jmp 0x5bb40
movl %ebx, %edi
callq 0x1e2db
jmp 0x5bb42
pushq $-0x3
popq %r12
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11b6f5(%rip), %rcx    # 0x177250
cmpb $0x0, 0x1d(%rcx)
setne %cl
movl 0x20(%rsp), %edx
notb %dl
testb %cl, %dl
jne 0x5bb8f
pushq $0x1
popq %rdx
movl %edx, %ecx
leal (%rcx,%rcx), %edx
cmpl %ebx, %ecx
jl 0x5bb6f
movl %ecx, 0x8(%rsp)
pushq $0x1
popq %rdx
movl %edx, %r15d
leal (%r15,%r15), %edx
cmpl %eax, %r15d
jl 0x5bb7f
movl %ecx, %ebx
jmp 0x5bb96
movl %ebx, 0x8(%rsp)
movl %eax, %r15d
movl %r15d, 0xc(%rsp)
movl %r15d, %eax
imull %ebx, %eax
movslq %eax, %rsi
shlq $0x2, %rsi
pushq $0x1
popq %rdi
callq 0x6930
movq %rax, (%rsp)
testq %rax, %rax
je 0x5bb42
leal (,%rbx,4), %ecx
movq %r13, %rdi
movl %r14d, %esi
movq %rax, %rdx
movl %ebx, %r8d
movl %r15d, %r9d
pushq $0x0
pushq $0x0
callq 0x3c560
popq %rcx
popq %rdx
movl %eax, %ebx
movq %r13, %rdi
callq 0x61c0
movq (%rsp), %rdi
testl %ebx, %ebx
je 0x5bbf8
callq 0x61c0
pushq $-0x5
jmp 0x5bb40
testl %ebp, %ebp
sete %cl
leaq 0x41b88(%rip), %rax     # 0x9d78c
movl (%rax), %eax
testl %eax, %eax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x5bc5f
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %esi
movslq 0x8(%rsp), %rax
movq %rax, %r8
shlq $0x2, %r8
movl $0xff, %ecx
movl 0x24(%rsp), %r15d
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %r13
testl %esi, %esi
jle 0x5bd9d
decl %esi
movq %rdx, %r9
testl %r9d, %r9d
jle 0x5bc5a
movzbl -0x1(%rdi,%r9,4), %r10d
decq %r9
andl %r10d, %ecx
jmp 0x5bc47
addq %r8, %rdi
jmp 0x5bc3a
xorl %ecx, %ecx
testl %eax, %eax
movl 0x24(%rsp), %r15d
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %r13
jne 0x5bc7d
leaq 0x155d89(%rip), %rax    # 0x1b1a04
movl (%rax), %ecx
movslq %ecx, %rax
shlq $0x8, %rax
addq 0x30345(%rip), %rax     # 0x8bfd0
movl 0x14(%rsp), %edx
movl %ebp, %esi
shll $0x1e, %esi
sarl $0x1f, %esi
movl $0xff, %ecx
movsd 0x1527c(%rip), %xmm0    # 0x70f20
movsd 0x1527c(%rip), %xmm1    # 0x70f28
movsd 0x1527c(%rip), %xmm2    # 0x70f30
movsd 0x102dc(%rip), %xmm3    # 0x6bf98
xorpd %xmm4, %xmm4
testl %edx, %edx
jle 0x5bd99
decl %edx
movl 0x10(%rsp), %r8d
testl %r8d, %r8d
jle 0x5bd8b
movzbl -0x2(%rdi,%r8,4), %r9d
movb (%rax,%r9), %r9b
movzbl -0x3(%rdi,%r8,4), %r10d
movb (%rax,%r10), %r10b
movzbl -0x4(%rdi,%r8,4), %r11d
movb (%rax,%r11), %r11b
testb $0x1, %bpl
je 0x5bd63
movzbl %r11b, %r11d
cvtsi2sd %r11d, %xmm5
mulsd %xmm0, %xmm5
cvtsd2ss %xmm5, %xmm5
movzbl %r10b, %r10d
cvtsi2sd %r10d, %xmm6
cvtss2sd %xmm5, %xmm5
mulsd %xmm1, %xmm6
addsd %xmm5, %xmm6
cvtsd2ss %xmm6, %xmm5
movzbl %r9b, %r9d
cvtsi2sd %r9d, %xmm6
cvtss2sd %xmm5, %xmm5
mulsd %xmm2, %xmm6
addsd %xmm5, %xmm6
cvtsd2ss %xmm6, %xmm5
cvtss2sd %xmm5, %xmm5
movapd %xmm3, %xmm6
minsd %xmm5, %xmm6
movapd %xmm6, %xmm5
cmplesd %xmm4, %xmm5
andnpd %xmm6, %xmm5
cvttsd2si %xmm5, %r9d
movl %r9d, %r10d
movl %r9d, %r11d
movzbl -0x1(%rdi,%r8,4), %ebx
andl %ebx, %ecx
xorb %sil, %r9b
xorb %sil, %r10b
xorb %sil, %r11b
movb %r11b, -0x4(%rdi,%r8,4)
movb %r10b, -0x3(%rdi,%r8,4)
movb %r9b, -0x2(%rdi,%r8,4)
decq %r8
jmp 0x5bccf
movslq 0x8(%rsp), %r8
leaq (%rdi,%r8,4), %rdi
jmp 0x5bcc0
movl 0x8(%rsp), %eax
xorl %edx, %edx
cmpl $0xff, %ecx
setne %dl
movl %edx, 0x1c(%rsp)
shll $0x3, %edx
movl %edx, 0x4(%r12)
movl %r15d, %ecx
andl $0x5, %ecx
cmpl $0x1, %ecx
je 0x5be28
cmpl 0x10(%rsp), %eax
jle 0x5bdfc
xorl %r14d, %r14d
movq (%rsp), %rbx
movl 0x14(%rsp), %ecx
cmpl %ecx, %r14d
jge 0x5be00
movslq 0x10(%rsp), %rcx
leaq (%rbx,%rcx,4), %rdi
subl %ecx, %eax
shll $0x2, %eax
movslq %eax, %rdx
movq %rbx, %rsi
callq 0x60c0
incl %r14d
movslq 0x8(%rsp), %rax
leaq (%rbx,%rax,4), %rbx
jmp 0x5bdcc
movl 0x14(%rsp), %ecx
movl 0xc(%rsp), %edx
subl %ecx, %edx
jle 0x5be28
movq (%rsp), %rsi
imull %eax, %ecx
movslq %ecx, %rcx
leaq (%rsi,%rcx,4), %rdi
imull %eax, %edx
shll $0x2, %edx
movslq %edx, %rdx
callq 0x60c0
movl 0x8(%rsp), %eax
leaq 0x11b421(%rip), %rcx    # 0x177250
movl $0x80e1, 0x18(%rsp)     # imm = 0x80E1
cmpb $0x0, 0x18(%rcx)
movl 0xc(%rsp), %edx
jne 0x5be73
imull %eax, %edx
testl %edx, %edx
jle 0x5be63
movq (%rsp), %rax
movb -0x4(%rax,%rdx,4), %cl
movb -0x2(%rax,%rdx,4), %sil
movb %cl, -0x2(%rax,%rdx,4)
movb %sil, -0x4(%rax,%rdx,4)
decq %rdx
jmp 0x5be44
movl $0x1908, 0x18(%rsp)     # imm = 0x1908
movl 0x8(%rsp), %eax
movl 0xc(%rsp), %edx
movq 0x10(%rsp), %rcx
movq %rcx, 0x10(%r12)
movl %eax, 0x8(%r12)
movl %edx, 0xc(%r12)
xorl %r14d, %r14d
cmpb $0x0, 0x20(%rsp)
je 0x5bed2
movl %eax, %edi
movl %r14d, %ecx
sarl %cl, %edi
movl %edx, %esi
sarl %cl, %esi
incl %r14d
cmpl $0x1, %edi
jg 0x5be91
cmpl $0x1, %esi
jg 0x5be91
movl %r14d, %edi
callq 0x5ddf8
movq %rax, %r14
movq %r13, %rdi
callq 0x6060
movq %rax, (%r14)
movl %ebp, 0x8(%r14)
movl 0x4(%r12), %eax
orl %r15d, %eax
andl $0x9, %eax
movl %eax, 0xc(%r14)
movzwl %r15w, %esi
movq %rsp, %rdx
movq %r12, %rdi
movq %r14, %rcx
callq 0x5bf2e
cmpb $0x0, 0x20(%rsp)
je 0x5bf1d
cmpl $0x2, 0x4269a(%rip)     # 0x9e58c
jl 0x5bf0d
movq (%r14), %rsi
movl 0x8(%r14), %edx
movl 0xc(%r14), %ecx
leaq 0x150b4(%rip), %rdi     # 0x70fba
xorl %eax, %eax
callq 0x22427
movq %r14, %rdi
callq 0x5de11
movq %r14, %rdi
callq 0x5ddba
movq (%rsp), %rdi
callq 0x61c0
xorl %r12d, %r12d
jmp 0x5bb42