// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/kplib.c
// Function: kpgetdim
int kpgetdim (const char *buf, int leng, int *xsiz, int *ysiz)
{
	int *lptr;
	const unsigned char *cptr;
	unsigned char *ubuf = (unsigned char *)buf;

	(*xsiz) = (*ysiz) = 0; if (leng < 16) return(-1);
	if ((ubuf[0] == 0x89) && (ubuf[1] == 0x50)) //.PNG
	{
		lptr = (int *)buf;
		if ((lptr[0] != LSWAPIB(0x474e5089)) || (lptr[1] != LSWAPIB(0x0a1a0a0d))) return(-1);
		lptr = &lptr[2];
		while (((uintptr_t)lptr-(uintptr_t)buf) < (uintptr_t)(leng-16))
		{
			if (lptr[1] == LSWAPIB(0x52444849)) //IHDR
				{ (*xsiz) = LSWAPIL(lptr[2]); (*ysiz) = LSWAPIL(lptr[3]); break; }
			lptr = (int *)((intptr_t)lptr + LSWAPIL(lptr[0]) + 12);
		}
	}
	else if ((ubuf[0] == 0xff) && (ubuf[1] == 0xd8)) //.JPG
	{
		cptr = (unsigned char *)&buf[2];
		while (((uintptr_t)cptr-(uintptr_t)buf) < (uintptr_t)(leng-8))
		{
			if (cptr[0] != 255) { cptr = &cptr[1]; continue; }
			if ((unsigned int)(cptr[1]-0xc0) < 3)
			{
				(*ysiz) = SSWAPIL(*(unsigned short *)&cptr[5]);
				(*xsiz) = SSWAPIL(*(unsigned short *)&cptr[7]);
				break;
			}
			cptr = &cptr[SSWAPIL(*(unsigned short *)&cptr[2])+2];
		}
	}
	else if ((ubuf[0] == 'G') && (ubuf[1] == 'I') && (ubuf[2] == 'F') && (ubuf[12] == 0)) //.GIF
	{
		(*xsiz) = (int)SSWAPIB(*(unsigned short *)&buf[6]);
		(*ysiz) = (int)SSWAPIB(*(unsigned short *)&buf[8]);
	}
	else if ((ubuf[0] == 0x19) && (ubuf[1] == 0x91) && (ubuf[10] == 8) && (ubuf[11] == 0)) //old .CEL/.PIC
	{
		(*xsiz) = (int)SSWAPIB(*(unsigned short *)&buf[2]);
		(*ysiz) = (int)SSWAPIB(*(unsigned short *)&buf[4]);
	}
	else if ((ubuf[0] == 'B') && (ubuf[1] == 'M')) //.BMP
	{
		if (*(int *)(&buf[14]) == LSWAPIB(12)) //OS/2 1.x (old format)
		{
			if (*(short *)(&buf[22]) != SSWAPIB(1)) return(-1);
			(*xsiz) = (int)SSWAPIB(*(unsigned short *)&buf[18]);
			(*ysiz) = (int)SSWAPIB(*(unsigned short *)&buf[20]);
		}
		else //All newer formats...
		{
			if (*(short *)(&buf[26]) != SSWAPIB(1)) return(-1);
			(*xsiz) = LSWAPIB(*(int *)&buf[18]);
			(*ysiz) = LSWAPIB(*(int *)&buf[22]);
		}
	}
	else if (*(int *)ubuf == LSWAPIB(0x0801050a)) //.PCX
	{
		(*xsiz) = SSWAPIB(*(short *)&buf[ 8])-SSWAPIB(*(short *)&buf[4])+1;
		(*ysiz) = SSWAPIB(*(short *)&buf[10])-SSWAPIB(*(short *)&buf[6])+1;
	}
	else if ((*(int *)ubuf == LSWAPIB(0x20534444)) && (*(int *)&ubuf[4] == LSWAPIB(124))) //.DDS
	{
		(*xsiz) = LSWAPIB(*(int *)&buf[16]);
		(*ysiz) = LSWAPIB(*(int *)&buf[12]);
	}
	else
	{     //Unreliable .TGA identification - this MUST be final case!
		if ((leng >= 20) && (!(ubuf[1]&0xfe)))
			if ((ubuf[2] < 12) && ((1<<ubuf[2])&0xe0e))
				if ((!(ubuf[16]&7)) && (ubuf[16] != 0) && (ubuf[16] <= 32))
					if (!(buf[17]&0xc0))
					{
						(*xsiz) = (int)SSWAPIB(*(unsigned short *)&buf[12]);
						(*ysiz) = (int)SSWAPIB(*(unsigned short *)&buf[14]);
					}
	}
	return(0);
}

