// File: /workspace/github_c_makefile_star2_O1/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 0xe19a1
xorl %r15d, %r15d
xorl %r14d, %r14d
jmp 0xe1b7e
movq %rdi, %rbx
leaq 0x631ec(%rip), %rsi     # 0x144b97
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0xea727
movl %eax, %ebp
testl %eax, %eax
jns 0xe19d9
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x4, %rsi
movl $0x2, %edx
callq 0xf1efb
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
testl %ebp, %ebp
leaq 0x6a09e(%rip), %rax     # 0x14ba80
leaq 0x6040b(%rip), %rdx     # 0x141df4
cmoveq %rax, %rdx
leaq 0x631b2(%rip), %rsi     # 0x144ba6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb703
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rsi
addq $-0x2, %rsi
callq 0xef37e
leaq 0x62f36(%rip), %rsi     # 0x14494c
movq %rbx, %rdi
movq %rax, (%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x53a04(%rip), %rsi     # 0x135431
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r14
movq %rax, %rdi
callq 0xf0e3f
movq %rbx, %rdi
movl $0x21, %esi
callq 0xeba56
movq %rax, %r15
leaq 0xfff6(%rip), %rsi      # 0xf1a51
movq %r14, %rdi
movq %rax, %rdx
callq 0xf1a12
leaq 0x38(%rsp), %r13
leaq 0x8(%rsp), %r12
leaq 0x58(%rsp), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0xfa6e0
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 0xe1ae5
movaps %xmm0, (%r8)
movl $0x5, (%r8)
movw $0x1, 0x4(%r8)
movb $0x0, 0x6(%r8)
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x58(%rsp), %rcx
movq %rbx, %rdi
callq 0xff330
jmp 0xe1b17
movaps %xmm0, (%r8)
movl $0x0, 0x10(%r8)
movl $0x4, (%r8)
movl $0xd, 0xc(%r8)
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x58(%rsp), %rcx
movq %rbx, %rdi
callq 0x102cf0
movq 0x8(%rsp), %rdi
callq 0xf0ea4
cmpl $0x0, 0x58(%rsp)
je 0xe1b4b
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0xfa6a8
leaq 0x33173(%rip), %rsi     # 0x114caf
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9df7
jmp 0xe1b7e
movq %r15, %rdi
callq 0xebe2a
movl %eax, %ebp
leaq 0x336d7(%rip), %rsi     # 0x115233
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpl (%rsp), %ebp
je 0xe1b7e
leaq 0x32ffc(%rip), %rsi     # 0x114b70
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %r14, %rdi
callq 0xf0b13
movq %r15, %rdi
callq 0xebe1d
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq