// File: /workspace/github_c_makefile_star2_O1/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), %r11b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x1, %r11b
ja 0x45134
movsbl 0x2(%rdi), %ecx
cmpl $0xb, %ecx
jg 0x45134
movl $0xe0e, %r9d            # imm = 0xE0E
btl %ecx, %r9d
jae 0x45134
movsbl 0x10(%rdi), %r13d
cmpl $0x20, %r13d
jg 0x45134
testb %r13b, %r13b
je 0x45134
movl %r13d, %ecx
andl $0x7, %ecx
jne 0x45134
cmpb $0x3f, 0x11(%rdi)
ja 0x45134
movzwl 0xc(%rdi), %r10d
testl %r10d, %r10d
je 0x45134
movzwl 0xe(%rdi), %ebp
testl %ebp, %ebp
je 0x45134
movsbq (%rdi), %rax
leaq (%rdi,%rax), %rbx
addq $0x12, %rbx
movl %r13d, %eax
sarl $0x3, %eax
xorl %r12d, %r12d
cmpb $0x1, %r11b
jne 0x44ed6
movsbl 0x7(%rdi), %r14d
addl $0x7, %r14d
sarl $0x3, %r14d
movzwl 0x3(%rdi), %r9d
movslq %r14d, %r11
imulq %r11, %r9
movq %rbx, %rcx
subq %r9, %rcx
movq %rcx, -0x20(%rsp)
movzwl 0x5(%rdi), %r9d
imulq %r11, %r9
addq %r9, %rbx
jmp 0x44ee2
movl %eax, %r14d
movq $0x0, -0x20(%rsp)
movb $0x2, %r11b
leal -0x2(%r14), %r9d
cmpl $0x2, %r9d
movl %ebp, %ecx
jb 0x44f4b
cmpl $0x4, %r14d
je 0x44f48
movq %r14, %r9
cmpl $0x1, %r14d
jne 0x44f55
movb $0x0, 0x58aed9(%rip)    # 0x5cfde0
movb $0x8, 0x58aed3(%rip)    # 0x5cfde1
movl $0xff000000, 0x58aac8(%rip) # imm = 0xFF000000
movl $0x1, %r11d
movl $0xff010101, %ebp       # imm = 0xFF010101
leaq 0x58aab6(%rip), %r14    # 0x5cf9e0
movl %ebp, (%r14,%r11,4)
incq %r11
addl $0x10101, %ebp          # imm = 0x10101
cmpq $0x100, %r11            # imm = 0x100
jne 0x44f2a
movl $0x100, %r12d           # imm = 0x100
jmp 0x44f55
movb $0x6, %r11b
movq %r14, %r9
movb %r11b, 0x58ae8b(%rip)   # 0x5cfde0
movb 0x11(%rdi), %r14b
movl %r14d, %r11d
andb $0x10, %r11b
leal -0x1(%r10), %r15d
xorl %ebp, %ebp
testb %r11b, %r11b
cmovel %ebp, %r15d
movq %r15, -0x28(%rsp)
negb %r11b
movl $0x0, %ebp
sbbl %ebp, %ebp
movl $0x1, -0x38(%rsp)
testb $0x20, %r14b
jne 0x44fa4
decl %ecx
movl %edx, %r15d
negl %r15d
movl $0xffffffff, -0x38(%rsp) # imm = 0xFFFFFFFF
movl %ecx, %r11d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
jmp 0x44faa
movl %edx, %r15d
xorl %r11d, %r11d
cmpl %ecx, %r11d
je 0x45132
movl %r13d, -0x30(%rsp)
movl %ecx, %r13d
movl 0x38(%rsp), %ecx
orl %r10d, %ebp
addl %ecx, %r13d
movl %r13d, -0x34(%rsp)
shrb $0x4, %r14b
movq -0x18(%rsp), %r10
movq -0x28(%rsp), %r13
addl %r10d, %r13d
addl %ecx, %r11d
imull %r11d, %edx
movslq %edx, %rdx
xorl %r10d, %r10d
movq %rdi, %rcx
cmpb $0x8, 0x2(%rdi)
setl %r10b
notl %r10d
movslq %eax, %rdi
movslq %r15d, %rax
movq %rax, -0x10(%rsp)
movslq %r13d, %rax
movzbl %r14b, %r15d
andl $0x1, %r15d
negq %r15
orq $0x1, %r15
leaq (%rsi,%rax,4), %rsi
addq %rdx, %rsi
movq %r15, %r13
shlq $0x2, %r13
movq %rsi, -0x8(%rsp)
movq -0x28(%rsp), %rax
cmpl $0x7f, %r10d
jg 0x45044
movl %r10d, %edx
notl %edx
testb $0x7f, %dl
jne 0x4504c
movzbl (%rbx), %r14d
incq %rbx
jmp 0x4504f
movl %r10d, %r14d
jmp 0x450e8
movl %r10d, %r14d
cmpb $0x1, 0x1(%rcx)
movq %rbx, %r10
jne 0x45078
cmpb $0x8, -0x30(%rsp)
jne 0x45065
movzbl (%rbx), %r12d
jmp 0x45069
movzwl (%rbx), %r12d
movl %r12d, %edx
imull %r9d, %edx
movslq %edx, %r10
addq -0x20(%rsp), %r10
leal -0x1(%r9), %edx
cmpl $0x3, %edx
ja 0x450e5
leaq 0x31618(%rip), %r12     # 0x766a0
movslq (%r12,%rdx,4), %rdx
addq %r12, %rdx
jmpq *%rdx
movzbl (%r10), %edx
leaq 0x58a944(%rip), %r10    # 0x5cf9e0
movl (%r10,%rdx,4), %r12d
jmp 0x450e5
movl (%r10), %r12d
movl $0xff000000, %edx       # imm = 0xFF000000
orl %edx, %r12d
jmp 0x450e5
movl (%r10), %r12d
jmp 0x450e5
movzwl (%r10), %edx
movl %edx, %r10d
shll $0x9, %r10d
andl $0xf80000, %r10d        # imm = 0xF80000
movl %edx, %r12d
shll $0x6, %r12d
andl $0xf800, %r12d          # imm = 0xF800
orl %r10d, %r12d
andl $0x1f, %edx
leal (%r12,%rdx,8), %r12d
addl $0xff000000, %r12d      # imm = 0xFF000000
addq %rdi, %rbx
cmpl %r8d, %r11d
jae 0x450fd
movq -0x18(%rsp), %rdx
addl %eax, %edx
cmpl -0x2c(%rsp), %edx
jae 0x450fd
movl %r12d, (%rsi)
movl %r14d, %r10d
notl %r10d
sarl $0x1f, %r10d
addl %r14d, %r10d
addl %r15d, %eax
addq %r13, %rsi
cmpl %eax, %ebp
jne 0x4502b
addl -0x38(%rsp), %r11d
movq -0x8(%rsp), %rsi
addq -0x10(%rsp), %rsi
cmpl -0x34(%rsp), %r11d
jne 0x45021
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq