// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ilbm.c
// Function: detect_hame
static void detect_hame(deark *c, lctx *d, struct imgbody_info *ibi,
	struct frame_ctx *frctx)
{
	i64 plane;
	i64 k;
	UI firstword[4];
	u8 pixelval[16];
	static const u8 sig[15] = { 0xa, 0x2, 0xf, 0x5, 0x8, 0x4, 0xd, 0xc,
		0x6, 0xd, 0xb, 0x0, 0x7, 0xf, 0x1 };

	if(d->is_hame) return;
	if(d->ham_flag) return;
	if(!d->found_cmap) return;
	if(ibi->width<640) return;
	if(ibi->planes_fg!=4 || ibi->planes_total!=4) return;
	if(ibi->is_thumb) return;
	if(frctx->formtype!=CODE_ILBM && frctx->formtype!=CODE_ACBM) return;
	if(!frctx || !frctx->frame_buffer) return;

	// Note: This is quite possibly not the right way to detect HAM-E.
	// RECOIL does it by looking up the palette color of each pixel, and using
	// certain bits in the palette entry. In all the HAM-E images I have, the
	// palette is constructed so as to make that process a no-op.

	// Need to examine the values of the first 16 pixels, so need the first 2
	// bytes of each of the 4 planes of row 0.
	for(plane=0; plane<4; plane++) {
		firstword[plane] = (UI)dbuf_getu16be(frctx->frame_buffer,
			plane * ibi->bytes_per_row_per_plane);
	}

	for(k=0; k<16; k++) {
		pixelval[k] = 0;
		for(plane=0; plane<4; plane++) {
			if(firstword[plane] & (1U<<(15-(UI)k))) {
				pixelval[k] |= 1U<<(UI)plane;
			}
		}
	}

	if(de_memcmp(pixelval, sig, 15)) return;
	if(pixelval[15]!=0x4 && pixelval[15]!=0x8) return;

	if(d->opt_allowhame) {
		de_warn(c, "This is probably a HAM-E image, which is not supported correctly.");
	}
	else {
		de_err(c, "HAM-E images are not supported. "
			"(\"-opt ilbm:allowspecial\" to decode anyway)");
		d->errflag = 1;
	}

	d->is_hame = 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x49(%rax)
je 0xb2de7
jmp 0xb2fb1
movq -0x10(%rbp), %rax
cmpb $0x0, 0x38(%rax)
je 0xb2df6
jmp 0xb2fb1
movq -0x10(%rbp), %rax
cmpb $0x0, 0x33(%rax)
jne 0xb2e05
jmp 0xb2fb1
movq -0x18(%rbp), %rax
cmpq $0x280, (%rax)          # imm = 0x280
jge 0xb2e17
jmp 0xb2fb1
movq -0x18(%rbp), %rax
cmpq $0x4, 0x10(%rax)
jne 0xb2e2d
movq -0x18(%rbp), %rax
cmpq $0x4, 0x18(%rax)
je 0xb2e32
jmp 0xb2fb1
movq -0x18(%rbp), %rax
cmpl $0x0, 0x5c(%rax)
je 0xb2e41
jmp 0xb2fb1
movq -0x20(%rbp), %rax
cmpl $0x494c424d, (%rax)     # imm = 0x494C424D
je 0xb2e5e
movq -0x20(%rbp), %rax
cmpl $0x4143424d, (%rax)     # imm = 0x4143424D
je 0xb2e5e
jmp 0xb2fb1
cmpq $0x0, -0x20(%rbp)
je 0xb2e70
movq -0x20(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xb2e75
jmp 0xb2fb1
movq $0x0, -0x28(%rbp)
cmpq $0x4, -0x28(%rbp)
jge 0xb2eb6
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rax
imulq 0x40(%rax), %rsi
callq 0x1651d0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, -0x40(%rbp,%rax,4)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xb2e7d
movq $0x0, -0x30(%rbp)
cmpq $0x10, -0x30(%rbp)
jge 0xb2f3e
movq -0x30(%rbp), %rax
movb $0x0, -0x50(%rbp,%rax)
movq $0x0, -0x28(%rbp)
cmpq $0x4, -0x28(%rbp)
jge 0xb2f2e
movq -0x28(%rbp), %rax
movl -0x40(%rbp,%rax,4), %eax
movq -0x30(%rbp), %rcx
movl %ecx, %edx
movl $0xf, %ecx
subl %edx, %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xb2f1e
movq -0x28(%rbp), %rax
movl %eax, %ecx
movl $0x1, %edx
shll %cl, %edx
movq -0x30(%rbp), %rax
movzbl -0x50(%rbp,%rax), %ecx
orl %edx, %ecx
movb %cl, -0x50(%rbp,%rax)
jmp 0xb2f20
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0xb2ed6
jmp 0xb2f30
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xb2ebe
leaq -0x50(%rbp), %rdi
leaq 0x1014c6(%rip), %rsi    # 0x1b440f
movl $0xf, %edx
callq 0x19170
cmpl $0x0, %eax
je 0xb2f5a
jmp 0xb2fb1
movzbl -0x41(%rbp), %eax
cmpl $0x4, %eax
je 0xb2f6e
movzbl -0x41(%rbp), %eax
cmpl $0x8, %eax
je 0xb2f6e
jmp 0xb2fb1
movq -0x10(%rbp), %rax
cmpb $0x0, 0x30(%rax)
je 0xb2f8c
movq -0x8(%rbp), %rdi
leaq 0x1019d0(%rip), %rsi    # 0x1b4953
movb $0x0, %al
callq 0x15bc10
jmp 0xb2fa9
movq -0x8(%rbp), %rdi
leaq 0x1019fe(%rip), %rsi    # 0x1b4995
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movl $0x1, 0x1c(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x49(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)