// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/ilbm.c
// Function: do_dlta
static void do_dlta(deark *c, lctx *d, i64 pos1, i64 len)
{
	struct frame_ctx *frctx = d->frctx;
	struct frame_ctx *reference_frctx = NULL;
	struct imgbody_info *ibi = NULL;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	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;

	// TODO: Should the imgbody_info be saved somewhere, or recalculated for every frame?
	ibi = de_malloc(c, sizeof(struct imgbody_info));

	do_before_image_chunk(c, d);

	if(!init_imgbody_info(c, d, ibi, 0)) {
		d->errflag = 1;
		goto done;
	}

	// Find the reference frame (if it exists).
	// It is the highest numbered oldfrctx[] item whose index is at most
	// frctx->interleave-1, and which is non-NULL.
	if(frctx->interleave>=2) {
		reference_frctx = d->oldfrctx[1];
		if(!reference_frctx) {
			reference_frctx = d->oldfrctx[0];
		}
	}
	else {
		reference_frctx = d->oldfrctx[0];
	}

	// Allocate buffer for this frame
	if(!frctx->frame_buffer) {
		frctx->frame_buffer = dbuf_create_membuf(c, ibi->frame_buffer_size, 0x1);
	}

	// Start by copying the reference frame to this frame. The decompress function
	// will then modify this frame.
	if(reference_frctx && reference_frctx->frame_buffer) {
		dbuf_copy(reference_frctx->frame_buffer, 0, reference_frctx->frame_buffer->len,
			frctx->frame_buffer);
	}

	switch(frctx->op) {
	case 3:
		decompress_delta_op3(c, d, ibi, frctx, pos1, len);
		break;
	case 5:
		decompress_delta_op5(c, d, ibi, frctx, pos1, len);
		break;
	case 7:
		decompress_delta_op7(c, d, ibi, frctx, pos1, len);
		break;
	case 8:
		decompress_delta_op8(c, d, ibi, frctx, pos1, len);
		break;
	case 74:
		decompress_delta_op74(c, d, ibi, frctx, pos1, len);
		break;
	case 108:
		decompress_delta_op108(c, d, ibi, frctx, pos1, len);
		break;
	default:
		de_err(c, "Unsupported DLTA operation: %d", (int)frctx->op);
		d->errflag = 1;
	}
	if(d->errflag) goto done;

	if(frctx->change_flag || d->opt_anim_includedups) {
		write_frame(c, d, ibi, frctx);
	}
	else {
		de_dbg(c, "[suppressing duplicate frame]");
	}

done:
	// For the summary line, and so we can know when encountering an op for the
	// first time.
	if(frctx) {
		d->delta_ops_used[(size_t)frctx->op] = 1;
	}

	de_free(c, ibi);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r13
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %rbx
movq 0xb0(%rsi), %r15
leaq 0xc(%rsp), %rsi
callq 0xe958c
cmpl $0x0, 0x1c(%r14)
jne 0x7a132
cmpb $0x0, 0x32(%r14)
setne %al
testq %r15, %r15
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7a132
cmpl $0x0, 0x8(%r15)
je 0x7a16e
xorl %r12d, %r12d
testq %r15, %r15
je 0x7a148
movzbl 0x10(%r15), %eax
movb $0x1, 0x8d4(%r14,%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe9752
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, 0x8(%r15)
movl $0x68, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7ae9c
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x7b076
testl %eax, %eax
je 0x7a23d
cmpl $0x2, 0x14(%r15)
jb 0x7a1bc
movq 0xc0(%r14), %rax
testq %rax, %rax
jne 0x7a1c3
movq 0xb8(%r14), %rax
cmpq $0x0, 0x20(%r15)
jne 0x7a1ea
movq 0x50(%r12), %rsi
movq %rbx, %rdi
movl $0x1, %edx
movq %rax, 0x10(%rsp)
callq 0xf0faf
movq %rax, 0x20(%r15)
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x7a207
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x7a207
movq 0x18(%rdi), %rdx
movq 0x20(%r15), %rcx
xorl %esi, %esi
callq 0xefabc
movzbl 0x10(%r15), %edx
leal -0x3(%rdx), %eax
cmpl $0x5, %eax
ja 0x7a24a
leaq 0xae60d(%rip), %rcx     # 0x128828
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %rbp, %rcx
movq %r13, %r8
callq 0x7bf66
jmp 0x7a2ee
movl $0x1, 0x1c(%r14)
jmp 0x7a135
cmpl $0x4a, %edx
je 0x7a2d7
cmpl $0x6c, %edx
jne 0x7a271
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c7be
jmp 0x7a2ee
leaq 0xaec0b(%rip), %rsi     # 0x128e83
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movl $0x1, 0x1c(%r14)
jmp 0x7a2ee
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c530
jmp 0x7a2ee
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c23a
jmp 0x7a2ee
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c363
jmp 0x7a2ee
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c665
cmpl $0x0, 0x1c(%r14)
jne 0x7a135
cmpl $0x0, 0xc(%r15)
jne 0x7a307
cmpb $0x0, 0x31(%r14)
je 0x7a31d
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x7b4f6
jmp 0x7a135
leaq 0xab479(%rip), %rsi     # 0x12579d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
jmp 0x7a135