// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %r9, -0x18(%rsp)
movl %ecx, -0x2c(%rsp)
movb 0x1(%rdi), %cl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x1, %cl
ja 0x473e7
movsbl 0x2(%rdi), %r10d
cmpl $0xb, %r10d
jg 0x473e7
movl $0xe0e, %r9d            # imm = 0xE0E
btl %r10d, %r9d
jae 0x473e7
movsbl 0x10(%rdi), %r10d
cmpl $0x20, %r10d
jg 0x473e7
testb %r10b, %r10b
je 0x473e7
movl %r10d, %r9d
andl $0x7, %r9d
jne 0x473e7
movb 0x11(%rdi), %r13b
cmpb $0x3f, %r13b
ja 0x473e7
movzwl 0xc(%rdi), %r15d
testl %r15d, %r15d
je 0x473e7
movzwl 0xe(%rdi), %r14d
testl %r14d, %r14d
je 0x473e7
movsbq (%rdi), %r9
leaq (%rdi,%r9), %rbx
addq $0x12, %rbx
movl %r10d, %eax
sarl $0x3, %eax
xorl %r12d, %r12d
cmpb $0x1, %cl
jne 0x47081
movsbl 0x7(%rdi), %ebp
addl $0x7, %ebp
sarl $0x3, %ebp
movzwl 0x3(%rdi), %ecx
movslq %ebp, %r9
imulq %r9, %rcx
movq %rbx, %r11
subq %rcx, %r11
movq %r11, -0x20(%rsp)
movzwl 0x5(%rdi), %ecx
imulq %r9, %rcx
addq %rcx, %rbx
jmp 0x4708c
movl %eax, %ebp
movq $0x0, -0x20(%rsp)
leal -0x2(%rbp), %ecx
cmpl $0x2, %ecx
movl %eax, -0x38(%rsp)
jb 0x471eb
cmpl $0x4, %ebp
je 0x471f4
cmpl $0x1, %ebp
jne 0x471fb
movb $0x0, 0x589d2b(%rip)    # 0x5d0de0
movb $0x8, 0x589d25(%rip)    # 0x5d0de1
movl $0xff000000, 0x58991a(%rip) # imm = 0xFF000000
movdqa 0x30802(%rip), %xmm0    # 0x778d0
xorl %eax, %eax
movdqa 0x2cff8(%rip), %xmm1    # 0x740d0
movdqa 0x2d000(%rip), %xmm2    # 0x740e0
movdqa 0x2d008(%rip), %xmm3    # 0x740f0
movdqa 0x30770(%rip), %xmm4    # 0x77860
pshufd $0xf5, %xmm4, %xmm5     # xmm5 = xmm4[1,1,3,3]
movdqa 0x307e3(%rip), %xmm6    # 0x778e0
leaq 0x5898dc(%rip), %rcx    # 0x5d09e0
movdqa 0x307e4(%rip), %xmm7    # 0x778f0
movq %rax, %xmm8
pshufd $0x44, %xmm8, %xmm9     # xmm9 = xmm8[0,1,0,1]
movdqa %xmm9, %xmm8
por %xmm2, %xmm8
pxor %xmm3, %xmm8
movdqa %xmm4, %xmm11
pcmpgtd %xmm8, %xmm11
pshufd $0xf5, %xmm8, %xmm10    # xmm10 = xmm8[1,1,3,3]
pcmpeqd %xmm5, %xmm10
pand %xmm11, %xmm10
movdqa %xmm0, %xmm8
paddd %xmm6, %xmm8
movd %xmm10, %r9d
testb $0x1, %r9b
je 0x4715c
movd %xmm8, 0x4(%rcx,%rax,4)
pextrw $0x4, %xmm10, %r9d
testb $0x1, %r9b
je 0x47175
pshufd $0x55, %xmm8, %xmm10    # xmm10 = xmm8[1,1,1,1]
movd %xmm10, 0x8(%rcx,%rax,4)
por %xmm1, %xmm9
pxor %xmm3, %xmm9
movdqa %xmm4, %xmm10
pcmpgtd %xmm9, %xmm10
pshufd $0xf5, %xmm9, %xmm9     # xmm9 = xmm9[1,1,3,3]
pcmpeqd %xmm5, %xmm9
pand %xmm10, %xmm9
pextrw $0x0, %xmm9, %r9d
testb $0x1, %r9b
je 0x471b2
pshufd $0xee, %xmm8, %xmm10    # xmm10 = xmm8[2,3,2,3]
movd %xmm10, 0xc(%rcx,%rax,4)
pextrw $0x4, %xmm9, %r9d
testb $0x1, %r9b
je 0x471cb
pshufd $0xff, %xmm8, %xmm8     # xmm8 = xmm8[3,3,3,3]
movd %xmm8, 0x10(%rcx,%rax,4)
addq $0x4, %rax
paddd %xmm7, %xmm0
cmpq $0x100, %rax            # imm = 0x100
jne 0x4710c
movb 0x11(%rdi), %r13b
movl $0x100, %r12d           # imm = 0x100
jmp 0x471fb
movb $0x2, 0x589bee(%rip)    # 0x5d0de0
jmp 0x471fb
movb $0x6, 0x589be5(%rip)    # 0x5d0de0
movq %rbp, -0x10(%rsp)
movl %r13d, %r9d
andb $0x10, %r9b
movq %r15, -0x40(%rsp)
leal -0x1(%r15), %ecx
xorl %eax, %eax
testb %r9b, %r9b
cmovel %eax, %ecx
movq %rcx, -0x28(%rsp)
negb %r9b
movl $0x0, %ebp
sbbl %ebp, %ebp
movl $0x1, -0x44(%rsp)
testb $0x20, %r13b
jne 0x47251
decl %r14d
movl %edx, %r15d
negl %r15d
movl $0xffffffff, -0x44(%rsp) # imm = 0xFFFFFFFF
movl %r14d, %r11d
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
jmp 0x47257
movl %edx, %r15d
xorl %r11d, %r11d
cmpl %r14d, %r11d
je 0x473e5
movq -0x10(%rsp), %rcx
movl %r10d, -0x30(%rsp)
movl 0x38(%rsp), %r9d
orl -0x40(%rsp), %ebp
addl %r9d, %r14d
movl %r14d, -0x40(%rsp)
shrb $0x4, %r13b
movq -0x18(%rsp), %r10
movq -0x28(%rsp), %rax
addl %eax, %r10d
addl %r9d, %r11d
imull %r11d, %edx
movslq %edx, %rax
xorl %r14d, %r14d
movq %rdi, %r9
cmpb $0x8, 0x2(%rdi)
setl %r14b
notl %r14d
movslq -0x38(%rsp), %rdx
movslq %r15d, %rdi
movq %rdi, -0x8(%rsp)
movslq %r10d, %rdi
movzbl %r13b, %r15d
andl $0x1, %r15d
negq %r15
orq $0x1, %r15
leaq (%rsi,%rdi,4), %rsi
addq %rax, %rsi
movq %r15, %r13
shlq $0x2, %r13
movq %rsi, -0x38(%rsp)
movq -0x28(%rsp), %rax
cmpl $0x7f, %r14d
jg 0x472f8
movl %r14d, %edi
notl %edi
testb $0x7f, %dil
jne 0x47300
movzbl (%rbx), %r10d
incq %rbx
jmp 0x47303
movl %r14d, %r10d
jmp 0x4739b
movl %r14d, %r10d
cmpb $0x1, 0x1(%r9)
movq %rbx, %r14
jne 0x4732c
cmpb $0x8, -0x30(%rsp)
jne 0x4731a
movzbl (%rbx), %r12d
jmp 0x4731e
movzwl (%rbx), %r12d
movl %r12d, %edi
imull %ecx, %edi
movslq %edi, %r14
addq -0x20(%rsp), %r14
leal -0x1(%rcx), %edi
cmpl $0x3, %edi
ja 0x47398
leaq 0x30651(%rip), %r12     # 0x7798c
movslq (%r12,%rdi,4), %rdi
addq %r12, %rdi
jmpq *%rdi
movzbl (%r14), %edi
leaq 0x589691(%rip), %r14    # 0x5d09e0
movl (%r14,%rdi,4), %r12d
jmp 0x47398
movl (%r14), %r12d
movl $0xff000000, %edi       # imm = 0xFF000000
orl %edi, %r12d
jmp 0x47398
movl (%r14), %r12d
jmp 0x47398
movzwl (%r14), %edi
movl %edi, %r14d
shll $0x9, %r14d
andl $0xf80000, %r14d        # imm = 0xF80000
movl %edi, %r12d
shll $0x6, %r12d
andl $0xf800, %r12d          # imm = 0xF800
orl %r14d, %r12d
andl $0x1f, %edi
leal (%r12,%rdi,8), %r12d
addl $0xff000000, %r12d      # imm = 0xFF000000
addq %rdx, %rbx
cmpl %r8d, %r11d
jae 0x473b0
movq -0x18(%rsp), %rdi
addl %eax, %edi
cmpl -0x2c(%rsp), %edi
jae 0x473b0
movl %r12d, (%rsi)
movl %r10d, %r14d
notl %r14d
sarl $0x1f, %r14d
addl %r10d, %r14d
addl %r15d, %eax
addq %r13, %rsi
cmpl %eax, %ebp
jne 0x472de
addl -0x44(%rsp), %r11d
movq -0x38(%rsp), %rsi
addq -0x8(%rsp), %rsi
cmpl -0x40(%rsp), %r11d
jne 0x472d4
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq