// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/jfbuild/src/kplib.c
// Function: kpcxrend
static int kpcxrend (const char *buf, int fleng,
	intptr_t daframeplace, int dabytesperline, int daxres, int dayres,
	int daglobxoffs, int daglobyoffs)
{
	int i, j, x, y, nplanes, x0, x1, y0, y1, xsiz, ysiz;
	intptr_t p;
	unsigned char c, *cptr;

	if (*(int *)buf != LSWAPIB(0x0801050a)) return(-1);
	xsiz = SSWAPIB(*(short *)&buf[ 8])-SSWAPIB(*(short *)&buf[4])+1; if (xsiz <= 0) return(-1);
	ysiz = SSWAPIB(*(short *)&buf[10])-SSWAPIB(*(short *)&buf[6])+1; if (ysiz <= 0) return(-1);

		//buf[3]: bpp/plane:{1,2,4,8}
		//bpl = *(short *)&buf[66];  //#bytes per scanline. Must be EVEN. May have unused data.
		//nplanes*bpl bytes per scanline; always be decoding break at the end of scan line

	nplanes = buf[65];
	if (nplanes == 1)
	{
		//if (buf[fleng-769] != 12) return(-1); //Some PCX are buggy!
		cptr = (unsigned char *)&buf[fleng-768];
		for(i=0;i<256;i++)
		{
			palcol[i] = (((int)cptr[0])<<16) +
							(((int)cptr[1])<< 8) +
							(((int)cptr[2])    ) + LSWAPIB(0xff000000);
			cptr += 3;
		}
		coltype = 3; bitdepth = 8; paleng = 256; //For PNGOUT
	}
	else if (nplanes == 3)
	{
		coltype = 2;

			//Make sure background is opaque (since 24-bit PCX renderer doesn't do it)
		x0 = max(daglobxoffs,0); x1 = min(xsiz+daglobxoffs,daxres);
		y0 = max(daglobyoffs,0); y1 = min(ysiz+daglobyoffs,dayres);
		p = y0*dabytesperline + daframeplace+3;
		for(y=y0;y<y1;y++,p+=dabytesperline)
			for(x=x0;x<x1;x++) *(unsigned char *)((x<<2)+p) = 255;
	}

	x = x0 = daglobxoffs; x1 = xsiz+daglobxoffs;
	y = y0 = daglobyoffs; y1 = ysiz+daglobyoffs;
	cptr = (unsigned char *)&buf[128];
	p = y*dabytesperline+daframeplace;

	j = nplanes-1; daxres <<= 2; x0 <<= 2; x1 <<= 2; x <<= 2; x += j;
	if (nplanes == 1) //8-bit PCX
	{
		do
		{
			c = *cptr++; if (c < 192) i = 1; else { i = (c&63); c = *cptr++; }
			j = palcol[(int)c];
			for(;i;i--)
			{
				if ((unsigned int)y < (unsigned int)dayres)
					if ((unsigned int)x < (unsigned int)daxres) *(int *)(x+p) = j;
				x += 4; if (x >= x1) { x = x0; y++; p += dabytesperline; }
			}
		} while (y < y1);
	}
	else if (nplanes == 3) //24-bit PCX
	{
		do
		{
			c = *cptr++; if (c < 192) i = 1; else { i = (c&63); c = *cptr++; }
			for(;i;i--)
			{
				if ((unsigned int)y < (unsigned int)dayres)
					if ((unsigned int)x < (unsigned int)daxres) *(unsigned char *)(x+p) = c;
				x += 4; if (x >= x1) { j--; if (j < 0) { j = 3-1; y++; p += dabytesperline; } x = x0+j; }
			}
		} while (y < y1);
	}

	return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
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)
movq -0x10(%rbp), %rax
cmpl $0x801050a, (%rax)      # imm = 0x801050A
je 0x66ac8
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x66ee8
movq -0x10(%rbp), %rax
movswl 0x8(%rax), %eax
movq -0x10(%rbp), %rcx
movswl 0x4(%rcx), %ecx
subl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x54(%rbp)
jg 0x66af2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x66ee8
movq -0x10(%rbp), %rax
movswl 0xa(%rax), %eax
movq -0x10(%rbp), %rcx
movswl 0x6(%rcx), %ecx
subl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
cmpl $0x0, -0x58(%rbp)
jg 0x66b1c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x66ee8
movq -0x10(%rbp), %rax
movsbl 0x41(%rax), %eax
movl %eax, -0x40(%rbp)
cmpl $0x1, -0x40(%rbp)
jne 0x66bc1
movq -0x10(%rbp), %rax
movl -0x14(%rbp), %ecx
subl $0x300, %ecx            # imm = 0x300
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x100, -0x30(%rbp)     # imm = 0x100
jge 0x66ba4
movq -0x70(%rbp), %rax
movzbl (%rax), %edx
shll $0x10, %edx
movq -0x70(%rbp), %rax
movzbl 0x1(%rax), %eax
shll $0x8, %eax
addl %eax, %edx
movq -0x70(%rbp), %rax
movzbl 0x2(%rax), %eax
addl %eax, %edx
addl $0xff000000, %edx       # imm = 0xFF000000
movslq -0x30(%rbp), %rcx
leaq 0x55bdf6(%rip), %rax    # 0x5c2980
movl %edx, (%rax,%rcx,4)
movq -0x70(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x70(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x66b4f
movb $0x3, 0x55c1d5(%rip)    # 0x5c2d80
movb $0x8, 0x55c1cf(%rip)    # 0x5c2d81
movl $0x100, 0x541f94(%rip)  # imm = 0x100
jmp 0x66cb4
cmpl $0x3, -0x40(%rbp)
jne 0x66cb2
movb $0x2, 0x55c1ae(%rip)    # 0x5c2d80
cmpl $0x0, 0x10(%rbp)
jle 0x66be0
movl 0x10(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x66be7
xorl %eax, %eax
movl %eax, -0x74(%rbp)
jmp 0x66be7
movl -0x74(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x54(%rbp), %eax
addl 0x10(%rbp), %eax
cmpl -0x28(%rbp), %eax
jge 0x66c03
movl -0x54(%rbp), %eax
addl 0x10(%rbp), %eax
movl %eax, -0x78(%rbp)
jmp 0x66c09
movl -0x28(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, 0x18(%rbp)
jle 0x66c1d
movl 0x18(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x66c24
xorl %eax, %eax
movl %eax, -0x7c(%rbp)
jmp 0x66c24
movl -0x7c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x58(%rbp), %eax
addl 0x18(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x66c40
movl -0x58(%rbp), %eax
addl 0x18(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x66c46
movl -0x2c(%rbp), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
imull -0x24(%rbp), %eax
cltq
addq -0x20(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x60(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jge 0x66cb0
movl -0x44(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jge 0x66c97
movl -0x38(%rbp), %eax
shll $0x2, %eax
cltq
addq -0x60(%rbp), %rax
movb $-0x1, (%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x66c75
jmp 0x66c99
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movslq -0x24(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x66c67
jmp 0x66cb2
jmp 0x66cb4
movl 0x10(%rbp), %eax
movl %eax, -0x44(%rbp)
movl %eax, -0x38(%rbp)
movl -0x54(%rbp), %eax
addl 0x10(%rbp), %eax
movl %eax, -0x48(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl %eax, -0x3c(%rbp)
movl -0x58(%rbp), %eax
addl 0x18(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x70(%rbp)
movl -0x3c(%rbp), %eax
imull -0x24(%rbp), %eax
cltq
addq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x40(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x28(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x28(%rbp)
movl -0x44(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x44(%rbp)
movl -0x48(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x48(%rbp)
movl -0x38(%rbp), %eax
shll $0x2, %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
cmpl $0x1, -0x40(%rbp)
jne 0x66e04
jmp 0x66d39
movq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x70(%rbp)
movb (%rax), %al
movb %al, -0x61(%rbp)
movzbl -0x61(%rbp), %eax
cmpl $0xc0, %eax
jge 0x66d61
movl $0x1, -0x30(%rbp)
jmp 0x66d7f
movzbl -0x61(%rbp), %eax
andl $0x3f, %eax
movl %eax, -0x30(%rbp)
movq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x70(%rbp)
movb (%rax), %al
movb %al, -0x61(%rbp)
movzbl -0x61(%rbp), %eax
movslq %eax, %rcx
leaq 0x55bbf3(%rip), %rax    # 0x5c2980
movl (%rax,%rcx,4), %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x66df1
movl -0x3c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x66db8
movl -0x38(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x66db6
movl -0x34(%rbp), %ecx
movslq -0x38(%rbp), %rax
addq -0x60(%rbp), %rax
movl %ecx, (%rax)
jmp 0x66db8
movl -0x38(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jl 0x66de4
movl -0x44(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movslq -0x24(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x66de6
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x66d93
jmp 0x66df3
movl -0x3c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x66d39
jmp 0x66ee1
cmpl $0x3, -0x40(%rbp)
jne 0x66edf
jmp 0x66e10
movq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x70(%rbp)
movb (%rax), %al
movb %al, -0x61(%rbp)
movzbl -0x61(%rbp), %eax
cmpl $0xc0, %eax
jge 0x66e38
movl $0x1, -0x30(%rbp)
jmp 0x66e56
movzbl -0x61(%rbp), %eax
andl $0x3f, %eax
movl %eax, -0x30(%rbp)
movq -0x70(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x70(%rbp)
movb (%rax), %al
movb %al, -0x61(%rbp)
jmp 0x66e58
cmpl $0x0, -0x30(%rbp)
je 0x66ecf
movl -0x3c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0x66e7d
movl -0x38(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0x66e7b
movb -0x61(%rbp), %cl
movslq -0x38(%rbp), %rax
addq -0x60(%rbp), %rax
movb %cl, (%rax)
jmp 0x66e7d
movl -0x38(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x48(%rbp), %eax
jl 0x66ec2
movl -0x34(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x66eb9
movl $0x2, -0x34(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movslq -0x24(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x44(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x66ec4
movl -0x30(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x66e58
jmp 0x66ed1
movl -0x3c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jl 0x66e10
jmp 0x66edf
jmp 0x66ee1
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
popq %rbp
retq
nopl (%rax)