// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl -0x1c(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x98c2e
leaq 0x48fcf(%rip), %rax     # 0xe1bc8
cmpl $0x0, (%rax)
je 0x98c2e
leaq 0x48fbb(%rip), %rax     # 0xe1bc0
cmpl $0x0, (%rax)
je 0x98c2e
movq -0x10(%rbp), %rdi
movl -0x20(%rbp), %esi
movl -0x1c(%rbp), %edx
andl $0x1, %edx
callq 0x9c960
movl %eax, -0x68(%rbp)
cmpl $0x0, -0x68(%rbp)
jne 0x98c2c
movl $0x1, -0x64(%rbp)
jmp 0x98c2e
cmpl $0x0, -0x68(%rbp)
je 0x98c5a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movl -0x20(%rbp), %ecx
callq 0x99100
cmpl $0x0, %eax
jne 0x98c58
movl $0x0, -0x4(%rbp)
jmp 0x990e7
jmp 0x98c5a
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x2fee0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0x98c7a
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x990e7
movl -0x44(%rbp), %edi
callq 0x30750
movl %eax, -0x48(%rbp)
movslq -0x48(%rbp), %rdi
callq 0x68f0
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x98cad
movl -0x44(%rbp), %edi
callq 0x30960
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x990e7
movl -0x44(%rbp), %edi
movq -0x58(%rbp), %rsi
movl -0x48(%rbp), %edx
callq 0x302b0
cmpl -0x48(%rbp), %eax
je 0x98cd5
movl -0x44(%rbp), %edi
callq 0x30960
movl $0xfffffffd, -0x4(%rbp) # imm = 0xFFFFFFFD
jmp 0x990e7
movl -0x44(%rbp), %edi
callq 0x30960
movq -0x58(%rbp), %rdi
movl -0x48(%rbp), %esi
leaq -0x40(%rbp), %rdx
addq $0x10, %rdx
leaq -0x40(%rbp), %rcx
addq $0x14, %rcx
callq 0x61c40
cmpl $0x0, -0x30(%rbp)
je 0x98d05
cmpl $0x0, -0x2c(%rbp)
jne 0x98d1a
movq -0x58(%rbp), %rdi
callq 0x61c0
movl $0xfffffffc, -0x4(%rbp) # imm = 0xFFFFFFFC
jmp 0x990e7
leaq 0x12208f(%rip), %rax    # 0x1badb0
cmpb $0x0, 0x1d(%rax)
je 0x98d2d
cmpl $0x0, -0x64(%rbp)
je 0x98d67
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0x98d49
jmp 0x98d3e
movl -0x38(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x98d34
movl $0x1, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x98d65
jmp 0x98d5a
movl -0x34(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x98d50
jmp 0x98d73
movl -0x30(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
imull -0x34(%rbp), %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x68f0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x98d9d
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
jmp 0x990e7
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %eax
imull -0x34(%rbp), %eax
movslq %eax, %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x62d0
movq -0x58(%rbp), %rdi
movl -0x48(%rbp), %esi
movq -0x40(%rbp), %rdx
movslq -0x38(%rbp), %rax
shlq $0x2, %rax
movl %eax, %ecx
movl -0x38(%rbp), %r8d
movl -0x34(%rbp), %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x62120
cmpl $0x0, %eax
je 0x98e0c
movq -0x58(%rbp), %rdi
callq 0x61c0
movq -0x40(%rbp), %rdi
callq 0x61c0
movl $0xfffffffb, -0x4(%rbp) # imm = 0xFFFFFFFB
jmp 0x990e7
movq -0x58(%rbp), %rdi
callq 0x61c0
movq $0x0, -0x58(%rbp)
movl -0x20(%rbp), %esi
leaq -0x40(%rbp), %rdi
callq 0x99490
movq -0x18(%rbp), %rax
movl $0x0, 0x4(%rax)
cmpl $0x0, -0x24(%rbp)
je 0x98e47
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
orl $0x8, %ecx
movl %ecx, 0x4(%rax)
movl -0x1c(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x98e61
movl -0x1c(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x98efd
movl -0x38(%rbp), %eax
cmpl -0x30(%rbp), %eax
jle 0x98ec6
movq -0x40(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x98ec4
movq -0x70(%rbp), %rdi
movslq -0x30(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq -0x70(%rbp), %rsi
movl -0x38(%rbp), %eax
subl -0x30(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdx
callq 0x60c0
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
movl -0x38(%rbp), %ecx
movq -0x70(%rbp), %rax
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
jmp 0x98e78
jmp 0x98ec6
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0x98efb
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %eax
imull -0x2c(%rbp), %eax
cltq
shlq $0x2, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movl -0x34(%rbp), %eax
subl -0x2c(%rbp), %eax
imull -0x38(%rbp), %eax
shll $0x2, %eax
movslq %eax, %rdx
callq 0x60c0
jmp 0x98efd
movl $0x80e1, -0x28(%rbp)    # imm = 0x80E1
leaq 0x121ea5(%rip), %rax    # 0x1badb0
cmpb $0x0, 0x18(%rax)
jne 0x98f5d
movl -0x38(%rbp), %eax
imull -0x34(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jl 0x98f56
movq -0x40(%rbp), %rdi
movslq -0x74(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
movq -0x40(%rbp), %rsi
movslq -0x74(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
addq $0x2, %rsi
callq 0x997c0
movl -0x74(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x98f1e
movl $0x1908, -0x28(%rbp)    # imm = 0x1908
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x14(%rax)
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0xc(%rax)
cmpl $0x0, -0x64(%rbp)
je 0x99065
movl $0x0, -0x78(%rbp)
movl -0x38(%rbp), %eax
movl -0x78(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jle 0x98fb3
movl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x98fbe
movl -0x38(%rbp), %eax
movl -0x78(%rbp), %ecx
sarl %cl, %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %ecx
movb $0x1, %al
cmpl $0x1, %ecx
movb %al, -0x7d(%rbp)
jg 0x99008
movl -0x34(%rbp), %eax
movl -0x78(%rbp), %ecx
sarl %cl, %eax
movl %eax, %ecx
movl $0x1, %eax
cmpl %ecx, %eax
jle 0x98feb
movl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x98ff9
movl -0x34(%rbp), %eax
movl -0x78(%rbp), %ecx
sarl %cl, %eax
movl %eax, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl $0x1, %eax
setg %al
movb %al, -0x7d(%rbp)
movb -0x7d(%rbp), %al
testb $0x1, %al
jne 0x99011
jmp 0x9901f
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x98f96
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %edi
callq 0x9ca40
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6060
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, (%rax)
movl -0x20(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x1c(%rbp), %ecx
movq -0x18(%rbp), %rax
orl 0x4(%rax), %ecx
andl $0x9, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %eax
movq -0x60(%rbp), %rcx
leaq -0x40(%rbp), %rdx
movzwl %ax, %esi
callq 0x997f0
cmpl $0x0, -0x64(%rbp)
je 0x990c8
cmpl $0x2, 0x48b47(%rip)     # 0xe1bd0
jl 0x990ae
movq -0x60(%rbp), %rax
movq (%rax), %rsi
movq -0x60(%rbp), %rax
movl 0x8(%rax), %edx
movq -0x60(%rbp), %rax
movl 0xc(%rax), %ecx
leaq 0x1ee93(%rip), %rdi     # 0xb7f3a
movb $0x0, %al
callq 0x37c90
movq -0x60(%rbp), %rdi
callq 0x9ca90
movq -0x60(%rbp), %rdi
callq 0x9c9b0
movq $0x0, -0x60(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x990e0
movq -0x40(%rbp), %rdi
callq 0x61c0
movq $0x0, -0x40(%rbp)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)