// Assembly:
andl $0x0, (%rcx)
andl $0x0, (%rdx)
pushq $-0x1
popq %rax
cmpl $0x10, %esi
jge 0x3c31b
retq
movzbl (%rdi), %r8d
cmpl $0x19, %r8d
je 0x3c441
cmpl $0x42, %r8d
je 0x3c39e
cmpl $0x47, %r8d
je 0x3c423
cmpl $0xff, %r8d
je 0x3c3cc
cmpl $0x89, %r8d
jne 0x3c457
cmpb $0x50, 0x1(%rdi)
jne 0x3c457
cmpl $0x474e5089, (%rdi)     # imm = 0x474E5089
jne 0x3c31a
cmpl $0xa1a0a0d, 0x4(%rdi)   # imm = 0xA1A0A0D
jne 0x3c31a
leaq 0x8(%rdi), %r8
addl $-0x10, %esi
movq %r8, %r9
subq %rdi, %r9
xorl %eax, %eax
cmpq %rsi, %r9
jae 0x3c31a
cmpl $0x52444849, 0x4(%r8)   # imm = 0x52444849
je 0x3c54f
movl (%r8), %eax
bswapl %eax
addq %rax, %r8
addq $0xc, %r8
jmp 0x3c375
cmpb $0x4d, 0x1(%rdi)
jne 0x3c457
cmpl $0xc, 0xe(%rdi)
jne 0x3c50b
cmpw $0x1, 0x16(%rdi)
jne 0x3c31a
movzwl 0x12(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x14(%rdi), %eax
jmp 0x3c491
cmpb $-0x28, 0x1(%rdi)
jne 0x3c457
leaq 0x2(%rdi), %r9
addl $-0x8, %esi
movq %r9, %r8
subq %rdi, %r9
xorl %eax, %eax
cmpq %rsi, %r9
jae 0x3c31a
leaq 0x1(%r8), %r9
cmpb $-0x1, (%r8)
jne 0x3c3dd
movzbl (%r9), %r9d
addl $0xffffff40, %r9d       # imm = 0xFFFFFF40
cmpl $0x2, %r9d
jbe 0x3c523
movzwl 0x2(%r8), %eax
rolw $0x8, %ax
movzwl %ax, %eax
leaq (%r8,%rax), %r9
addq $0x2, %r9
jmp 0x3c3dd
cmpb $0x49, 0x1(%rdi)
jne 0x3c457
cmpb $0x46, 0x2(%rdi)
jne 0x3c457
cmpb $0x0, 0xc(%rdi)
jne 0x3c457
movzwl 0x6(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x8(%rdi), %eax
jmp 0x3c491
cmpb $-0x6f, 0x1(%rdi)
jne 0x3c457
cmpb $0x8, 0xa(%rdi)
jne 0x3c457
cmpb $0x0, 0xb(%rdi)
je 0x3c540
movl (%rdi), %eax
cmpl $0x20534444, %eax       # imm = 0x20534444
je 0x3c483
cmpl $0x801050a, %eax        # imm = 0x801050A
jne 0x3c496
movswl 0x8(%rdi), %eax
movswl 0x4(%rdi), %esi
subl %esi, %eax
incl %eax
movl %eax, (%rdx)
movswl 0xa(%rdi), %eax
movswl 0x6(%rdi), %edx
subl %edx, %eax
incl %eax
jmp 0x3c491
cmpl $0x7c, 0x4(%rdi)
jne 0x3c496
movl 0x10(%rdi), %eax
movl %eax, (%rdx)
movl 0xc(%rdi), %eax
movl %eax, (%rcx)
xorl %eax, %eax
retq
movl %eax, %r8d
shrl $0x10, %r8d
cmpl $0x14, %esi
setb %sil
testw $0xfe00, %ax            # imm = 0xFE00
setne %r9b
orb %sil, %r9b
cmpb $0xc, %r8b
setae %sil
xorl %eax, %eax
orb %r9b, %sil
jne 0x3c31a
andl $0xf, %r8d
movl $0xe0e, %esi            # imm = 0xE0E
btl %r8d, %esi
jae 0x3c31a
movb 0x10(%rdi), %sil
testb $0x7, %sil
setne %r8b
decb %sil
cmpb $0x20, %sil
setae %sil
orb %r8b, %sil
jne 0x3c31a
cmpb $0x3f, 0x11(%rdi)
ja 0x3c31a
movzwl 0xc(%rdi), %esi
movl %esi, (%rdx)
movzwl 0xe(%rdi), %edx
jmp 0x3c55d
cmpw $0x1, 0x1a(%rdi)
jne 0x3c31a
movl 0x12(%rdi), %eax
movl %eax, (%rdx)
movl 0x16(%rdi), %eax
jmp 0x3c491
movzwl 0x5(%r8), %esi
rolw $0x8, %si
movzwl %si, %esi
movl %esi, (%rcx)
movzwl 0x7(%r8), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movl %ecx, (%rdx)
retq
movzwl 0x2(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x4(%rdi), %eax
jmp 0x3c491
movl 0x8(%r8), %esi
bswapl %esi
movl %esi, (%rdx)
movl 0xc(%r8), %edx
bswapl %edx
movl %edx, (%rcx)
retq