// File: /workspace/github_c_makefile_star2_O1/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, 0x38(%rsp)
movq %rdx, %rbp
movq %rsi, 0x18(%rsp)
cmpq $0x0, 0x28(%rdx)
jle 0x3a6b6
movq %rdi, %r15
xorl %ebx, %ebx
leaq 0x58(%rsp), %r13
leaq 0x40(%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 0xfa6e0
movq 0x18(%r15), %rax
movq %rax, 0x58(%rsp)
movq 0x38(%rbp), %rax
movq (%rax,%rbx), %rcx
movq %rcx, 0x60(%rsp)
movq 0x8(%rax,%rbx), %rax
movq %rax, 0x68(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xf0ea4
movq 0x40(%rsp), %rax
movq 0x18(%rax), %rbp
movq 0x18(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x3a64b
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x6, 0x20(%rsp)
movl $0xc, 0x2c(%rsp)
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
leaq 0x78(%rsp), %rcx
leaq 0x20(%rsp), %r8
callq 0x102cf0
jmp 0x3a661
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
leaq 0x78(%rsp), %rcx
xorl %r8d, %r8d
callq 0x10117f
movq 0x40(%rsp), %rdi
callq 0xf0ea4
cmpl $0x0, 0x78(%rsp)
jne 0x3a6bc
movq 0x40(%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 0x3a6f3
incq %r12
addq $0x18, %rbx
cmpq 0x28(%rbp), %r12
leaq 0x58(%rsp), %r13
leaq 0x40(%rsp), %r14
movq 0x8(%rsp), %r15
leaq 0x78(%rsp), %rax
jl 0x3a5be
movb $0x1, 0x2(%rbp)
jmp 0x3a70d
movq 0x10(%rsp), %rbp
movq 0x18(%rbp), %rbx
leaq 0x78(%rsp), %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0xfa6a8
leaq 0xde8a5(%rip), %rsi     # 0x118f83
movq %r14, %rdi
movq %rbx, %rdx
movq %r12, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xe9df7
jmp 0x3a70d
movq 0x18(%rbp), %rdx
leaq 0xde8b1(%rip), %rsi     # 0x118faf
movq 0x8(%rsp), %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xe9df7
cmpb $0x0, 0x2(%rbp)
jne 0x3a71c
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