// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/aldus.c
// Function: decompress_fork
static void decompress_fork(deark *c, lctx *d, struct fork_data *fkd,
	dbuf *outf)
{
	i64 i;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;

	for(i=0; i<fkd->nsegs; i++) {
		i64 len1, len_diff;

		de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);

		dcmpri.f = c->infile;
		dcmpri.pos = fkd->sdata[i].cmpr_pos;
		dcmpri.len = fkd->sdata[i].cmpr_len;
		dcmpro.f = outf;

		dbuf_flush(dcmpro.f);
		len1 = dcmpro.f->len;

		if(d->fmtcode==1) {
			struct de_lzw_params delzwp;

			de_zeromem(&delzwp, sizeof(struct de_lzw_params));
			delzwp.fmt = DE_LZWFMT_TIFFNEW;
			delzwp.max_code_size = 12;
			fmtutil_decompress_lzw(c, &dcmpri, &dcmpro, &dres, &delzwp);
		}
		else {
			// Each segment seems to be compressed independently. They don't
			// share a history buffer.
			// TODO: It's not efficient to start over for each segment, because
			// DCL Implode has some overhead. We should have a better way to do it.
			fmtutil_dclimplode_codectype1(c, &dcmpri, &dcmpro, &dres, NULL);
		}
		dbuf_flush(dcmpro.f);

		if(dres.errcode) {
			de_err(c, "[%s fork, seg %"I64_FMT"] Decompression failed: %s",
				fkd->forkname, i, de_dfilter_get_errmsg(c, &dres));
			goto done;
		}

		len_diff = dcmpro.f->len - len1;
		if(len_diff != fkd->sdata[i].orig_len) {
			de_err(c, "[%s fork, seg %"I64_FMT"] Expected %"I64_FMT" bytes, "
				"got %"I64_FMT, fkd->forkname, i, fkd->sdata[i].orig_len,
				len_diff);
			goto done;
		}
	}

	fkd->decompress_succeeded = 1;

done:
	if(!fkd->decompress_succeeded) {
		d->errflag = 1;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, %rbp
movq %rsi, 0x18(%rsp)
cmpq $0x0, 0x28(%rdx)
jle 0x3a597
movq %rdi, %r15
xorl %ebx, %ebx
leaq 0x58(%rsp), %r13
leaq 0x28(%rsp), %r14
leaq 0x78(%rsp), %rax
xorl %r12d, %r12d
movq %rdi, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %rax, %rcx
callq 0xfa1dc
movq 0x18(%r15), %rax
movq %rax, 0x58(%rsp)
movq 0x38(%rbp), %rax
movups (%rax,%rbx), %xmm0
movups %xmm0, 0x60(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xf08ba
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rbp
movq 0x18(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x3a52c
leaq 0x48(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl $0x6, 0x44(%rsp)
movl $0xc, 0x50(%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
leaq 0x78(%rsp), %rcx
leaq 0x44(%rsp), %r8
callq 0x102400
jmp 0x3a542
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
leaq 0x78(%rsp), %rcx
xorl %r8d, %r8d
callq 0x1009eb
movq 0x28(%rsp), %rdi
callq 0xf08ba
cmpl $0x0, 0x78(%rsp)
jne 0x3a59d
movq 0x28(%rsp), %rax
movq 0x18(%rax), %r9
subq %rbp, %r9
movq 0x10(%rsp), %rbp
movq 0x38(%rbp), %rax
movq 0x10(%rax,%rbx), %r8
cmpq %r8, %r9
jne 0x3a5d4
incq %r12
addq $0x18, %rbx
cmpq 0x28(%rbp), %r12
leaq 0x58(%rsp), %r13
leaq 0x28(%rsp), %r14
movq 0x8(%rsp), %r15
leaq 0x78(%rsp), %rax
jl 0x3a4ae
movb $0x1, 0x2(%rbp)
jmp 0x3a5ee
movq 0x10(%rsp), %rbp
movq 0x18(%rbp), %rbx
leaq 0x78(%rsp), %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xfa1a4
leaq 0xdeae4(%rip), %rsi     # 0x1190a3
movq %r14, %rdi
movq %rbx, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xe9295
jmp 0x3a5ee
movq 0x18(%rbp), %rdx
leaq 0xdeaf0(%rip), %rsi     # 0x1190cf
movq 0x8(%rsp), %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xe9295
cmpb $0x0, 0x2(%rbp)
jne 0x3a5fd
movq 0x18(%rsp), %rax
movb $0x1, 0x8(%rax)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop