// File: /workspace/github_c_makefile_star2_O3/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, %r13d
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, %r15
xorl %r12d, %r12d
testb $0x10, %bl
jne 0x66549
leaq 0x3f266(%rip), %rax     # 0xa5588
cmpl $0x0, (%rax)
je 0x66549
leaq 0x3f24e(%rip), %rax     # 0xa5580
cmpl $0x0, (%rax)
je 0x66549
movl %ebx, %ebp
andl $0x1, %ebp
movq %r15, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x68a40
testl %eax, %eax
je 0x6647c
movq %r15, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x687a1
testq %rax, %rax
je 0x66546
movq %rax, %r12
movl 0x10(%rax), %r9d
cmpl $0x83f3, %r9d           # imm = 0x83F3
je 0x664e6
cmpl $0x83f1, %r9d           # imm = 0x83F1
jne 0x66512
leaq 0x117ebd(%rip), %rcx    # 0x17e250
movl $0x1, %eax
cmpb $0x0, 0x1a(%rcx)
je 0x664fc
cmpl $0x2, 0x3f1e3(%rip)     # 0xa558c
jl 0x663d2
movq (%r12), %rsi
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
leaq 0x2c7a0(%rip), %rdi     # 0x92b60
movq (%rdi,%rax,8), %r8
leaq 0x15fac(%rip), %rdi     # 0x7c377
xorl %eax, %eax
callq 0x25221
movl (%r14), %esi
testl %esi, %esi
jne 0x663f1
leaq 0x594360(%rip), %rax    # 0x5fa740
movl $0x1, %edi
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 0x594318(%rip), %r14    # 0x5fa740
movl $0xde1, %edi            # imm = 0xDE1
callq *0xc8(%r14)
leaq 0x594561(%rip), %rax    # 0x5fa99c
movl (%rax), %r15d
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
testb $0x20, %bl
cmovnel %eax, %r15d
leaq 0x594543(%rip), %rax    # 0x5fa998
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 0x66473
cmpl %eax, (%rcx)
jle 0x66484
incl %r15d
addq $0x18, %rcx
jmp 0x6646a
movb $0x1, %r12b
jmp 0x66549
cmpl %r15d, 0x1c(%r12)
jle 0x664d7
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 0x6649c
movq %r12, %rdi
callq 0x68a61
xorl %ebp, %ebp
jmp 0x665fa
leaq 0x117d63(%rip), %rcx    # 0x17e250
movl $0x2, %eax
cmpb $0x0, 0x1b(%rcx)
jne 0x663a2
cmpl $0x1, 0x3f089(%rip)     # 0xa558c
jle 0x6653e
leaq 0x2c654(%rip), %rcx     # 0x92b60
movq (%rcx,%rax,8), %r8
jmp 0x66522
cmpl $0x1, 0x3f073(%rip)     # 0xa558c
jle 0x6653e
leaq 0xe03f(%rip), %r8       # 0x74561
movq (%r12), %rsi
movl 0x8(%r12), %edx
movl 0xc(%r12), %ecx
leaq 0x15def(%rip), %rdi     # 0x7c326
xorl %eax, %eax
callq 0x25221
movq %r12, %rdi
callq 0x68a61
xorl %r12d, %r12d
movq %r15, %rdi
xorl %esi, %esi
callq 0x2043c
testl %eax, %eax
js 0x665e7
movl %eax, %ebp
movl %r12d, 0x30(%rsp)
movl %r13d, 0x34(%rsp)
movl %eax, %edi
callq 0x209a1
movl %eax, %r13d
movslq %eax, %rdi
callq 0x69a0
testq %rax, %rax
je 0x665ee
movq %rax, %r12
movq %r14, 0x28(%rsp)
movl %ebp, %edi
movq %rax, %rsi
movl %r13d, %edx
callq 0x20684
movl %eax, %r14d
movl %ebp, %edi
callq 0x20b40
movl $0xfffffffd, %ebp       # imm = 0xFFFFFFFD
cmpl %r13d, %r14d
jne 0x665fa
leaq 0x18(%rsp), %rdx
leaq 0x1c(%rsp), %rbp
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rcx
callq 0x41560
movl -0x4(%rbp), %r14d
movl (%rbp), %eax
testl %r14d, %r14d
sete %cl
testl %eax, %eax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x6660b
movq %r12, %rdi
callq 0x61c0
movl $0xfffffffc, %ebp       # imm = 0xFFFFFFFC
jmp 0x665fa
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x665fa
movl %ebp, %edi
callq 0x20b40
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x117c3e(%rip), %rcx    # 0x17e250
cmpb $0x0, 0x1d(%rcx)
setne %cl
movl 0x30(%rsp), %edx
notb %dl
testb %cl, %dl
jne 0x6664d
movl $0x1, %edx
movl %edx, %ecx
leal (%rcx,%rcx), %edx
cmpl %r14d, %ecx
jl 0x66628
movl %ecx, 0x10(%rsp)
movl $0x1, %edx
movl %edx, %ebp
leal (,%rbp,2), %edx
cmpl %eax, %ebp
jl 0x6663b
movl %ecx, %r14d
jmp 0x66654
movl %r14d, 0x10(%rsp)
movl %eax, %ebp
movl %ebp, 0x14(%rsp)
movl %ebp, %eax
imull %r14d, %eax
movslq %eax, %rsi
shlq $0x2, %rsi
movl $0x1, %edi
callq 0x6930
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x665f5
leal (,%r14,4), %ecx
xorl %r10d, %r10d
movq %r12, %rdi
movl %r13d, %esi
movq %rax, %rdx
movl %r14d, %r8d
movl %ebp, %r9d
pushq %r10
pushq %r10
callq 0x417cd
addq $0x10, %rsp
movl %eax, %ebp
movq %r12, %rdi
callq 0x61c0
testl %ebp, %ebp
je 0x666c6
movq 0x8(%rsp), %rdi
callq 0x61c0
movl $0xfffffffb, %ebp       # imm = 0xFFFFFFFB
jmp 0x665fa
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movl 0x34(%rsp), %r12d
movl %r12d, %esi
callq 0x66897
xorl %eax, %eax
cmpl $0x0, 0x1c(%r14)
setne %al
shll $0x3, %eax
movq 0x28(%rsp), %rcx
movl %eax, 0x4(%rcx)
movl %ebx, %ecx
andl $0x5, %ecx
movl 0x8(%r14), %eax
cmpl $0x1, %ecx
je 0x66774
cmpl 0x18(%rsp), %eax
setg %dl
movl 0x1c(%rsp), %ecx
testl %ecx, %ecx
setg %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x6674b
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
movslq 0x18(%rsp), %rcx
leaq (%r14,%rcx,4), %rdi
subl %ecx, %eax
shll $0x2, %eax
movslq %eax, %rdx
movq %r14, %rsi
callq 0x60c0
incl %ebp
movslq 0x10(%rsp), %rax
leaq (%r14,%rax,4), %r14
movl 0x1c(%rsp), %ecx
cmpl %ecx, %ebp
jl 0x6671f
movl 0x14(%rsp), %edx
subl %ecx, %edx
jle 0x66774
movq 0x8(%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 0x10(%rsp), %eax
movq 0x28(%rsp), %rdi
leaq 0x117ad0(%rip), %rcx    # 0x17e250
movl $0x80e1, 0x20(%rsp)     # imm = 0x80E1
cmpb $0x0, 0x18(%rcx)
movl 0x14(%rsp), %edx
jne 0x667d0
movl %edx, %ecx
imull %eax, %ecx
testl %ecx, %ecx
jle 0x667c8
movl %ecx, %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
ja 0x667a0
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %edx
movl $0x1908, 0x20(%rsp)     # imm = 0x1908
movq 0x18(%rsp), %rcx
movq %rcx, 0x10(%rdi)
movl %eax, 0x8(%rdi)
movl %edx, 0xc(%rdi)
cmpb $0x0, 0x30(%rsp)
je 0x6686e
xorl %ecx, %ecx
movl %eax, %edi
sarl %cl, %edi
movl %edx, %esi
sarl %cl, %esi
incl %ecx
cmpl $0x1, %edi
jg 0x667ec
cmpl $0x1, %esi
jg 0x667ec
movl %ecx, %edi
callq 0x68ab0
movq %rax, %r14
movq %r15, %rdi
callq 0x6060
movq %rax, (%r14)
movl %r12d, 0x8(%r14)
movq 0x28(%rsp), %rdi
movl 0x4(%rdi), %eax
orl %ebx, %eax
andl $0x9, %eax
movl %eax, 0xc(%r14)
movzwl %bx, %esi
leaq 0x8(%rsp), %rdx
movq %r14, %rcx
callq 0x66a6c
cmpl $0x2, 0x3ed4b(%rip)     # 0xa558c
jl 0x6685c
movq (%r14), %rsi
movl 0x8(%r14), %edx
movl 0xc(%r14), %ecx
leaq 0x15a39(%rip), %rdi     # 0x7c28e
xorl %eax, %eax
callq 0x25221
movq %r14, %rdi
callq 0x68acf
movq %r14, %rdi
callq 0x68a61
jmp 0x6687d
movzwl %bx, %esi
leaq 0x8(%rsp), %rdx
xorl %ecx, %ecx
callq 0x66a6c
movq 0x8(%rsp), %rdi
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x665fa
callq 0x61c0
jmp 0x665fa