// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/kplib.c
// Function: ktgarend
static int ktgarend (const char *header, int fleng,
	intptr_t daframeplace, int dabytesperline, int daxres, int dayres,
	int daglobxoffs, int daglobyoffs)
{
	int i=0, x, y, pi, xi, yi, x0, x1, y0, y1, xsiz, ysiz, rlestat, colbyte, pixbyte;
	intptr_t p;
	const unsigned char *fptr, *cptr=0, *nptr;

		//Ugly and unreliable identification for .TGA!
	if ((fleng < 20) || (header[1]&0xfe)) return(-1);
	if ((header[2] >= 12) || (!((1<<header[2])&0xe0e))) return(-1);
	if ((header[16]&7) || (header[16] == 0) || (header[16] > 32)) return(-1);
	if (header[17]&0xc0) return(-1);

	fptr = (unsigned char *)&header[header[0]+18];
	xsiz = (int)SSWAPIB(*(unsigned short *)&header[12]); if (xsiz <= 0) return(-1);
	ysiz = (int)SSWAPIB(*(unsigned short *)&header[14]); if (ysiz <= 0) return(-1);
	colbyte = ((((int)header[16])+7)>>3);

	if (header[1] == 1)
	{
		pixbyte = ((((int)header[7])+7)>>3);
		cptr = &fptr[-SSWAPIB(*(unsigned short *)&header[3])*pixbyte];
		fptr += SSWAPIB(*(unsigned short *)&header[5])*pixbyte;
	} else pixbyte = colbyte;

	switch(pixbyte) //For PNGOUT
	{
		case 1: coltype = 0; bitdepth = 8; palcol[0] = LSWAPIB(0xff000000);
				  for(i=1;i<256;i++) palcol[i] = palcol[i-1]+LSWAPIB(0x10101);
				  break;
		case 2: case 3: coltype = 2; break;
		case 4: coltype = 6; break;
	}

	if (!(header[17]&16)) { x0 = 0;      x1 = xsiz; xi = 1; }
						  else { x0 = xsiz-1; x1 = -1;   xi =-1; }
	if (header[17]&32) { y0 = 0;      y1 = ysiz; yi = 1; pi = dabytesperline; }
					  else { y0 = ysiz-1; y1 = -1;   yi =-1; pi =-dabytesperline; }
	x0 += daglobxoffs; y0 += daglobyoffs;
	x1 += daglobxoffs; y1 += daglobyoffs;
	if (header[2] < 8) rlestat = -2; else rlestat = -1;

	p = y0*dabytesperline+daframeplace;
	for(y=y0;y!=y1;y+=yi,p+=pi)
		for(x=x0;x!=x1;x+=xi)
		{
			if (rlestat < 128)
			{
				if ((rlestat&127) == 127) { rlestat = (int)fptr[0]; fptr++; }
				if (header[1] == 1)
				{
					if (colbyte == 1) i = fptr[0];
									 else i = (int)SSWAPIB(*(unsigned short *)&fptr[0]);
					nptr = &cptr[i*pixbyte];
				} else nptr = fptr;

				switch(pixbyte)
				{
					case 1: i = palcol[(int)nptr[0]]; break;
					case 2: i = (int)SSWAPIB(*(unsigned short *)&nptr[0]);
						i = LSWAPIB(((i&0x7c00)<<9) + ((i&0x03e0)<<6) + ((i&0x001f)<<3) + 0xff000000);
						break;
					case 3: i = (*(int *)&nptr[0]) | LSWAPIB(0xff000000); break;
					case 4: i = (*(int *)&nptr[0]); break;
				}
				fptr += colbyte;
			}
			if (rlestat >= 0) rlestat--;

			if (((unsigned int)x < (unsigned int)daxres) && ((unsigned int)y < (unsigned int)dayres))
				*(int *)(x*4+p) = i;
		}
	return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x18, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movq $0x0, -0x80(%rbp)
cmpl $0x14, -0x14(%rbp)
jl 0x678ab
movq -0x10(%rbp), %rax
movsbl 0x1(%rax), %eax
andl $0xfe, %eax
cmpl $0x0, %eax
je 0x678b7
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x67d29
movq -0x10(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0xc, %eax
jge 0x678dd
movq -0x10(%rbp), %rax
movsbl 0x2(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
andl $0xe0e, %eax            # imm = 0xE0E
cmpl $0x0, %eax
jne 0x678e9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x67d29
movq -0x10(%rbp), %rax
movsbl 0x10(%rax), %eax
andl $0x7, %eax
cmpl $0x0, %eax
jne 0x67913
movq -0x10(%rbp), %rax
movsbl 0x10(%rax), %eax
cmpl $0x0, %eax
je 0x67913
movq -0x10(%rbp), %rax
movsbl 0x10(%rax), %eax
cmpl $0x20, %eax
jle 0x6791f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x67d29
movq -0x10(%rbp), %rax
movsbl 0x11(%rax), %eax
andl $0xc0, %eax
cmpl $0x0, %eax
je 0x6793d
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x67d29
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movsbl (%rcx), %ecx
addl $0x12, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movzwl 0xc(%rax), %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jg 0x67972
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x67d29
movq -0x10(%rbp), %rax
movzwl 0xe(%rax), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x5c(%rbp)
jg 0x6798f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x67d29
movq -0x10(%rbp), %rax
movsbl 0x10(%rax), %eax
addl $0x7, %eax
sarl $0x3, %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x1, %eax
jne 0x679f8
movq -0x10(%rbp), %rax
movsbl 0x7(%rax), %eax
addl $0x7, %eax
sarl $0x3, %eax
movl %eax, -0x68(%rbp)
movq -0x78(%rbp), %rax
movq -0x10(%rbp), %rcx
movzwl 0x3(%rcx), %edx
xorl %ecx, %ecx
subl %edx, %ecx
imull -0x68(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x5(%rax), %ecx
imull -0x68(%rbp), %ecx
movq -0x78(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
jmp 0x679fe
movl -0x64(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movl %eax, -0x8c(%rbp)
subl $0x1, %eax
je 0x67a2b
jmp 0x67a0e
movl -0x8c(%rbp), %eax
addl $-0x2, %eax
subl $0x2, %eax
jb 0x67a87
jmp 0x67a1e
movl -0x8c(%rbp), %eax
subl $0x4, %eax
je 0x67a90
jmp 0x67a97
movb $0x0, 0x55b34e(%rip)    # 0x5c2d80
movb $0x8, 0x55b348(%rip)    # 0x5c2d81
movl $0xff000000, 0x55af3d(%rip) # imm = 0xFF000000
movl $0x1, -0x30(%rbp)
cmpl $0x100, -0x30(%rbp)     # imm = 0x100
jge 0x67a85
movl -0x30(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rcx
leaq 0x55af1d(%rip), %rax    # 0x5c2980
movl (%rax,%rcx,4), %edx
addl $0x10101, %edx          # imm = 0x10101
movslq -0x30(%rbp), %rcx
leaq 0x55af09(%rip), %rax    # 0x5c2980
movl %edx, (%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x67a4a
jmp 0x67a97
movb $0x2, 0x55b2f2(%rip)    # 0x5c2d80
jmp 0x67a97
movb $0x6, 0x55b2e9(%rip)    # 0x5c2d80
movq -0x10(%rbp), %rax
movsbl 0x11(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
jne 0x67abd
movl $0x0, -0x48(%rbp)
movl -0x58(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl $0x1, -0x40(%rbp)
jmp 0x67ad4
movl -0x58(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x40(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movsbl 0x11(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x67b00
movl $0x0, -0x50(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl $0x1, -0x44(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0x67b1f
movl -0x5c(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x50(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x44(%rbp) # imm = 0xFFFFFFFF
xorl %eax, %eax
subl -0x24(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl 0x10(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x48(%rbp)
movl 0x18(%rbp), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movl 0x10(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl 0x18(%rbp), %eax
addl -0x54(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x8, %eax
jge 0x67b59
movl $0xfffffffe, -0x60(%rbp) # imm = 0xFFFFFFFE
jmp 0x67b60
movl $0xffffffff, -0x60(%rbp) # imm = 0xFFFFFFFF
movl -0x50(%rbp), %eax
imull -0x24(%rbp), %eax
cltq
addq -0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movl -0x50(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x54(%rbp), %eax
je 0x67d22
movl -0x48(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0x67d06
cmpl $0x80, -0x60(%rbp)
jge 0x67cc6
movl -0x60(%rbp), %eax
andl $0x7f, %eax
cmpl $0x7f, %eax
jne 0x67bc3
movq -0x78(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x1, %eax
jne 0x67c06
cmpl $0x1, -0x64(%rbp)
jne 0x67be2
movq -0x78(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x30(%rbp)
jmp 0x67bec
movq -0x78(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x80(%rbp), %rax
movl -0x30(%rbp), %ecx
imull -0x68(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
jmp 0x67c11
movq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movl -0x68(%rbp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x98(%rbp)
subl $0x3, %eax
ja 0x67cb5
movq -0x98(%rbp), %rax
leaq 0x4bb4e(%rip), %rcx     # 0xb3784
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x88(%rbp), %rax
movzbl (%rax), %eax
movslq %eax, %rcx
leaq 0x55ad2d(%rip), %rax    # 0x5c2980
movl (%rax,%rcx,4), %eax
movl %eax, -0x30(%rbp)
jmp 0x67cb5
movq -0x88(%rbp), %rax
movzwl (%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
andl $0x7c00, %eax           # imm = 0x7C00
shll $0x9, %eax
movl -0x30(%rbp), %ecx
andl $0x3e0, %ecx            # imm = 0x3E0
shll $0x6, %ecx
addl %ecx, %eax
movl -0x30(%rbp), %ecx
andl $0x1f, %ecx
shll $0x3, %ecx
addl %ecx, %eax
addl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, -0x30(%rbp)
jmp 0x67cb5
movq -0x88(%rbp), %rax
movl (%rax), %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, -0x30(%rbp)
jmp 0x67cb5
movq -0x88(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x64(%rbp), %ecx
movq -0x78(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
cmpl $0x0, -0x60(%rbp)
jl 0x67cd5
movl -0x60(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x60(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x67cf6
movl -0x38(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x67cf6
movl -0x30(%rbp), %ecx
movl -0x34(%rbp), %eax
shll $0x2, %eax
cltq
addq -0x70(%rbp), %rax
movl %ecx, (%rax)
jmp 0x67cf8
movl -0x40(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x67b89
jmp 0x67d08
movl -0x44(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movslq -0x3c(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x67b77
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x18, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)