// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/ilbm.c
// Function: do_image_chunk_internal
static int do_image_chunk_internal(deark *c, lctx *d, struct frame_ctx *frctx, i64 pos1, i64 len, int is_thumb)
{
	struct imgbody_info *ibi = NULL;
	int ret;
	int retval = 0;

	if(d->errflag) goto done;
	if(!d->found_bmhd) goto done;
	if(!frctx) goto done;
	if(frctx->done_flag) goto done;
	frctx->done_flag = 1;

	ibi = de_malloc(c, sizeof(struct imgbody_info));

	do_before_image_chunk(c, d);

	if(!init_imgbody_info(c, d, ibi, is_thumb)) {
		goto done;
	}

	if(!frctx->frame_buffer) {
		frctx->frame_buffer = dbuf_create_membuf(c, ibi->frame_buffer_size, 0x1);
	}

	if(d->formtype==CODE_ACBM) {
		// Note: I don't think ABIT images are ever compressed.
		if(!convert_abit(c, d, ibi, pos1, len, frctx->frame_buffer)) goto done;
	}
	else if(d->formtype==CODE_RGBN || d->formtype==CODE_RGB8) {
		if(!decompress_rgbn8(c, d, ibi, pos1, len, frctx->frame_buffer)) goto done;
	}
	else if(ibi->compression==0) {
		if(!decompress_method0(c, d, pos1, len, frctx->frame_buffer, ibi->frame_buffer_size)) goto done;
	}
	else if(ibi->compression==1) {
		dbuf_enable_wbuffer(frctx->frame_buffer);
		ret = decompress_ilbm_packbits(c, c->infile, pos1, len,
			frctx->frame_buffer, ibi->frame_buffer_size, 1);
		dbuf_disable_wbuffer(frctx->frame_buffer);
		if(!ret) goto done;
	}
	else if(ibi->compression==2) {
		if(!decompress_method2(c, d, ibi, pos1, len, frctx->frame_buffer, ibi->frame_buffer_size)) goto done;
	}
	else {
		de_err(c, "Unsupported compression method: %d", (int)ibi->compression);
		goto done;
	}

	if(ibi->is_pbm && (frctx->frame_buffer->len != ibi->frame_buffer_size) && (ibi->width%2)) {
		if(frctx->frame_buffer->len == ibi->width*ibi->height) {
			// Hack: I have some PBM images (e.g. BBM thumbnails) that aren't row-padded.
			de_dbg(c, "[assuming rows are not 16-bit padded]");
			ibi->bytes_per_row_per_plane = ibi->width;
			ibi->bits_per_row_per_plane = ibi->bytes_per_row_per_plane * 8;
			ibi->frame_buffer_rowspan = ibi->bytes_per_row_per_plane * ibi->planes_total;
			ibi->frame_buffer_size = ibi->frame_buffer_rowspan * ibi->height;
		}
	}

	if(frctx->frame_buffer->len != ibi->frame_buffer_size) {
		de_warn(c, "Expected %"I64_FMT" decompressed bytes, got %"I64_FMT, ibi->frame_buffer_size,
			frctx->frame_buffer->len);
	}

	detect_dctv(c, d, ibi, frctx);
	detect_hame(c, d, ibi, frctx);

	write_frame(c, d, ibi, frctx);

	retval = 1;

done:
	de_free(c, ibi);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r13
cmpl $0x0, 0x1c(%rsi)
je 0x76ab7
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r13, %rdi
movq %r14, %rsi
callq 0xded32
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r15
xorl %r14d, %r14d
testq %rdx, %rdx
je 0x76a94
movq %rsi, %rbp
movq %r8, 0x10(%rsp)
cmpb $0x0, 0x32(%rsi)
movl $0x0, %r12d
je 0x76a97
xorl %r12d, %r12d
cmpl $0x0, 0x8(%r15)
je 0x76ae5
xorl %r14d, %r14d
jmp 0x76a97
movl %r9d, %ebx
movq %rcx, 0x18(%rsp)
movl $0x1, 0x8(%r15)
pushq $0x68
popq %rsi
movq %r13, %rdi
callq 0xdecaf
movq %rax, %r14
movq %r13, %rdi
movq %rbp, %rsi
callq 0x770e3
movq %r13, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movl %ebx, %ecx
callq 0x77306
testl %eax, %eax
je 0x76a97
movq %rbp, 0x8(%rsp)
movq %r15, 0x30(%rsp)
movq 0x20(%r15), %rbp
testq %rbp, %rbp
jne 0x76b54
movq 0x50(%r14), %rsi
pushq $0x1
popq %rdx
movq %r13, %rdi
callq 0xe59cd
movq %rax, %rbp
movq 0x30(%rsp), %rax
movq %rbp, 0x20(%rax)
movq 0x8(%rsp), %rax
movl 0x4(%rax), %ecx
cmpl $0x5247424e, %ecx       # imm = 0x5247424E
je 0x76bea
cmpl $0x52474238, %ecx       # imm = 0x52474238
je 0x76bea
cmpl $0x4143424d, %ecx       # imm = 0x4143424D
jne 0x76d15
movq 0x8(%r14), %rax
movq 0x40(%r14), %rcx
imulq %rax, %rcx
movq %rcx, 0x10(%rsp)
xorl %r12d, %r12d
cmpq 0x18(%r14), %r12
jge 0x76e14
movq 0x10(%rsp), %rbx
imulq %r12, %rbx
addq 0x18(%rsp), %rbx
xorl %r15d, %r15d
cmpq %rax, %r15
jge 0x76be5
movq 0x18(%r13), %rdi
movq 0x40(%r14), %rdx
movq %rdx, %rsi
imulq %r15, %rsi
addq %rbx, %rsi
movq 0x48(%r14), %rax
imulq %r15, %rax
movq %rdx, %r8
imulq %r12, %r8
addq %rax, %r8
movq %rbp, %rcx
callq 0xe490d
incq %r15
movq 0x8(%r14), %rax
jmp 0x76bab
incq %r12
jmp 0x76b90
leaq 0x50(%rsp), %rsi
movq 0x18(%rsp), %rax
movq %rax, (%rsi)
movq 0x8(%r14), %rdx
imulq (%r14), %rdx
movq %rdx, 0x48(%rsp)
addq %rax, 0x10(%rsp)
xorl %edx, %edx
leaq 0x20(%rsp), %r15
pushq $0x3
popq %r12
movq %r13, 0x40(%rsp)
movl %ecx, 0x3c(%rsp)
cmpq 0x48(%rsp), %rdx
jge 0x76e14
movq 0x10(%rsp), %rax
cmpq %rax, 0x50(%rsp)
jge 0x76d57
movq %rdx, 0x18(%rsp)
movq 0x18(%r13), %rdi
cmpl $0x52474238, %ecx       # imm = 0x52474238
jne 0x76c6a
callq 0xe4071
movq %rax, %rbx
shrl $0x18, %eax
movb %al, 0x20(%rsp)
movl %ebx, %eax
shrl $0x10, %eax
movb %al, 0x21(%rsp)
movb %bh, 0x22(%rsp)
andl $0x7f, %ebx
jmp 0x76ca9
callq 0xe3f6e
movq %rax, %rbx
shrl $0xc, %eax
movb %al, 0x20(%rsp)
movl %ebx, %eax
shrl $0x8, %eax
andb $0xf, %al
movb %al, 0x21(%rsp)
movl %ebx, %eax
shrb $0x4, %al
movb %al, 0x22(%rsp)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x76ca6
movzbl 0x20(%rsp,%rax), %ecx
imull $0x11, %ecx, %ecx
movb %cl, 0x20(%rsp,%rax)
incq %rax
jmp 0x76c8f
andl $0x7, %ebx
testq %rbx, %rbx
jne 0x76cd7
movq 0x18(%r13), %rdi
leaq 0x50(%rsp), %rbx
movq %rbx, %rsi
callq 0xe3f2a
testb %al, %al
je 0x76cc8
movzbl %al, %ebx
jmp 0x76cd7
movq 0x18(%r13), %rdi
movq %rbx, %rsi
callq 0xe3f6e
movq %rax, %rbx
testq %rbx, %rbx
movl $0x0, %r13d
cmovgq %rbx, %r13
subq $0x1, %r13
jb 0x76cfa
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xe46ba
jmp 0x76ce4
movq 0x18(%rsp), %rdx
addq %rbx, %rdx
movq 0x40(%rsp), %r13
movl 0x3c(%rsp), %ecx
leaq 0x50(%rsp), %rsi
jmp 0x76c1d
movzbl 0x20(%r14), %edx
cmpl $0x2, %edx
je 0x76db6
cmpl $0x1, %edx
je 0x76d70
testl %edx, %edx
jne 0x76fd7
movq 0x50(%r14), %rsi
movq 0x10(%rsp), %rdi
callq 0xdfb45
movq 0x18(%r13), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
movq %rbp, %rcx
callq 0xe45f4
jmp 0x76e14
leaq 0xa4fc0(%rip), %rsi     # 0x11bd1e
xorl %r12d, %r12d
movq %r13, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x76a97
movq %rbp, %rdi
callq 0xe589c
movq 0x18(%r13), %rsi
movq 0x30(%rsp), %r15
movq 0x20(%r15), %r8
movq 0x50(%r14), %r9
movl $0x1, (%rsp)
movq %r13, %rdi
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0x7755d
movl %eax, %ebx
movq 0x20(%r15), %rdi
callq 0xe58c4
testl %ebx, %ebx
jne 0x76e14
jmp 0x76a97
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r15
andq $0x0, 0x28(%r15)
movups %xmm0, 0x18(%r15)
movq 0x8(%rsp), %rax
movq %rax, (%r15)
movq %r14, 0x8(%r15)
movq %rbp, 0x10(%r15)
movq %r13, %rdi
callq 0xfacc0
movq %rax, %rbx
movq %r15, 0x10(%rax)
leaq 0xef1(%rip), %rax       # 0x77cdf
movq %rax, 0x20(%rbx)
movq 0x18(%r13), %rax
movq %rax, 0x18(%rbx)
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0xface4
movq %rbx, %rdi
callq 0xfacd3
cmpb $0x0, 0x60(%r14)
movq 0x30(%rsp), %rbp
movq 0x20(%rbp), %rax
je 0x76e36
movq 0x18(%rax), %rcx
movq 0x50(%r14), %rdx
cmpq %rdx, %rcx
jne 0x76e3c
movq %rcx, %rdx
jmp 0x76e89
movq 0x50(%r14), %rdx
jmp 0x76e89
movq (%r14), %rsi
testb $0x1, %sil
je 0x76e89
imulq 0x8(%r14), %rsi
cmpq %rsi, %rcx
jne 0x76e89
leaq 0xa4d95(%rip), %rsi     # 0x11bbeb
movq %r13, %rdi
xorl %eax, %eax
callq 0xde896
movq (%r14), %rdx
movq %rdx, 0x40(%r14)
leaq (,%rdx,8), %rax
movq %rax, 0x38(%r14)
imulq 0x18(%r14), %rdx
movq %rdx, 0x48(%r14)
imulq 0x8(%r14), %rdx
movq %rdx, 0x50(%r14)
movq 0x20(%rbp), %rax
movq 0x18(%rax), %rcx
cmpq %rdx, %rcx
je 0x76ea3
leaq 0xb4095(%rip), %rsi     # 0x12af2e
movq %r13, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x8(%rsp), %rax
cmpb $0x0, 0x4a(%rax)
je 0x76eda
movq 0x8(%rsp), %rsi
cmpb $0x0, 0x49(%rsi)
jne 0x76ec3
cmpb $0x0, 0x38(%rsi)
je 0x76f5a
pushq $0x1
popq %r12
movq %r13, %rdi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x7764a
jmp 0x76a97
movq 0x20(%rbp), %rdi
testq %rdi, %rdi
je 0x76eae
cmpl $0x0, 0x5c(%r14)
jne 0x76eae
movl (%rbp), %eax
cmpl $0x494c424d, %eax       # imm = 0x494C424D
je 0x76efb
cmpl $0x4143424d, %eax       # imm = 0x4143424D
jne 0x76eae
movq 0x40(%r14), %rsi
cmpq $0x40, %rsi
jl 0x76eae
movq 0x8(%rsp), %rax
imulq 0x60(%rax), %rsi
addq $-0x20, %rsi
callq 0x77f7a
testl %eax, %eax
jne 0x76f35
movq 0x40(%r14), %rsi
addq $-0x20, %rsi
movq 0x20(%rbp), %rdi
callq 0x77f7a
testl %eax, %eax
je 0x76eae
movq 0x8(%rsp), %rax
cmpb $0x0, 0x2f(%rax)
je 0x76ff0
leaq 0xa4e0e(%rip), %rsi     # 0x11bd59
movq %r13, %rdi
xorl %eax, %eax
callq 0xdf05a
jmp 0x7700d
cmpb $0x0, 0x33(%rsi)
je 0x76ec3
cmpq $0x280, (%r14)          # imm = 0x280
jl 0x76ec3
cmpq $0x4, 0x10(%r14)
jne 0x76ec3
cmpq $0x4, 0x18(%r14)
jne 0x76ec3
cmpl $0x0, 0x5c(%r14)
jne 0x76ec3
movl (%rbp), %eax
cmpl $0x494c424d, %eax       # imm = 0x494C424D
je 0x76fa7
cmpl $0x4143424d, %eax       # imm = 0x4143424D
jne 0x76ec3
cmpq $0x0, 0x20(%rbp)
je 0x76ec3
movq %r13, %r15
xorl %ebx, %ebx
cmpq $0x4, %rbx
je 0x7701b
movq 0x20(%rbp), %rdi
movq 0x40(%r14), %rsi
imulq %rbx, %rsi
callq 0xe3f46
movl %eax, 0x50(%rsp,%rbx,4)
incq %rbx
jmp 0x76fb7
leaq 0xc0915(%rip), %rsi     # 0x1378f3
xorl %r12d, %r12d
movq %r13, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x76a97
leaq 0xa4da3(%rip), %rsi     # 0x11bd9a
movq %r13, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq 0x8(%rsp), %rax
movl $0x1, 0x1c(%rax)
movq 0x8(%rsp), %rax
movb $0x1, 0x4a(%rax)
jmp 0x76eae
xorl %eax, %eax
pushq $0x1
popq %rdx
cmpq $0x10, %rax
je 0x7705b
movb $0xf, %cl
subb %al, %cl
movl %edx, %esi
shll %cl, %esi
movb $0x0, 0x20(%rsp,%rax)
xorl %edi, %edi
xorl %ecx, %ecx
cmpq $0x4, %rcx
je 0x77056
testl %esi, 0x50(%rsp,%rcx,4)
je 0x77051
movl %edx, %r8d
shll %cl, %r8d
orb %r8b, %dil
movb %dil, 0x20(%rsp,%rax)
incq %rcx
jmp 0x77037
incq %rax
jmp 0x77020
leaq 0xa482d(%rip), %rsi     # 0x11b88f
leaq 0x20(%rsp), %rdi
pushq $0xf
popq %rdx
callq 0x18230
testl %eax, %eax
movq %r15, %r13
movq 0x30(%rsp), %rbp
movq 0x8(%rsp), %rsi
jne 0x76ec3
movzbl 0x2f(%rsp), %eax
cmpl $0x8, %eax
je 0x77097
cmpl $0x4, %eax
jne 0x76ec3
cmpb $0x0, 0x30(%rsi)
je 0x770b0
leaq 0xa4d41(%rip), %rsi     # 0x11bde5
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf05a
jmp 0x770cd
leaq 0xa4d70(%rip), %rsi     # 0x11be27
movq %r15, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq 0x8(%rsp), %rax
movl $0x1, 0x1c(%rax)
movq 0x8(%rsp), %rsi
movb $0x1, 0x49(%rsi)
movq %r15, %r13
movq 0x30(%rsp), %rbp
jmp 0x76ec3