// File: /workspace/github_c_makefile_star2_O3/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:
xorl %eax, %eax
movl %eax, (%rcx)
movl %eax, (%rdx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl $0x10, %esi
jge 0x41571
retq
movzbl (%rdi), %r8d
cmpl $0x46, %r8d
jle 0x415f0
cmpl $0x47, %r8d
je 0x41632
cmpl $0xff, %r8d
je 0x41657
cmpl $0x89, %r8d
jne 0x416bf
cmpb $0x50, 0x1(%rdi)
jne 0x416bf
cmpl $0x474e5089, (%rdi)     # imm = 0x474E5089
jne 0x41570
cmpl $0xa1a0a0d, 0x4(%rdi)   # imm = 0xA1A0A0D
jne 0x41570
addl $-0x10, %esi
xorl %eax, %eax
cmpl $0x9, %esi
jb 0x41570
leaq 0x8(%rdi), %r8
cmpl $0x52444849, 0x4(%r8)   # imm = 0x52444849
je 0x417bd
movl (%r8), %r9d
bswapl %r9d
addq %r9, %r8
addq $0xc, %r8
movq %r8, %r9
subq %rdi, %r9
cmpq %rsi, %r9
jb 0x415c8
jmp 0x41570
cmpl $0x19, %r8d
je 0x416a9
cmpl $0x42, %r8d
jne 0x416bf
cmpb $0x4d, 0x1(%rdi)
jne 0x416bf
cmpl $0xc, 0xe(%rdi)
jne 0x41775
cmpw $0x1, 0x16(%rdi)
jne 0x41570
movzwl 0x12(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x14(%rdi), %eax
jmp 0x416f9
cmpb $0x49, 0x1(%rdi)
jne 0x416bf
cmpb $0x46, 0x2(%rdi)
jne 0x416bf
cmpb $0x0, 0xc(%rdi)
jne 0x416bf
movzwl 0x6(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x8(%rdi), %eax
jmp 0x416f9
cmpb $-0x28, 0x1(%rdi)
jne 0x416bf
addl $-0x8, %esi
leaq 0x2(%rdi), %r8
leaq 0x1(%r8), %r9
cmpb $-0x1, (%r8)
jne 0x41694
movzbl (%r9), %eax
addl $0xffffff40, %eax       # imm = 0xFFFFFF40
cmpl $0x2, %eax
jbe 0x4178d
movzwl 0x2(%r8), %eax
rolw $0x8, %ax
movzwl %ax, %eax
leaq (%r8,%rax), %r9
addq $0x2, %r9
movq %r9, %r10
subq %rdi, %r10
xorl %eax, %eax
movq %r9, %r8
cmpq %rsi, %r10
jb 0x41664
jmp 0x41570
cmpb $-0x6f, 0x1(%rdi)
jne 0x416bf
cmpb $0x8, 0xa(%rdi)
jne 0x416bf
cmpb $0x0, 0xb(%rdi)
je 0x417ae
movl (%rdi), %eax
cmpl $0x20534444, %eax       # imm = 0x20534444
je 0x416eb
cmpl $0x801050a, %eax        # imm = 0x801050A
jne 0x416fe
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 0x416f9
cmpl $0x7c, 0x4(%rdi)
jne 0x416fe
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
testl $0xfe00, %eax           # imm = 0xFE00
setne %r9b
orb %sil, %r9b
cmpb $0xc, %r8b
setae %sil
xorl %eax, %eax
orb %r9b, %sil
jne 0x41570
andl $0xf, %r8d
movl $0xe0e, %esi            # imm = 0xE0E
btl %r8d, %esi
jae 0x41570
movb 0x10(%rdi), %sil
testb $0x7, %sil
setne %r8b
decb %sil
cmpb $0x20, %sil
setae %sil
orb %r8b, %sil
jne 0x41570
cmpb $0x3f, 0x11(%rdi)
ja 0x41570
movzwl 0xc(%rdi), %esi
movl %esi, (%rdx)
movzwl 0xe(%rdi), %edx
movl %edx, (%rcx)
retq
cmpw $0x1, 0x1a(%rdi)
jne 0x41570
movl 0x12(%rdi), %eax
movl %eax, (%rdx)
movl 0x16(%rdi), %eax
jmp 0x416f9
movzwl 0x5(%r8), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, (%rcx)
movzwl 0x7(%r8), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, (%rdx)
jmp 0x416fb
movzwl 0x2(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x4(%rdi), %eax
jmp 0x416f9
movl 0x8(%r8), %esi
bswapl %esi
movl %esi, (%rdx)
movl 0xc(%r8), %edx
bswapl %edx
jmp 0x41772