// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/zoo.c
// Function: de_run_zoo_filter
static void de_run_zoo_filter(deark *c, de_module_params *mparams)
{
	dbuf *outf = NULL;
	struct de_crcobj *crco = NULL;
	int use_lzh = 0;
	u32 crc_reported;
	u32 crc_calculated;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;

	if(c->infile->len<6) goto done;

	use_lzh = de_get_ext_option_bool(c, "zoo_filter:lzh", -1);
	if(use_lzh<0) {
		if(dbuf_is_all_zeroes(c->infile, c->infile->len-4, 2)) {
			use_lzh = 1;
		}
		else {
			use_lzh = 0;
		}
	}

	de_declare_fmtf(c, "Zoo filter, LZ%s", (use_lzh?"H":"D"));

	crc_reported = (u32)de_getu32le(c->infile->len-2);
	de_dbg(c, "crc (reported): 0x%04x", (UI)crc_reported);

	outf = dbuf_create_output_file(c, "bin", NULL, 0);
	dbuf_enable_wbuffer(outf);
	crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_ARC);
	dbuf_set_writelistener(outf, de_writelistener_for_crc, (void*)crco);

	de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);
	dcmpri.f = c->infile;
	dcmpri.pos = 2;
	dcmpri.len = c->infile->len - 4;

	dcmpro.f = outf;
	dcmpro.len_known = 0;

	if(use_lzh) {
		decompress_lzh(c, &dcmpri, &dcmpro, &dres);
	}
	else {
		decompress_lzd(c, &dcmpri, &dcmpro, &dres);
	}
	dbuf_flush(dcmpro.f);

	if(dres.errcode) {
		de_err(c, "%s", de_dfilter_get_errmsg(c, &dres));
		goto done;
	}

	crc_calculated = de_crcobj_getval(crco);
	de_dbg(c, "crc (calculated): 0x%04x", (UI)crc_calculated);
	if(crc_calculated != crc_reported) {
		de_err(c, "CRC check failed");
		goto done;
	}

done:
	dbuf_close(outf);
	de_crcobj_destroy(crco);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq 0x18(%rdi), %rax
cmpq $0x6, 0x18(%rax)
jge 0xe0f8e
xorl %r15d, %r15d
xorl %r14d, %r14d
jmp 0xe115d
movq %rdi, %rbx
leaq 0x640df(%rip), %rsi     # 0x145077
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0xe9bd0
movl %eax, %ebp
testl %eax, %eax
jns 0xe0fc6
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x4, %rsi
movl $0x2, %edx
callq 0xf1856
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
testl %ebp, %ebp
leaq 0x6b0d1(%rip), %rax     # 0x14c0a0
leaq 0x612fe(%rip), %rdx     # 0x1422d4
cmoveq %rax, %rdx
leaq 0x640a5(%rip), %rsi     # 0x145086
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb152
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x2, %rsi
callq 0xeed61
leaq 0x63e29(%rip), %rsi     # 0x144e2c
movq %rbx, %rdi
movq %rax, (%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0x547a3(%rip), %rsi     # 0x1357bd
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %r14
movq %rax, %rdi
callq 0xf0855
movq %rbx, %rdi
movl $0x21, %esi
callq 0xeb499
movq %rax, %r15
leaq 0x10409(%rip), %rsi     # 0xf1451
movq %r14, %rdi
movq %rax, %rdx
callq 0xf1412
leaq 0x38(%rsp), %r13
leaq 0x8(%rsp), %r12
leaq 0x58(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xfa1dc
movq 0x18(%rbx), %rax
movq %rax, (%r13)
movq $0x2, 0x8(%r13)
movq 0x18(%rax), %rax
addq $-0x4, %rax
movq %rax, 0x10(%r13)
movq %r14, (%r12)
movb $0x0, 0x8(%r12)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r8
testl %ebp, %ebp
je 0xe10cb
movaps %xmm0, (%r8)
movl $0x5, (%r8)
movb $0x1, 0x4(%r8)
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x58(%rsp), %rcx
movq %rbx, %rdi
callq 0xfec18
jmp 0xe10f6
movups %xmm0, 0x4(%r8)
movl $0x4, (%r8)
movl $0xd, 0xc(%r8)
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x58(%rsp), %rcx
movq %rbx, %rdi
callq 0x102400
movq 0x8(%rsp), %rdi
callq 0xf08ba
cmpl $0x0, 0x58(%rsp)
je 0xe112a
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0xfa1a4
leaq 0x33c44(%rip), %rsi     # 0x114d5f
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9295
jmp 0xe115d
movq %r15, %rdi
callq 0xeb865
movl %eax, %ebp
leaq 0x341a8(%rip), %rsi     # 0x1152e3
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpl (%rsp), %ebp
je 0xe115d
leaq 0x33af8(%rip), %rsi     # 0x114c4b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %r14, %rdi
callq 0xf052b
movq %r15, %rdi
callq 0xeb858
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq