// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: de_dfilter_decompress_two_layer
void de_dfilter_decompress_two_layer(deark *c, struct de_dcmpr_two_layer_params *tlp)
{
	dbuf *outf_codec1 = NULL;
	struct de_dfilter_out_params dcmpro_codec1;
	struct de_dfilter_results dres_codec2;
	struct my_2layer_userdata u;
	struct de_dfilter_ctx *dfctx_codec2 = NULL;

	de_dfilter_init_objects(c, NULL, &dcmpro_codec1, NULL);
	de_dfilter_init_objects(c, NULL, NULL, &dres_codec2);
	de_zeromem(&u, sizeof(struct my_2layer_userdata));

	// Make a custom dbuf. The output from the first decompressor will be written
	// to it, and it will relay that output to the second decompressor.
	outf_codec1 = dbuf_create_custom_dbuf(c, 0, 0);
	dbuf_enable_wbuffer(outf_codec1);
	outf_codec1->userdata_for_customwrite = (void*)&u;
	outf_codec1->customwrite_fn = my_2layer_write_cb;

	dcmpro_codec1.f = outf_codec1;
	if(tlp->intermed_len_known) {
		dcmpro_codec1.len_known = 1;
		dcmpro_codec1.expected_len = tlp->intermed_expected_len;
	}
	else {
		dcmpro_codec1.len_known = 0;
		dcmpro_codec1.expected_len = 0;
	}

	dfctx_codec2 = de_dfilter_create(c, tlp->codec2, tlp->codec2_private_params, tlp->dcmpro, &dres_codec2);
	u.dfctx_codec2 = dfctx_codec2;

	// The first codec in the chain does not need the advanced (de_dfilter_create) API.
	if(tlp->codec1_type1) {
		tlp->codec1_type1(c, tlp->dcmpri, &dcmpro_codec1, tlp->dres, tlp->codec1_private_params);
	}
	else {
		de_dfilter_decompress_oneshot(c, tlp->codec1_pushable, tlp->codec1_private_params,
			tlp->dcmpri, &dcmpro_codec1, tlp->dres);
	}
	dbuf_flush(outf_codec1);
	de_dfilter_finish(dfctx_codec2);

	if(tlp->dres->errcode) goto done;
	de_dbg2(c, "size after intermediate decompression: %"I64_FMT, u.intermediate_nbytes);

	if(dres_codec2.errcode) {
		// An error occurred in codec2, and not in codec1.
		// Copy the error info to the dres that will be returned to the caller.
		de_dfilter_transfer_error(c, &dres_codec2, tlp->dres);
		goto done;
	}

done:
	de_dfilter_destroy(dfctx_codec2);
	dbuf_close(outf_codec1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rsi, %r14
xorl %r12d, %r12d
movq %r12, 0x20(%rsp)
movl $0x0, 0x48(%rsp)
movb %r12b, 0x4c(%rsp)
movq %r12, 0x50(%rsp)
movb %r12b, 0x58(%rsp)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r13
movaps %xmm0, (%r13)
movq %rdi, 0x8(%rsp)
xorl %esi, %esi
xorl %edx, %edx
callq 0xf13d3
movq %rax, %rbx
movq %rax, %rdi
callq 0xf0855
movq %r13, 0xa8(%rbx)
leaq 0x14d(%rip), %rax       # 0xfb2bf
movq %rax, 0xb0(%rbx)
movq %rbx, 0x18(%rsp)
movl $0x0, %eax
cmpb %r12b, 0x40(%r14)
je 0xfb190
movq 0x48(%r14), %rax
movb $0x1, %r12b
movb %r12b, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq 0x18(%r14), %rbp
movq 0x20(%r14), %rax
movq %rax, 0x10(%rsp)
movq 0x30(%r14), %r15
movl $0x50, %esi
movq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0xe8b2b
movq %rax, %r12
movq %r13, (%rax)
leaq 0x48(%rsp), %rax
movq %rax, 0x8(%r12)
movq %r15, 0x10(%r12)
testq %rbp, %rbp
je 0xfb1e1
movq %r12, %rdi
movq 0x10(%rsp), %rsi
callq *%rbp
movq %r12, 0x30(%rsp)
movq (%r14), %rax
testq %rax, %rax
je 0xfb208
movq 0x10(%r14), %r8
movq 0x28(%r14), %rsi
movq 0x38(%r14), %rcx
leaq 0x18(%rsp), %rdx
movq 0x8(%rsp), %rdi
callq *%rax
jmp 0xfb227
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq 0x28(%r14), %rcx
movq 0x38(%r14), %r9
leaq 0x18(%rsp), %r8
movq 0x8(%rsp), %rdi
callq 0xfa46f
movq %rbx, %rdi
callq 0xf08ba
movq 0x40(%r12), %rax
testq %rax, %rax
je 0xfb23e
movq %r12, %rdi
callq *%rax
movq 0x38(%r14), %rax
cmpl $0x0, (%rax)
je 0xfb27f
movq (%r12), %r14
movq 0x48(%r12), %rax
testq %rax, %rax
je 0xfb25a
movq %r12, %rdi
callq *%rax
movq %r14, %rdi
movq %r12, %rsi
callq 0xe8be2
movq %rbx, %rdi
callq 0xf052b
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rsp), %rdx
leaq 0x4f899(%rip), %rsi     # 0x14ab24
movq 0x8(%rsp), %rdi
xorl %eax, %eax
callq 0xe87e4
movl 0x48(%rsp), %eax
testl %eax, %eax
je 0xfb247
movq 0x38(%r14), %rdi
cmpl $0x0, (%rdi)
jne 0xfb247
leaq 0x58(%rsp), %rsi
movl %eax, (%rdi)
addq $0x10, %rdi
movl $0x50, %edx
callq 0xe8219
jmp 0xfb247