// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/corel.c
// Function: do_decompress
static void do_decompress(deark *c, lctx *d)
{
	u32 cmprmeth;
	i64 pos = d->pack_dpos;
	i64 cmpr_start;
	i64 cmpr_len;
	i64 unc_len;
	i64 unc_len_padded;
	i64 unc_riff_size;
	i64 after_pack_pos;
	i64 after_pack_len;
	dbuf *outf = NULL;
	dbuf *unc_data = NULL;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;
	struct de_deflate_params inflparams;

	de_zeromem(&inflparams, sizeof(struct de_deflate_params));
	if(d->pack_dlen < 12) goto done;
	unc_len = de_getu32le_p(&pos);
	de_dbg(c, "uncompressed len (reported): %"I64_FMT, unc_len);
	if(unc_len > DE_MAX_SANE_OBJECT_SIZE) goto done;

	cmprmeth = (u32)de_getu32be_p(&pos);
	if(cmprmeth != CODE_CPng) {
		de_err(c, "Unsupported compression method");
		goto done;
	}
	pos += 4; // Unknown field

	unc_len_padded = de_pad_to_2(unc_len);
	cmpr_start = pos;
	cmpr_len = d->pack_dpos + d->pack_dlen - cmpr_start;
	if(cmpr_len<1) goto done;

	// Decompress the "pack" chunk to a membuf.
	// We *could* decompress directly to the output file instead, but this way
	// is more flexible.
	unc_data = dbuf_create_membuf(c, unc_len_padded, 0x1);
	de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);
	dcmpri.f = c->infile;
	dcmpri.pos = cmpr_start;
	dcmpri.len = cmpr_len;
	dcmpro.f = unc_data;
	dcmpro.len_known = 1;
	dcmpro.expected_len = unc_len;
	inflparams.flags = DE_DEFLATEFLAG_ISZLIB;
	fmtutil_decompress_deflate_ex(c, &dcmpri, &dcmpro, &dres, &inflparams);

	if(dres.errcode) {
		de_err(c, "%s", dres.errmsg);
		goto done;
	}
	if(unc_data->len < unc_len) {
		de_warn(c, "Decompression may have failed (expected %"I64_FMT" bytes, got %"I64_FMT")",
			unc_len, unc_data->len);
	}
	dbuf_truncate(unc_data, unc_len_padded);

	after_pack_pos = d->pack_dpos + de_pad_to_2(d->pack_dlen);
	after_pack_len = de_pad_to_2(c->infile->len - after_pack_pos);
	unc_riff_size = d->pack_pos + unc_data->len + after_pack_len - 8;

	outf = dbuf_create_output_file(c, "cmx", NULL, 0);
	d->wrote_cmx_file = 1;

	dbuf_writeu32be(outf, CODE_RIFF);
	// Use the new the RIFF size
	dbuf_writeu32le(outf, unc_riff_size);
	// Change the RIFF type: CDRX -> CMX1
	dbuf_writeu32be(outf, CODE_CMX1);

	// Copy everything else, up to the "pack" chunk
	dbuf_copy(c->infile, 12, d->pack_pos-12, outf);

	// Copy the decompressed contents of the "pack" chunk
	dbuf_copy(unc_data, 0, unc_data->len, outf);

	// Copy everything after the "pack" chunk
	dbuf_copy(c->infile, after_pack_pos, after_pack_len, outf);

done:
	dbuf_close(unc_data);
	dbuf_close(outf);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x19140
movq -0x10(%rbp), %rax
cmpq $0xc, 0x10(%rax)
jge 0x78972
jmp 0x78c2a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x165580
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x133c36(%rip), %rsi    # 0x1ac5cc
movb $0x0, %al
callq 0x15b0d0
cmpq $0x5f5e100, -0x38(%rbp) # imm = 0x5F5E100
jle 0x789ac
jmp 0x78c2a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1654b0
movl %eax, -0x14(%rbp)
cmpl $0x43506e67, -0x14(%rbp) # imm = 0x43506E67
je 0x789e0
movq -0x8(%rbp), %rdi
leaq 0x133c19(%rip), %rsi    # 0x1ac5ed
movb $0x0, %al
callq 0x15bfb0
jmp 0x78c2a
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x38(%rbp), %rdi
callq 0x15cdf0
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x10(%rcx), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x1, -0x30(%rbp)
jge 0x78a25
jmp 0x78c2a
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
leaq -0x100(%rbp), %rcx
callq 0x1773f0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xa0(%rbp)
movb $0x1, -0x98(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x90(%rbp)
movl $0x1, -0x110(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
leaq -0x100(%rbp), %rcx
leaq -0x110(%rbp), %r8
callq 0x17f4b0
cmpl $0x0, -0x100(%rbp)
je 0x78aef
movq -0x8(%rbp), %rdi
leaq -0x100(%rbp), %rdx
addq $0x10, %rdx
leaq 0x12804c(%rip), %rsi    # 0x1a0b2f
movb $0x0, %al
callq 0x15bfb0
jmp 0x78c2a
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rax
cmpq -0x38(%rbp), %rax
jge 0x78b1b
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0x133af8(%rip), %rsi    # 0x1ac60c
movb $0x0, %al
callq 0x15bc10
movq -0x68(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x118(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x15cdf0
movq %rax, %rcx
movq -0x118(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdi
subq -0x50(%rbp), %rdi
callq 0x15cdf0
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x68(%rbp), %rcx
addq 0x18(%rcx), %rax
addq -0x58(%rbp), %rax
subq $0x8, %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x133ab4(%rip), %rsi    # 0x1ac648
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0x60(%rbp), %rdi
movl $0x52494646, %esi       # imm = 0x52494646
callq 0x168b80
movq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x168c30
movq -0x60(%rbp), %rdi
movl $0x434d5831, %esi       # imm = 0x434D5831
callq 0x168b80
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
subq $0xc, %rdx
movq -0x60(%rbp), %rcx
movl $0xc, %esi
callq 0x166070
movq -0x68(%rbp), %rdi
movq -0x68(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x60(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x166070
movq -0x68(%rbp), %rdi
callq 0x1676a0
movq -0x60(%rbp), %rdi
callq 0x1676a0
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop