// File: /workspace/github_c_makefile_star2_O2/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)
movq %rdi, %r12
xorl %ebx, %ebx
leaq 0x58(%rsp), %r13
leaq 0x28(%rsp), %r14
leaq 0x78(%rsp), %r15
xorl %eax, %eax
movq %rdi, (%rsp)
movq %rdx, 0x10(%rsp)
cmpq 0x28(%rbp), %rax
jge 0x38e6b
movq %rax, 0x8(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0xee7ee
movq 0x18(%r12), %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 0xe58e8
movq 0x28(%rsp), %rax
movq 0x18(%rax), %rbp
movq 0x18(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x38e03
leaq 0x48(%rsp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movl $0x6, 0x44(%rsp)
movl $0xc, 0x50(%rsp)
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
leaq 0x44(%rsp), %r8
callq 0xf644c
jmp 0x38e17
movq %r12, %rdi
movq %r13, %rsi
movq %r14, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0xf4aaa
movq 0x28(%rsp), %rdi
callq 0xe58e8
cmpl $0x0, 0x78(%rsp)
jne 0x38e71
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 0x38ea9
movq 0x8(%rsp), %rax
incq %rax
addq $0x18, %rbx
leaq 0x58(%rsp), %r13
leaq 0x28(%rsp), %r14
leaq 0x78(%rsp), %r15
movq (%rsp), %r12
jmp 0x38d77
movb $0x1, 0x2(%rbp)
jmp 0x38ec4
movq 0x10(%rsp), %rbp
movq 0x18(%rbp), %rbx
leaq 0x78(%rsp), %rsi
movq (%rsp), %r14
movq %r14, %rdi
callq 0xee7bc
leaq 0xd3121(%rip), %rsi     # 0x10bfb3
movq %r14, %rdi
movq %rbx, %rdx
movq 0x8(%rsp), %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0xdf2a5
jmp 0x38ec4
movq 0x18(%rbp), %rdx
leaq 0xd312b(%rip), %rsi     # 0x10bfdf
movq (%rsp), %rdi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0xdf2a5
cmpb $0x0, 0x2(%rbp)
jne 0x38ed3
movq 0x18(%rsp), %rax
movb $0x1, 0x8(%rax)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)