// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movl %ecx, %r14d
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r15
xorl %r13d, %r13d
movb $0x1, %al
testb $0x10, %bpl
jne 0x645e6
leaq 0x3ffcc(%rip), %rcx     # 0xa4588
cmpl $0x0, (%rcx)
je 0x645e6
leaq 0x3ffb8(%rip), %rcx     # 0xa4580
cmpl $0x0, (%rcx)
je 0x645e6
movl %ebp, %edx
andl $0x1, %edx
movq %r15, %rdi
movl %r14d, %esi
callq 0x66d26
testl %eax, %eax
sete %r13b
movl %r13d, %eax
testb %al, %al
jne 0x647d9
movl %ebp, %edx
andl $0x1, %edx
movq %r15, %rdi
movl %r14d, %esi
callq 0x66a62
testq %rax, %rax
je 0x647d9
movq %rax, %r12
movl 0x10(%rax), %r9d
cmpl $0x83f3, %r9d           # imm = 0x83F3
je 0x64774
cmpl $0x83f1, %r9d           # imm = 0x83F1
jne 0x6478e
leaq 0x118c21(%rip), %rcx    # 0x17d250
movl $0x1, %eax
cmpb $0x0, 0x1a(%rcx)
je 0x6478a
cmpl $0x2, 0x3ff47(%rip)     # 0xa458c
jl 0x6466e
movq (%r12), %rsi
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
leaq 0x2d4d4(%rip), %rdi     # 0x91b30
movq (%rdi,%rax,8), %r8
leaq 0x16824(%rip), %rdi     # 0x7ae8b
xorl %eax, %eax
callq 0x24cb4
cmpl $0x0, (%rbx)
jne 0x64688
leaq 0x5950c6(%rip), %rax    # 0x5f9740
movl $0x1, %edi
movq %rbx, %rsi
callq *0xb8(%rax)
movl 0x14(%r12), %eax
movl %eax, 0x10(%rbx)
movl 0x18(%r12), %eax
movl %eax, 0x14(%rbx)
movl 0x20(%r12), %eax
movl %eax, 0x8(%rbx)
movl 0x24(%r12), %eax
movl %eax, 0xc(%rbx)
movl 0xc(%r12), %eax
andl $0x8, %eax
movl %eax, 0x4(%rbx)
leaq 0x595086(%rip), %r14    # 0x5f9740
movl (%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0xc8(%r14)
leaq 0x5952cd(%rip), %rax    # 0x5f999c
movl (%rax), %r15d
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
testb $0x20, %bpl
cmovnel %eax, %r15d
leaq 0x5952ae(%rip), %rax    # 0x5f9998
movb (%rax), %cl
movl $0x1, %eax
shll %cl, %eax
leaq (%r15,%r15,2), %rcx
leaq (%r12,%rcx,8), %rcx
addq $0x24, %rcx
cmpl %eax, -0x4(%rcx)
jg 0x64708
cmpl %eax, (%rcx)
jle 0x64711
incl %r15d
addq $0x18, %rcx
jmp 0x646ff
cmpl %r15d, 0x1c(%r12)
jle 0x64764
movl %r15d, %eax
leaq (%rax,%rax,2), %rax
leaq (%r12,%rax,8), %r13
addq $0x30, %r13
xorl %ebx, %ebx
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
incl %r15d
incl %ebx
addq $0x18, %r13
cmpl %r15d, 0x1c(%r12)
jg 0x64729
movq %r12, %rdi
callq 0x66d47
xorl %r14d, %r14d
jmp 0x64891
leaq 0x118ad5(%rip), %rcx    # 0x17d250
movl $0x2, %eax
cmpb $0x0, 0x1b(%rcx)
jne 0x6463e
xorl %edi, %edi
jmp 0x64793
movb $0x1, %dil
xorl %eax, %eax
cmpl $0x2, 0x3fdf2(%rip)     # 0xa458c
jl 0x647d1
movq (%r12), %rsi
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
testb %dil, %dil
je 0x647b8
leaq 0xeae7(%rip), %r8       # 0x7329d
jmp 0x647c3
leaq 0x2d371(%rip), %rdi     # 0x91b30
movq (%rdi,%rax,8), %r8
leaq 0x16670(%rip), %rdi     # 0x7ae3a
xorl %eax, %eax
callq 0x24cb4
movq %r12, %rdi
callq 0x66d47
movl %r14d, 0x30(%rsp)
movq %r15, %rdi
xorl %esi, %esi
callq 0x1ff50
testl %eax, %eax
js 0x6487b
movl %eax, %r14d
movl %r13d, 0x2c(%rsp)
movl %eax, %edi
callq 0x204d2
movl %eax, %r13d
movslq %eax, %rdi
callq 0x69a0
testq %rax, %rax
je 0x64883
movq %rax, %r12
movq %rbx, 0x38(%rsp)
movl %r14d, %edi
movq %rax, %rsi
movl %r13d, %edx
callq 0x201c2
movl %eax, %ebx
movl %r14d, %edi
callq 0x20671
movl $0xfffffffd, %r14d      # imm = 0xFFFFFFFD
cmpl %r13d, %ebx
jne 0x64891
leaq 0x18(%rsp), %rdx
leaq 0x1c(%rsp), %rbx
movq %r12, %rdi
movl %r13d, %esi
movq %rbx, %rcx
callq 0x40168
movl -0x4(%rbx), %ecx
movl (%rbx), %eax
testl %ecx, %ecx
sete %dl
testl %eax, %eax
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x648a3
movq %r12, %rdi
callq 0x61c0
movl $0xfffffffc, %r14d      # imm = 0xFFFFFFFC
jmp 0x64891
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x64891
movl %r14d, %edi
callq 0x20671
movl $0xfffffffe, %r14d      # imm = 0xFFFFFFFE
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1189a6(%rip), %rdx    # 0x17d250
cmpb $0x0, 0x1d(%rdx)
setne %dl
movl 0x2c(%rsp), %esi
notb %sil
testb %dl, %sil
movl %ebp, 0x34(%rsp)
jne 0x648e6
movl $0x1, %esi
movl %esi, %edx
leal (%rdx,%rdx), %esi
cmpl %ecx, %edx
jl 0x648c6
movl %edx, 0x10(%rsp)
movl $0x1, %ecx
movl %ecx, %r14d
leal (%r14,%r14), %ecx
cmpl %eax, %r14d
jl 0x648d8
jmp 0x648ed
movl %ecx, 0x10(%rsp)
movl %eax, %r14d
movl %r14d, 0x14(%rsp)
movl 0x10(%rsp), %ecx
movl %r14d, %eax
movq %rcx, 0x40(%rsp)
imull %ecx, %eax
movslq %eax, %rbx
shlq $0x2, %rbx
movq %rbx, %rdi
callq 0x69a0
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x6488b
movq %rax, %rbp
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x62f0
movq 0x40(%rsp), %r8
leal (,%r8,4), %ecx
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rdx
movl %r14d, %r9d
xorl %eax, %eax
pushq %rax
pushq %rax
callq 0x403fd
addq $0x10, %rsp
movl %eax, %ebx
movq %r12, %rdi
callq 0x61c0
testl %ebx, %ebx
je 0x64977
movq 0x8(%rsp), %rdi
callq 0x61c0
movl $0xfffffffb, %r14d      # imm = 0xFFFFFFFB
jmp 0x64891
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movl 0x30(%rsp), %r14d
movl %r14d, %esi
callq 0x64b69
movq 0x38(%rsp), %rcx
movl $0x0, 0x4(%rcx)
xorl %eax, %eax
cmpl $0x0, 0x1c(%rbx)
movq %rcx, %rbx
setne %al
shll $0x3, %eax
movl %eax, 0x4(%rcx)
movl 0x34(%rsp), %ebp
movl %ebp, %eax
andl $0x5, %eax
cmpl $0x1, %eax
movl 0x2c(%rsp), %r13d
je 0x64a3a
movl 0x10(%rsp), %eax
cmpl 0x18(%rsp), %eax
jle 0x64a03
cmpl $0x0, 0x1c(%rsp)
jle 0x64a03
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
movslq 0x18(%rsp), %rax
leaq (%r14,%rax,4), %rdi
movl 0x10(%rsp), %ecx
subl %eax, %ecx
shll $0x2, %ecx
movslq %ecx, %rdx
movq %r14, %rsi
callq 0x60c0
incl %ebx
movslq 0x10(%rsp), %rax
leaq (%r14,%rax,4), %r14
cmpl 0x1c(%rsp), %ebx
jl 0x649d5
movl 0x14(%rsp), %eax
movl 0x1c(%rsp), %ecx
subl %ecx, %eax
movq 0x38(%rsp), %rbx
movl 0x30(%rsp), %r14d
jle 0x64a3a
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
imull %edx, %ecx
movslq %ecx, %rcx
leaq (%rsi,%rcx,4), %rdi
imull %edx, %eax
shll $0x2, %eax
movslq %eax, %rdx
callq 0x60c0
movl $0x80e1, 0x20(%rsp)     # imm = 0x80E1
leaq 0x118807(%rip), %rax    # 0x17d250
cmpb $0x0, 0x18(%rax)
jne 0x64a89
movl 0x14(%rsp), %eax
imull 0x10(%rsp), %eax
testl %eax, %eax
jle 0x64a81
movl %eax, %eax
incq %rax
movq 0x8(%rsp), %rcx
movb -0x8(%rcx,%rax,4), %dl
movb -0x6(%rcx,%rax,4), %sil
movb %dl, -0x6(%rcx,%rax,4)
movb %sil, -0x8(%rcx,%rax,4)
decq %rax
cmpq $0x1, %rax
jg 0x64a61
movl $0x1908, 0x20(%rsp)     # imm = 0x1908
movl 0x18(%rsp), %eax
movl %eax, 0x10(%rbx)
movl 0x1c(%rsp), %eax
movl %eax, 0x14(%rbx)
movl 0x10(%rsp), %eax
movl %eax, 0x8(%rbx)
movl 0x14(%rsp), %edx
movl %edx, 0xc(%rbx)
xorl %r12d, %r12d
testb %r13b, %r13b
je 0x64af8
movl %eax, %esi
movl %r12d, %ecx
sarl %cl, %esi
cmpl $0x2, %esi
setge %sil
movl %edx, %edi
sarl %cl, %edi
cmpl $0x2, %edi
setge %cl
orb %sil, %cl
incl %r12d
testb %cl, %cl
jne 0x64aad
movl %r12d, %edi
callq 0x66d93
movq %rax, %r12
movq %r15, %rdi
callq 0x6060
movq %rax, (%r12)
movl %r14d, 0x8(%r12)
movl 0x4(%rbx), %eax
orl %ebp, %eax
andl $0x9, %eax
movl %eax, 0xc(%r12)
movzwl %bp, %esi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movq %r12, %rcx
callq 0x64d3d
testb %r13b, %r13b
je 0x64b45
cmpl $0x2, 0x3fa75(%rip)     # 0xa458c
jl 0x64b35
movq (%r12), %rsi
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
leaq 0x16274(%rip), %rdi     # 0x7ada2
xorl %eax, %eax
callq 0x24cb4
movq %r12, %rdi
callq 0x66dcc
movq %r12, %rdi
callq 0x66d47
movq 0x8(%rsp), %rdi
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x64891
callq 0x61c0
movq $0x0, 0x8(%rsp)
jmp 0x64891