// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
je 0xb1c6e
jmp 0xb1ffa
movq -0x10(%rbp), %rax
cmpb $0x0, 0x32(%rax)
jne 0xb1c7d
jmp 0xb1ffa
cmpq $0x0, -0x18(%rbp)
jne 0xb1c89
jmp 0xb1ffa
movq -0x18(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0xb1c98
jmp 0xb1ffa
movq -0x18(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x8(%rbp), %rdi
movl $0x68, %esi
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0xb2010
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0xb22c0
cmpl $0x0, %eax
jne 0xb1ce0
jmp 0xb1ffa
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xb1d0c
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
cmpl $0x4143424d, 0x4(%rax)  # imm = 0x4143424D
jne 0xb1d49
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %r9
callq 0xb2770
cmpl $0x0, %eax
jne 0xb1d44
jmp 0xb1ffa
jmp 0xb1ea8
movq -0x10(%rbp), %rax
cmpl $0x5247424e, 0x4(%rax)  # imm = 0x5247424E
je 0xb1d63
movq -0x10(%rbp), %rax
cmpl $0x52474238, 0x4(%rax)  # imm = 0x52474238
jne 0xb1d93
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %r9
callq 0xb2860
cmpl $0x0, %eax
jne 0xb1d8e
jmp 0xb1ffa
jmp 0xb1ea6
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x0, %eax
jne 0xb1dd4
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %r8
movq -0x38(%rbp), %rax
movq 0x50(%rax), %r9
callq 0xb2a60
cmpl $0x0, %eax
jne 0xb1dcf
jmp 0xb1ffa
jmp 0xb1ea4
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x1, %eax
jne 0xb1e3b
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rdx
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %r8
movq -0x38(%rbp), %rax
movq 0x50(%rax), %r9
movl $0x1, (%rsp)
callq 0xb2ac0
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x167be0
cmpl $0x0, -0x3c(%rbp)
jne 0xb1e39
jmp 0xb1ffa
jmp 0xb1ea2
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x2, %eax
jne 0xb1e81
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x18(%rbp), %rax
movq 0x20(%rax), %r9
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, (%rsp)
callq 0xb2bd0
cmpl $0x0, %eax
jne 0xb1e7f
jmp 0xb1ffa
jmp 0xb1ea0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movzbl 0x20(%rax), %edx
leaq 0x11e9cf(%rip), %rsi    # 0x1d0863
movb $0x0, %al
callq 0x15bfb0
jmp 0xb1ffa
jmp 0xb1ea2
jmp 0xb1ea4
jmp 0xb1ea6
jmp 0xb1ea8
movq -0x38(%rbp), %rax
movzbl 0x60(%rax), %eax
cmpl $0x0, %eax
je 0xb1f78
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
cmpq 0x50(%rcx), %rax
je 0xb1f78
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl $0x2, %ecx
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0xb1f78
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
imulq 0x8(%rdx), %rcx
cmpq %rcx, %rax
jne 0xb1f76
movq -0x8(%rbp), %rdi
leaq 0x10283f(%rip), %rsi    # 0x1b4759
movb $0x0, %al
callq 0x15b0d0
movq -0x38(%rbp), %rax
movq (%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rcx
shlq $0x3, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x38(%rbp), %rax
imulq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x38(%rbp), %rax
imulq 0x8(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x50(%rax)
jmp 0xb1f78
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
cmpq 0x50(%rcx), %rax
je 0xb1fb4
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rcx
leaq 0x111b35(%rip), %rsi    # 0x1c3ae2
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0xb2c90
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0xb2dc0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x18(%rbp), %rcx
callq 0xb2fc0
movl $0x1, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movl -0x40(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq