// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movq %rdi, %r14
cmpl $0x0, 0x1c(%rsi)
je 0x7a88b
xorl %r10d, %r10d
xorl %r15d, %r15d
movq %r14, %rdi
movq %r10, %rsi
callq 0xe9752
movl %r15d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
xorl %r10d, %r10d
testq %rdx, %rdx
je 0x7a86b
movq %rsi, 0x18(%rsp)
cmpb $0x0, 0x32(%rsi)
movl $0x0, %r15d
je 0x7a86e
xorl %r15d, %r15d
cmpl $0x0, 0x8(%r12)
je 0x7a8b7
xorl %r10d, %r10d
jmp 0x7a86e
movl %r9d, %ebp
movq %rcx, %rbx
movq %r8, 0x30(%rsp)
movl $0x1, 0x8(%r12)
movl $0x68, %esi
movq %r14, %rdi
callq 0xe969b
movq %r12, 0x60(%rsp)
movq %rax, %r12
movq %r14, %rdi
movq 0x18(%rsp), %r13
movq %r13, %rsi
callq 0x7ae9c
movq %r14, %rdi
movq %r13, %rsi
movq %r12, 0x8(%rsp)
movq %r12, %rdx
movl %ebp, %ecx
callq 0x7b076
testl %eax, %eax
je 0x7ae2f
movq 0x60(%rsp), %r12
cmpq $0x0, 0x20(%r12)
jne 0x7a935
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rsi
movq %r14, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, 0x20(%r12)
movq 0x18(%rsp), %rax
movl 0x4(%rax), %ecx
cmpl $0x5247424e, %ecx       # imm = 0x5247424E
movq %r14, 0x10(%rsp)
movq 0x8(%rsp), %r10
je 0x7a980
cmpl $0x52474238, %ecx       # imm = 0x52474238
je 0x7a980
cmpl $0x4143424d, %ecx       # imm = 0x4143424D
jne 0x7ac18
movq 0x20(%r12), %rcx
movq %r14, %rdi
movq %r10, %rsi
movq %rbx, %rdx
callq 0x7b2f2
movq 0x8(%rsp), %r10
jmp 0x7aaca
movq 0x20(%r12), %rbp
leaq 0x40(%rsp), %rsi
movq %rbx, (%rsi)
movq 0x8(%r10), %r12
imulq (%r10), %r12
addq %rbx, 0x30(%rsp)
xorl %r14d, %r14d
leaq 0x20(%rsp), %r15
xorl %ebx, %ebx
movl %ecx, 0x38(%rsp)
movq %r12, 0x58(%rsp)
cmpq %r12, %r14
jge 0x7a9f3
movq 0x30(%rsp), %rax
cmpq %rax, 0x40(%rsp)
jge 0x7a9f8
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
cmpl $0x52474238, %ecx       # imm = 0x52474238
movl %ebx, 0x3c(%rsp)
jne 0x7a9ff
callq 0xef34e
movl %eax, %ecx
shrl $0x18, %ecx
movb %cl, 0x20(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x21(%rsp)
movb %ah, 0x22(%rsp)
andl $0x7f, %eax
jmp 0x7aa44
movl $0x1, %ebx
xorl %eax, %eax
jmp 0x7aab0
callq 0xef193
movl %eax, %ecx
shrl $0xc, %ecx
movb %cl, 0x20(%rsp)
movl %eax, %ecx
shrl $0x8, %ecx
andb $0xf, %cl
movb %cl, 0x21(%rsp)
movq %rax, %rsi
shrb $0x4, %al
movb %al, 0x22(%rsp)
xorl %eax, %eax
movzbl 0x20(%rsp,%rax), %ecx
movl %ecx, %edx
shll $0x4, %edx
addl %ecx, %edx
movb %dl, 0x20(%rsp,%rax)
incq %rax
cmpq $0x3, %rax
jne 0x7aa25
movq %rsi, %rax
andl $0x7, %eax
testq %rax, %rax
jne 0x7aa79
movq 0x10(%rsp), %rbx
movq 0x18(%rbx), %rdi
leaq 0x40(%rsp), %r13
movq %r13, %rsi
callq 0xef14d
testb %al, %al
je 0x7aa68
movzbl %al, %eax
jmp 0x7aa74
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef193
movq 0x58(%rsp), %r12
movq %rax, %r13
testq %rax, %rax
jle 0x7aa99
movq %r13, %rbx
movl $0x3, %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0xefb82
decq %rbx
jne 0x7aa84
addq %r13, %r14
movb $0x1, %al
movq 0x8(%rsp), %r10
movl 0x38(%rsp), %ecx
leaq 0x40(%rsp), %rsi
movl 0x3c(%rsp), %ebx
testb %al, %al
jne 0x7a9ad
testl %ebx, %ebx
je 0x7ac61
movq 0x10(%rsp), %r14
movq 0x60(%rsp), %r12
cmpb $0x0, 0x60(%r10)
je 0x7ab2d
movq 0x20(%r12), %rax
movq 0x18(%rax), %rax
cmpq 0x50(%r10), %rax
je 0x7ab2d
movq (%r10), %rcx
testb $0x1, %cl
je 0x7ab2d
imulq 0x8(%r10), %rcx
cmpq %rcx, %rax
jne 0x7ab2d
leaq 0xae102(%rip), %rsi     # 0x128bfb
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rsp), %r10
movq (%r10), %rax
movq %rax, 0x40(%r10)
leaq (,%rax,8), %rcx
movq %rcx, 0x38(%r10)
imulq 0x18(%r10), %rax
movq %rax, 0x48(%r10)
imulq 0x8(%r10), %rax
movq %rax, 0x50(%r10)
movq 0x20(%r12), %rax
movq 0x18(%rax), %rcx
movq 0x50(%r10), %rdx
cmpq %rdx, %rcx
movq 0x18(%rsp), %r15
je 0x7ab5a
leaq 0xbd357(%rip), %rsi     # 0x137ea2
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x8(%rsp), %r10
cmpb $0x0, 0x4a(%r15)
je 0x7ab94
cmpb $0x0, 0x49(%r15)
jne 0x7ab73
cmpb $0x0, 0x38(%r15)
je 0x7ac7f
movq %r14, %rdi
movq %r15, %rsi
movq %r10, %rdx
movq %r12, %rcx
callq 0x7b4f6
movq 0x8(%rsp), %r10
movl $0x1, %r15d
jmp 0x7a86e
movq 0x20(%r12), %rdi
testq %rdi, %rdi
je 0x7ab61
cmpl $0x0, 0x5c(%r10)
jne 0x7ab61
movl (%r12), %eax
cmpl $0x494c424d, %eax       # imm = 0x494C424D
je 0x7abb7
cmpl $0x4143424d, %eax       # imm = 0x4143424D
jne 0x7ab61
movq 0x40(%r10), %rsi
cmpq $0x40, %rsi
jl 0x7ab61
imulq 0x60(%r15), %rsi
addq $-0x20, %rsi
callq 0x7bf26
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x7abf7
movq 0x40(%rcx), %rsi
addq $-0x20, %rsi
movq 0x20(%r12), %rdi
callq 0x7bf26
movq 0x8(%rsp), %r10
testl %eax, %eax
je 0x7ab61
cmpb $0x0, 0x2f(%r15)
je 0x7ae39
leaq 0xae160(%rip), %rsi     # 0x128d69
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x7ae52
movzbl 0x20(%r10), %edx
cmpl $0x2, %edx
je 0x7adf9
cmpl $0x1, %edx
je 0x7ada8
testl %edx, %edx
jne 0x7ae1b
movq 0x20(%r12), %r15
movq 0x50(%r10), %rsi
movq 0x30(%rsp), %rdi
callq 0xea792
movq 0x18(%r14), %rdi
movq %rbx, %rsi
movq %rax, %rdx
movq %r15, %rcx
callq 0xefabc
jmp 0x7a976
leaq 0xae0c6(%rip), %rsi     # 0x128d2e
xorl %r15d, %r15d
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x7ae2f
cmpb $0x0, 0x33(%r15)
je 0x7ab73
cmpq $0x280, (%r10)          # imm = 0x280
jl 0x7ab73
cmpq $0x4, 0x10(%r10)
jne 0x7ab73
cmpq $0x4, 0x18(%r10)
jne 0x7ab73
cmpl $0x0, 0x5c(%r10)
jne 0x7ab73
movl (%r12), %eax
cmpl $0x494c424d, %eax       # imm = 0x494C424D
je 0x7acce
cmpl $0x4143424d, %eax       # imm = 0x4143424D
jne 0x7ab73
cmpq $0x0, 0x20(%r12)
je 0x7ab73
xorl %ebx, %ebx
movq %r10, %r14
movq 0x20(%r12), %rdi
movq 0x40(%r14), %rsi
imulq %rbx, %rsi
callq 0xef169
movl %eax, 0x40(%rsp,%rbx,4)
incq %rbx
cmpq $0x4, %rbx
jne 0x7acdf
movq %r12, %rbx
xorl %eax, %eax
movb $0x0, 0x20(%rsp,%rax)
movb $0xf, %cl
subb %al, %cl
movl $0x1, %edx
shll %cl, %edx
xorl %ecx, %ecx
testl %edx, 0x40(%rsp,%rcx,4)
je 0x7ad27
movl $0x1, %esi
shll %cl, %esi
orb %sil, 0x20(%rsp,%rax)
incq %rcx
cmpq $0x4, %rcx
jne 0x7ad15
incq %rax
cmpq $0x10, %rax
jne 0x7ad03
movabsq $0xc0d0408050f020a, %rax # imm = 0xC0D0408050F020A
xorq 0x20(%rsp), %rax
movabsq $0x10f07000b0d060c, %rcx # imm = 0x10F07000B0D060C
xorq 0x27(%rsp), %rcx
orq %rax, %rcx
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r10
movq %rbx, %r12
movq 0x18(%rsp), %r15
jne 0x7ab73
movzbl 0x2f(%rsp), %eax
cmpl $0x8, %eax
je 0x7ad85
cmpl $0x4, %eax
jne 0x7ab73
cmpb $0x0, 0x30(%r15)
je 0x7ae61
leaq 0xae05e(%rip), %rsi     # 0x128df5
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x7ae80
movq 0x20(%r12), %rdi
callq 0xf0e3f
movq 0x18(%r14), %rsi
movq 0x20(%r12), %r8
movq 0x8(%rsp), %rax
movq 0x50(%rax), %r9
movl $0x1, (%rsp)
movq %r14, %rdi
movq %rbx, %rdx
movq 0x30(%rsp), %rcx
callq 0x7b38b
movl %eax, %ebx
movq 0x20(%r12), %rdi
callq 0xf0e67
movq 0x8(%rsp), %r10
testl %ebx, %ebx
jne 0x7aaca
jmp 0x7a86e
movq 0x20(%r12), %r9
movq %r14, %rdi
movq 0x18(%rsp), %rsi
movq %r10, %rdx
movq %rbx, %rcx
movq 0x30(%rsp), %r8
callq 0x7b47a
jmp 0x7a976
leaq 0xc9a41(%rip), %rsi     # 0x144863
xorl %r15d, %r15d
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x8(%rsp), %r10
jmp 0x7a86e
leaq 0xadf6a(%rip), %rsi     # 0x128daa
movq %r14, %rdi
xorl %eax, %eax
callq 0xe9df7
movl $0x1, 0x1c(%r15)
movb $0x1, 0x4a(%r15)
movq 0x8(%rsp), %r10
jmp 0x7ab61
leaq 0xadfcf(%rip), %rsi     # 0x128e37
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x18(%rsp), %rax
movl $0x1, 0x1c(%rax)
movq 0x18(%rsp), %r15
movb $0x1, 0x49(%r15)
movq 0x10(%rsp), %r14
movq 0x8(%rsp), %r10
movq %rbx, %r12
jmp 0x7ab73