// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %rdi, %rbp
cmpl $0x0, 0x1c(%rsi)
je 0x7a47b
xorl %esi, %esi
xorl %r15d, %r15d
movq %rbp, %rdi
callq 0xe8be2
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r12
movq %rsi, %r13
xorl %esi, %esi
testq %rdx, %rdx
je 0x7a45e
cmpb $0x0, 0x32(%r13)
movl $0x0, %r15d
je 0x7a461
xorl %r15d, %r15d
cmpl $0x0, 0x8(%r12)
je 0x7a4a4
xorl %esi, %esi
jmp 0x7a461
movl %r9d, %ebx
movq %rcx, %r14
movq %r8, 0x38(%rsp)
movl $0x1, 0x8(%r12)
movl $0x68, %esi
movq %rbp, %rdi
callq 0xe8b2b
movq %r12, 0x30(%rsp)
movq %rax, %r12
movq %rbp, %rdi
movq %r13, %rsi
callq 0x7aacb
movq %rbp, %rdi
movq %r13, 0x18(%rsp)
movq %r13, %rsi
movq %r12, 0x8(%rsp)
movq %r12, %rdx
movl %ebx, %ecx
callq 0x7ad0a
testl %eax, %eax
je 0x7aa66
movq %rbp, 0x10(%rsp)
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rbp
testq %rbp, %rbp
jne 0x7a52f
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq 0x50(%rax), %rsi
movq 0x10(%rsp), %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %rbp
movq %rax, 0x20(%rbx)
movq 0x18(%rsp), %rax
movl 0x4(%rax), %ecx
cmpl $0x5247424e, %ecx       # imm = 0x5247424E
movq 0x8(%rsp), %rax
je 0x7a57b
cmpl $0x52474238, %ecx       # imm = 0x52474238
je 0x7a57b
cmpl $0x4143424d, %ecx       # imm = 0x4143424D
jne 0x7a6cb
movq 0x30(%rsp), %rbx
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
movq %rbp, %rcx
movq %rdi, %rbp
movq %rax, %r12
callq 0x7af85
jmp 0x7a6a5
movq 0x8(%rax), %rdx
imulq (%rax), %rdx
movq %r14, 0x40(%rsp)
movq %rdx, 0x50(%rsp)
testq %rdx, %rdx
jle 0x7a696
addq %r14, 0x38(%rsp)
xorl %r14d, %r14d
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %r15
movq 0x38(%rsp), %rax
cmpq %rax, 0x40(%rsp)
jge 0x7a81a
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
movl %ecx, %r12d
cmpl $0x52474238, %ecx       # imm = 0x52474238
jne 0x7a5ec
callq 0xeed31
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 0x7a62d
callq 0xeeb91
movl %eax, %ecx
shrl $0xc, %ecx
movb %cl, 0x20(%rsp)
movl %eax, %ecx
shrl $0x8, %ecx
andb $0xf, %cl
movb %cl, 0x21(%rsp)
movl %eax, %ecx
shrb $0x4, %cl
movb %cl, 0x22(%rsp)
xorl %esi, %esi
movzbl 0x20(%rsp,%rsi), %ecx
movl %ecx, %edx
shll $0x4, %edx
addl %ecx, %edx
movb %dl, 0x20(%rsp,%rsi)
incq %rsi
cmpq $0x3, %rsi
jne 0x7a611
andl $0x7, %eax
testq %rax, %rax
jne 0x7a662
movq 0x10(%rsp), %rbx
movq 0x18(%rbx), %rdi
leaq 0x40(%rsp), %r13
movq %r13, %rsi
callq 0xeeb4b
testb %al, %al
je 0x7a651
movzbl %al, %eax
jmp 0x7a662
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb91
testq %rax, %rax
jle 0x7a680
movq %rax, %r13
movq %rax, %rbx
movl $0x3, %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0xef547
decq %rbx
jne 0x7a668
movq %r13, %rax
addq %rax, %r14
cmpq 0x50(%rsp), %r14
movl %r12d, %ecx
leaq 0x40(%rsp), %rsi
jl 0x7a5a8
movq 0x30(%rsp), %rbx
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r12
cmpb $0x0, 0x60(%r12)
movq 0x20(%rbx), %rax
je 0x7a6c4
movq 0x18(%rax), %rcx
movq 0x50(%r12), %rdx
cmpq %rdx, %rcx
jne 0x7a71b
movq %rcx, %rdx
jmp 0x7a729
movq 0x50(%r12), %rdx
jmp 0x7a729
movzbl 0x20(%rax), %edx
cmpl $0x2, %edx
je 0x7aa20
cmpl $0x1, %edx
je 0x7a9c7
testl %edx, %edx
jne 0x7aa4d
movq 0x30(%rsp), %rbx
movq 0x50(%rax), %rsi
movq 0x38(%rsp), %rdi
movq %rax, %r12
callq 0xe9c3b
movq 0x10(%rsp), %r8
movq 0x18(%r8), %rdi
movq %r14, %rsi
movq %rax, %rdx
movq %rbp, %rcx
movq %r8, %rbp
callq 0xef481
jmp 0x7a6a5
movq (%r12), %rsi
testb $0x1, %sil
jne 0x7a838
movq %rbx, %r15
movq 0x18(%rax), %rcx
cmpq %rdx, %rcx
je 0x7a746
leaq 0xbdaf2(%rip), %rsi     # 0x13822e
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rsp), %rax
cmpb $0x0, 0x4a(%rax)
je 0x7a787
movq 0x18(%rsp), %rsi
cmpb $0x0, 0x49(%rsi)
jne 0x7a766
cmpb $0x0, 0x38(%rsi)
je 0x7a88e
movq %rbp, %rdi
movq 0x8(%rsp), %rbx
movq %rbx, %rdx
movq %r15, %rcx
callq 0x7b190
movq %rbx, %rsi
movl $0x1, %r15d
jmp 0x7a461
movq 0x20(%r15), %rdi
testq %rdi, %rdi
je 0x7a751
movq 0x8(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
jne 0x7a751
movl (%r15), %eax
cmpl $0x494c424d, %eax       # imm = 0x494C424D
je 0x7a7ac
cmpl $0x4143424d, %eax       # imm = 0x4143424D
jne 0x7a751
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rsi
cmpq $0x40, %rsi
jl 0x7a751
movq 0x18(%rsp), %rax
imulq 0x60(%rax), %rsi
addq $-0x20, %rsi
callq 0x7bb61
testl %eax, %eax
jne 0x7a7f0
movq 0x8(%rsp), %rax
movq 0x40(%rax), %rsi
addq $-0x20, %rsi
movq 0x20(%r15), %rdi
callq 0x7bb61
testl %eax, %eax
je 0x7a751
movq 0x18(%rsp), %rax
cmpb $0x0, 0x2f(%rax)
je 0x7aa70
leaq 0xae793(%rip), %rsi     # 0x128f99
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rsp), %rax
jmp 0x7aa8d
leaq 0xae73d(%rip), %rsi     # 0x128f5e
xorl %r15d, %r15d
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x7aa66
imulq 0x8(%r12), %rsi
cmpq %rsi, %rcx
movq %rbx, %r15
jne 0x7a72c
leaq 0xae5da(%rip), %rsi     # 0x128e2b
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x40(%rcx)
leaq (,%rdx,8), %rax
movq %rax, 0x38(%rcx)
imulq 0x18(%rcx), %rdx
movq %rdx, 0x48(%rcx)
imulq 0x8(%rcx), %rdx
movq %rdx, 0x50(%rcx)
movq 0x20(%r15), %rax
jmp 0x7a72c
cmpb $0x0, 0x33(%rsi)
je 0x7a766
movq 0x8(%rsp), %rax
cmpq $0x280, (%rax)          # imm = 0x280
jl 0x7a766
movq 0x8(%rsp), %rax
cmpq $0x4, 0x10(%rax)
jne 0x7a766
movq 0x8(%rsp), %rax
cmpq $0x4, 0x18(%rax)
jne 0x7a766
movq 0x8(%rsp), %rax
cmpl $0x0, 0x5c(%rax)
jne 0x7a766
movl (%r15), %eax
cmpl $0x494c424d, %eax       # imm = 0x494C424D
je 0x7a8ee
cmpl $0x4143424d, %eax       # imm = 0x4143424D
jne 0x7a766
cmpq $0x0, 0x20(%r15)
je 0x7a766
xorl %ebx, %ebx
movq 0x8(%rsp), %r14
movq 0x20(%r15), %rdi
movq 0x40(%r14), %rsi
imulq %rbx, %rsi
callq 0xeeb67
movl %eax, 0x40(%rsp,%rbx,4)
incq %rbx
cmpq $0x4, %rbx
jne 0x7a900
movq %r15, %rbx
xorl %eax, %eax
movb $0xf, %cl
subb %al, %cl
movl $0x1, %edx
shll %cl, %edx
movb $0x0, 0x20(%rsp,%rax)
xorl %esi, %esi
xorl %ecx, %ecx
testl %edx, 0x40(%rsp,%rcx,4)
je 0x7a94c
movl $0x1, %edi
shll %cl, %edi
orb %dil, %sil
movb %sil, 0x20(%rsp,%rax)
incq %rcx
cmpq $0x4, %rcx
jne 0x7a937
incq %rax
cmpq $0x10, %rax
jne 0x7a923
movabsq $0xc0d0408050f020a, %rax # imm = 0xC0D0408050F020A
xorq 0x20(%rsp), %rax
movabsq $0x10f07000b0d060c, %rcx # imm = 0x10F07000B0D060C
xorq 0x27(%rsp), %rcx
orq %rax, %rcx
movq 0x10(%rsp), %rbp
movq %rbx, %r15
movq 0x18(%rsp), %rsi
jne 0x7a766
movzbl 0x2f(%rsp), %eax
cmpl $0x8, %eax
je 0x7a9a5
cmpl $0x4, %eax
jne 0x7a766
cmpb $0x0, 0x30(%rsi)
je 0x7aa96
leaq 0xae66f(%rip), %rsi     # 0x129025
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0x7aab5
movq %rbp, %rdi
movq 0x30(%rsp), %r13
movq %rax, %r12
callq 0xf0855
movq 0x10(%rsp), %rbp
movq 0x18(%rbp), %rsi
movq 0x20(%r13), %r8
movq 0x50(%r12), %r9
movl $0x1, (%rsp)
movq %rbp, %rdi
movq %r14, %rdx
movq 0x38(%rsp), %rcx
callq 0x7b027
movl %eax, %r14d
movq %r13, %rbx
movq 0x20(%r13), %rdi
callq 0xf087d
testl %r14d, %r14d
jne 0x7a6a5
movq %r12, %rsi
jmp 0x7a461
movq 0x30(%rsp), %rbx
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rax, %rdx
movq %r14, %rcx
movq 0x38(%rsp), %r8
movq %rbp, %r9
movq %rdi, %rbp
movq %rax, %r12
callq 0x7b116
jmp 0x7a6a5
leaq 0xca2ef(%rip), %rsi     # 0x144d43
xorl %r15d, %r15d
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x8(%rsp), %rsi
jmp 0x7a461
leaq 0xae563(%rip), %rsi     # 0x128fda
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x18(%rsp), %rax
movl $0x1, 0x1c(%rax)
movb $0x1, 0x4a(%rax)
jmp 0x7a751
leaq 0xae5ca(%rip), %rsi     # 0x129067
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x18(%rsp), %rax
movl $0x1, 0x1c(%rax)
movq 0x18(%rsp), %rsi
movb $0x1, 0x49(%rsi)
movq 0x10(%rsp), %rbp
movq %rbx, %r15
jmp 0x7a766