// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/ilbm.c
// Function: do_before_image_chunk
static void do_before_image_chunk(deark *c, lctx *d)
{
	if(d->bmhd_changed_flag || d->camg_changed_flag) {
		if(d->ham_flag) {
			if(d->planes_raw==6 || d->planes_raw==5) {
				d->is_ham6 = 1;
			}
			else if(d->planes_raw==8 || d->planes_raw==7) {
				d->is_ham8 = 1;
			}
			else {
				de_warn(c, "Invalid bit depth (%d) for HAM image.", (int)d->planes_raw);
			}
		}

		if(!d->found_cmap && d->planes_raw<=8) {
			de_make_grayscale_palette(d->pal, (i64)1<<(UI)d->planes_raw, 0);
		}

		if(d->planes_raw==6 && d->pal_ncolors==32 && !d->ehb_flag && !d->ham_flag) {
			de_warn(c, "Assuming this is an EHB image");
			d->ehb_flag = 1;
		}
	}

	if(d->cmap_changed_flag) {
		de_memcpy(d->pal, d->pal_raw, (size_t)d->pal_ncolors * sizeof(d->pal_raw[0]));
	}

	if(d->cmap_changed_flag && d->ehb_flag && d->planes_raw==6) {
		UI k;

		// TODO: Should we still do this if the palette already has 64 colors
		// (as it often does)?
		for(k=0; k<32; k++) {
			u8 cr, cg, cb;

			cr = DE_COLOR_R(d->pal[k]);
			cg = DE_COLOR_G(d->pal[k]);
			cb = DE_COLOR_B(d->pal[k]);
			d->pal[k+32] = DE_MAKE_RGB(cr/2, cg/2, cb/2);
		}
	}

	if(d->fixpal_setting && d->cmap_changed_flag) {
		fixup_palette(c, d);
	}

	if(d->cmap_changed_flag) {
		d->pal_is_grayscale = de_is_grayscale_palette(d->pal, 256);
	}

	d->cmap_changed_flag = 0;
	d->bmhd_changed_flag = 0;
	d->camg_changed_flag = 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
cmpb $0x0, 0x35(%rsi)
jne 0x7aae9
cmpb $0x0, 0x36(%rbx)
je 0x7ab7c
cmpb $0x0, 0x38(%rbx)
je 0x7ab24
movq 0x60(%rbx), %rdx
leaq -0x7(%rdx), %rax
cmpq $0x2, %rax
jb 0x7ab0d
leaq -0x5(%rdx), %rax
cmpq $0x1, %rax
ja 0x7ab13
movb $0x1, 0x39(%rbx)
jmp 0x7ab24
movb $0x1, 0x3a(%rbx)
jmp 0x7ab24
leaq 0xae337(%rip), %rsi     # 0x128e51
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8faa
cmpb $0x0, 0x33(%rbx)
jne 0x7ab4a
movq 0x60(%rbx), %rcx
cmpq $0x8, %rcx
jg 0x7ab4a
leaq 0x4d4(%rbx), %rdi
movl $0x1, %esi
shlq %cl, %rsi
xorl %edx, %edx
callq 0xf3bdb
cmpq $0x6, 0x60(%rbx)
jne 0x7ab7c
cmpq $0x20, 0xc8(%rbx)
jne 0x7ab7c
cmpb $0x0, 0x3b(%rbx)
jne 0x7ab7c
cmpb $0x0, 0x38(%rbx)
jne 0x7ab7c
leaq 0xae309(%rip), %rsi     # 0x128e77
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8faa
movb $0x1, 0x3b(%rbx)
cmpb $0x0, 0x34(%rbx)
je 0x7acbd
leaq 0x4d4(%rbx), %r15
leaq 0xd4(%rbx), %rsi
movq 0xc8(%rbx), %r12
leaq (,%r12,4), %rdx
movq %r15, %rdi
callq 0x181e0
cmpb $0x0, 0x3b(%rbx)
je 0x7abf4
cmpq $0x6, 0x60(%rbx)
jne 0x7abf4
movq $-0x20, %rax
movdqa 0x961a9(%rip), %xmm0    # 0x110d70
movdqa 0x961b1(%rip), %xmm1    # 0x110d80
movdqu 0x554(%rbx,%rax,4), %xmm2
psrld $0x1, %xmm2
pand %xmm0, %xmm2
por %xmm1, %xmm2
movdqu %xmm2, 0x5d4(%rbx,%rax,4)
addq $0x4, %rax
jne 0x7abcf
cmpb $0x0, 0x2d(%rbx)
sete %al
testq %r12, %r12
setle %cl
orb %al, %cl
jne 0x7aca4
movl 0x4(%rbx), %eax
cmpl $0x52474238, %eax       # imm = 0x52474238
je 0x7aca4
cmpl $0x5247424e, %eax       # imm = 0x5247424E
je 0x7aca4
cmpb $0x0, 0x3a(%rbx)
je 0x7ac55
xorl %eax, %eax
movl (%r15,%rax,4), %ecx
movl %ecx, %edx
shrl $0x6, %edx
andl $0xfcfcfc, %ecx         # imm = 0xFCFCFC
andl $0x30303, %edx          # imm = 0x30303
addl %edx, %ecx
addl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, (%r15,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x7ac2a
jmp 0x7aca4
cmpb $0x0, 0x39(%rbx)
je 0x7acd3
xorl %eax, %eax
movl (%r15,%rax,4), %ecx
movl %ecx, %edx
shrl $0x14, %edx
movl %ecx, %esi
shrl $0xc, %esi
andl $0xf, %edx
andl $0xf, %esi
shrb $0x4, %cl
movzbl %cl, %ecx
movl %ecx, %edi
shll $0x4, %edi
addl %ecx, %edi
imull $0x110000, %edx, %ecx   # imm = 0x110000
imull $0x1100, %esi, %edx     # imm = 0x1100
orl %ecx, %edx
movzbl %dil, %ecx
addl %edx, %ecx
addl $0xff000000, %ecx       # imm = 0xFF000000
movl %ecx, (%r15,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x7ac5d
cmpb $0x0, 0x34(%rbx)
je 0x7acbd
movl $0x100, %esi            # imm = 0x100
movq %r15, %rdi
callq 0xf1ed8
movl %eax, 0xd0(%rbx)
movw $0x0, 0x34(%rbx)
movb $0x0, 0x36(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
xorl %eax, %eax
testl $0xf0f0f, (%r15,%rax,4) # imm = 0xF0F0F
jne 0x7aca4
incq %rax
cmpq %rax, %r12
jne 0x7acd5
leaq 0xae1a7(%rip), %rsi     # 0x128e95
movq %r14, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0xc8(%rbx), %r12
testq %r12, %r12
jg 0x7ac5b
jmp 0x7aca4