// File: /workspace/github_c_makefile_star2_O3/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 0xe8a1c
cmpl $0x0, 0x1c(%r14)
jne 0x79d2f
cmpb $0x0, 0x32(%r14)
setne %al
testq %r15, %r15
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x79d2f
cmpl $0x0, 0x8(%r15)
je 0x79d6b
xorl %r12d, %r12d
jmp 0x79d37
xorl %r12d, %r12d
testq %r15, %r15
je 0x79d45
movzbl 0x10(%r15), %eax
movb $0x1, 0x8d4(%r14,%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
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 0xe8b2b
movq %rax, %r12
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7aacb
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x7ad0a
testl %eax, %eax
je 0x79e76
cmpl $0x2, 0x14(%r15)
jb 0x79db9
movq 0xc0(%r14), %rax
testq %rax, %rax
jne 0x79dc0
movq 0xb8(%r14), %rax
movq 0x20(%r15), %rcx
testq %rcx, %rcx
jne 0x79dec
movq 0x50(%r12), %rsi
movq %rbx, %rdi
movl $0x1, %edx
movq %rax, 0x10(%rsp)
callq 0xf09c5
movq %rax, %rcx
movq 0x10(%rsp), %rax
movq %rcx, 0x20(%r15)
testq %rax, %rax
je 0x79e05
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x79e05
movq 0x18(%rdi), %rdx
xorl %esi, %esi
callq 0xef481
movzbl 0x10(%r15), %edx
leal -0x3(%rdx), %eax
cmpl $0x5, %eax
ja 0x79e3b
leaq 0xaec3f(%rip), %rcx     # 0x128a58
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 0x7bba1
jmp 0x79eea
cmpl $0x4a, %edx
je 0x79ece
cmpl $0x6c, %edx
jne 0x79e65
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c3b6
jmp 0x79eea
leaq 0xaf247(%rip), %rsi     # 0x1290b3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movl $0x1, 0x1c(%r14)
jmp 0x79d37
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c13d
jmp 0x79eea
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7be47
jmp 0x79eea
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7bf70
jmp 0x79eea
movzbl 0x91e(%r14), %esi
movq %rbx, %rdi
movq %r12, %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %r13, %r9
callq 0x7c272
cmpl $0x0, 0x1c(%r14)
jne 0x79d37
cmpl $0x0, 0xc(%r15)
jne 0x79f03
cmpb $0x0, 0x31(%r14)
je 0x79f19
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x7b190
jmp 0x79d37
leaq 0xabaad(%rip), %rsi     # 0x1259cd
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
jmp 0x79d37