// File: /workspace/github_c_makefile_star2_O1/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 0x40179
retq
movzbl (%rdi), %r8d
cmpl $0x46, %r8d
jle 0x401fa
cmpl $0x47, %r8d
je 0x4028d
cmpl $0xff, %r8d
je 0x4021c
cmpl $0x89, %r8d
jne 0x402f5
cmpb $0x50, 0x1(%rdi)
jne 0x401f4
cmpl $0x474e5089, (%rdi)     # imm = 0x474E5089
jne 0x40178
cmpl $0xa1a0a0d, 0x4(%rdi)   # imm = 0xA1A0A0D
jne 0x40178
addl $-0x10, %esi
xorl %eax, %eax
cmpl $0x9, %esi
jb 0x40178
leaq 0x8(%rdi), %r8
cmpl $0x52444849, 0x4(%r8)   # imm = 0x52444849
je 0x403ec
movl (%r8), %r9d
bswapl %r9d
addq %r9, %r8
addq $0xc, %r8
movq %r8, %r9
subq %rdi, %r9
cmpq %rsi, %r9
jb 0x401cc
jmp 0x40178
cmpl $0x46, %r8d
jg 0x40209
cmpl $0x19, %r8d
jne 0x402c5
jmp 0x402af
cmpl $0x47, %r8d
je 0x4028d
cmpl $0xff, %r8d
jne 0x402f5
cmpb $-0x28, 0x1(%rdi)
jne 0x4027b
addl $-0x8, %esi
xorl %eax, %eax
cmpl $0x3, %esi
jb 0x40178
leaq 0x2(%rdi), %r8
leaq 0x1(%r8), %r9
cmpb $-0x1, (%r8)
jne 0x40268
movzbl (%r9), %r9d
addl $0xffffff40, %r9d       # imm = 0xFFFFFF40
cmpl $0x2, %r9d
jbe 0x403cf
movzwl 0x2(%r8), %r9d
rolw $0x8, %r9w
movzwl %r9w, %r9d
addq %r8, %r9
addq $0x2, %r9
movq %r9, %r10
subq %rdi, %r10
movq %r9, %r8
cmpq %rsi, %r10
jb 0x40234
jmp 0x40178
cmpl $0x19, %r8d
je 0x402af
cmpl $0x42, %r8d
je 0x402cb
cmpl $0x47, %r8d
jne 0x402f5
cmpb $0x49, 0x1(%rdi)
jne 0x402a3
cmpb $0x46, 0x2(%rdi)
jne 0x402a3
cmpb $0x0, 0xc(%rdi)
je 0x403c0
cmpl $0x42, %r8d
je 0x402cb
cmpl $0x19, %r8d
jne 0x402f5
cmpb $-0x6f, 0x1(%rdi)
jne 0x402c5
cmpb $0x8, 0xa(%rdi)
jne 0x402c5
cmpb $0x0, 0xb(%rdi)
je 0x403b4
cmpl $0x42, %r8d
jne 0x402f5
cmpb $0x4d, 0x1(%rdi)
jne 0x402f5
cmpl $0xc, 0xe(%rdi)
jne 0x4039f
cmpw $0x1, 0x16(%rdi)
jne 0x40178
movzwl 0x12(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x14(%rdi), %eax
jmp 0x403ca
movl (%rdi), %eax
cmpl $0x20534444, %eax       # imm = 0x20534444
je 0x40324
cmpl $0x801050a, %eax        # imm = 0x801050A
jne 0x40337
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 0x403ca
cmpl $0x7c, 0x4(%rdi)
jne 0x40337
movl 0x10(%rdi), %eax
movl %eax, (%rdx)
movl 0xc(%rdi), %eax
jmp 0x403ca
xorl %eax, %eax
cmpl $0x14, %esi
jl 0x40178
cmpb $0x1, 0x1(%rdi)
ja 0x40178
movzbl 0x2(%rdi), %esi
cmpl $0xb, %esi
ja 0x40178
movl $0xe0e, %r8d            # imm = 0xE0E
btl %esi, %r8d
jae 0x40178
movb 0x10(%rdi), %sil
testb $0x7, %sil
setne %r8b
decb %sil
cmpb $0x20, %sil
setae %sil
orb %r8b, %sil
jne 0x40178
cmpb $0x3f, 0x11(%rdi)
ja 0x40178
movzwl 0xc(%rdi), %esi
movl %esi, (%rdx)
movzwl 0xe(%rdi), %edx
jmp 0x403fa
cmpw $0x1, 0x1a(%rdi)
jne 0x40178
movl 0x12(%rdi), %eax
movl %eax, (%rdx)
movl 0x16(%rdi), %eax
jmp 0x403ca
movzwl 0x2(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x4(%rdi), %eax
jmp 0x403ca
movzwl 0x6(%rdi), %eax
movl %eax, (%rdx)
movzwl 0x8(%rdi), %eax
movl %eax, (%rcx)
xorl %eax, %eax
retq
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
movl 0x8(%r8), %esi
bswapl %esi
movl %esi, (%rdx)
movl 0xc(%r8), %edx
bswapl %edx
movl %edx, (%rcx)
retq