// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/lbr.c
// Function: de_run_squeeze
static void de_run_squeeze(deark *c, de_module_params *mparams)
{
	i64 pos = 0;
	i64 n;
	struct squeeze_ctx *sqctx = NULL;
	de_finfo *fi = NULL;
	struct de_crcobj *crco = NULL;
	dbuf *outf_tmp = NULL;
	dbuf *outf_final = NULL;
	int saved_indent_level;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;
	struct de_dcmpr_two_layer_params tlp;

	de_dbg_indent_save(c, &saved_indent_level);
	sqctx = de_malloc(c, sizeof(struct squeeze_ctx));
	sqctx->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);

	n = de_getu16le_p(&pos);
	if(n==0xff76) {
		de_declare_fmt(c, "Squeezed");
	}
	else if(n==0xfffa) {
		de_declare_fmt(c, "Squeeze v2 (SQ2)");
		sqctx->is_sq2 = 1;
	}
	else {
		de_dbg(c, "Not a Squeezed file");
		goto done;
	}

	if(sqctx->is_sq2) {
		if(!read_sq2_headers(c, sqctx, pos)) goto done;
	}
	else {
		if(!read_squeeze_headers(c, sqctx, pos)) goto done;
	}

	pos = sqctx->cmpr_data_pos;

	fi = de_finfo_create(c);
	de_finfo_set_name_from_ucstring(c, fi, sqctx->fnd.fn, 0);
	fi->original_filename_flag = 1;

	de_dbg(c, "squeeze-compressed data at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	// We have to decompress the file before we can find the timestamp. That's
	// why we decompress to a membuf.
	outf_tmp = dbuf_create_membuf(c, 0, 0);
	dbuf_enable_wbuffer(outf_tmp);

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

	crco = de_crcobj_create(c, DE_CRCOBJ_SUM_BYTES);
	dbuf_set_writelistener(outf_tmp, de_writelistener_for_crc, (void*)crco);

	de_zeromem(&tlp, sizeof(struct de_dcmpr_two_layer_params));
	tlp.codec1_type1 = fmtutil_huff_squeeze_codectype1;
	tlp.codec2 = dfilter_rle90_codec;
	tlp.dcmpri = &dcmpri;
	tlp.dcmpro = &dcmpro;
	tlp.dres = &dres;
	de_dfilter_decompress_two_layer(c, &tlp);
	dbuf_flush(dcmpro.f);

	if(dres.bytes_consumed_valid) {
		de_dbg(c, "compressed data size: %"I64_FMT", ends at %"I64_FMT, dres.bytes_consumed,
			dcmpri.pos+dres.bytes_consumed);

		do_sqeeze_timestamp(c, sqctx, dcmpri.pos+dres.bytes_consumed);
		if(sqctx->timestamp.is_valid) {
			fi->timestamp[DE_TIMESTAMPIDX_MODIFY] = sqctx->timestamp;
		}
	}

	outf_final = dbuf_create_output_file(c, NULL, fi, 0);
	dbuf_copy(outf_tmp, 0, outf_tmp->len, outf_final);

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

	sqctx->checksum_calc = de_crcobj_getval(crco);
	sqctx->checksum_calc &= 0xffff;
	de_dbg(c, "checksum (calculated): %u", (UI)sqctx->checksum_calc);
	if(sqctx->checksum_calc != sqctx->checksum_reported) {
		de_err(c, "Checksum error. Decompression probably failed.");
		goto done;
	}

done:
	if(sqctx) {
		crcr_filename_data_freecontents(c, &sqctx->fnd);
		de_destroy_stringreaderdata(c, sqctx->sq2_timestamp_string);
		de_destroy_stringreaderdata(c, sqctx->sq2_comment);
		de_free(c, sqctx);
	}
	dbuf_close(outf_final);
	dbuf_close(outf_tmp);
	de_finfo_destroy(c, fi);
	de_crcobj_destroy(crco);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdi, %rbx
leaq 0x8(%rsp), %r15
andq $0x0, (%r15)
leaq 0x1c(%rsp), %rsi
callq 0xdebae
pushq $0x50
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, 0x4(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
cmpq $0xff76, %rax           # imm = 0xFF76
je 0x99237
cmpq $0xfffa, %rax           # imm = 0xFFFA
jne 0x99353
leaq 0x8bcea(%rip), %rsi     # 0x124f13
movq %rbx, %rdi
callq 0xe07f7
movb $0x1, (%r14)
jmp 0x99250
leaq 0x8bccc(%rip), %rsi     # 0x124f0a
movq %rbx, %rdi
callq 0xe07f7
cmpb $0x0, (%r14)
je 0x9936c
movq 0x8(%rsp), %r15
movq %r15, 0x38(%rsp)
leaq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9aba2
testl %eax, %eax
je 0x9968a
addq 0x18(%r14), %r15
movq %r15, 0x38(%rsp)
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %r9d
pushq $0x1
popq %r8
movl $0x12c, %edx            # imm = 0x12C
movl $0x12c, %ecx            # imm = 0x12C
movq %r15, %rsi
callq 0xe495c
movq %rax, 0x20(%r14)
cmpl $0x0, 0x34(%rax)
je 0x9968a
movq 0x18(%rax), %rdi
callq 0xeb877
leaq 0x8bcf7(%rip), %rsi     # 0x124fad
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x20(%r14), %rax
addq (%rax), %r15
movq %r15, 0x38(%rsp)
movq 0x18(%rbx), %rdi
movl 0x4(%r14), %r9d
pushq $0x1
popq %r8
movl $0x12c, %edx            # imm = 0x12C
movl $0x12c, %ecx            # imm = 0x12C
movq %r15, %rsi
callq 0xe495c
movq %rax, 0x28(%r14)
cmpl $0x0, 0x34(%rax)
je 0x9968a
movq 0x18(%rax), %rdi
callq 0xeb877
leaq 0x6e4ea(%rip), %rsi     # 0x1077f5
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x28(%r14), %rax
addq (%rax), %r15
leaq 0x38(%rsp), %rsi
movq %r15, (%rsi)
movq 0x18(%rbx), %rdi
callq 0xe3f2a
cmpb $0x1a, %al
jne 0x9968a
movq 0x38(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9adc0
movq 0x38(%rsp), %r15
addq $0x6, %r15
jmp 0x9939e
leaq 0x8bbca(%rip), %rsi     # 0x124f24
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
jmp 0x9969e
movq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x9adc0
addq $0x2, %r15
leaq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9aba2
testl %eax, %eax
je 0x9968a
addq 0x18(%r14), %r15
movq %r15, 0x38(%r14)
movq %r15, 0x8(%rsp)
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, %r15
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movq %rax, %rsi
xorl %ecx, %ecx
callq 0xdfd55
movb $0x1, 0x8(%r15)
movq 0x8(%rsp), %rdx
leaq 0x8bb64(%rip), %rsi     # 0x124f38
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
movq %rax, %rdi
callq 0xe589c
leaq 0xb0(%rsp), %rbp
leaq 0x98(%rsp), %rdx
leaq 0x38(%rsp), %rcx
movq %rbx, %rdi
movq %rbp, %rsi
movq %rdx, %r13
callq 0xee7ee
movq 0x18(%rbx), %rax
movq %rax, (%rbp)
movq 0x8(%rsp), %rcx
movq %rcx, 0x8(%rbp)
movq 0x18(%rax), %rax
subq %rcx, %rax
movq %rax, 0x10(%rbp)
movq %r12, (%r13)
pushq $0x30
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, %r13
leaq 0x4ce69(%rip), %rsi     # 0xe62c1
movq %r12, %rdi
movq %rax, %rdx
callq 0xe629a
xorps %xmm0, %xmm0
leaq 0x118(%rsp), %rsi
movups %xmm0, 0x18(%rsi)
movups %xmm0, 0x28(%rsi)
movups %xmm0, 0x38(%rsi)
andq $0x0, 0x48(%rsi)
movups %xmm0, 0x8(%rsi)
leaq 0x5626d(%rip), %rax     # 0xef6f7
movq %rax, (%rsi)
leaq 0x559af(%rip), %rax     # 0xeee43
movq %rax, 0x18(%rsi)
movq %rbp, 0x28(%rsi)
leaq 0x98(%rsp), %rax
movq %rax, 0x30(%rsi)
leaq 0x38(%rsp), %rbp
movq %rbp, 0x38(%rsi)
movq %rbx, %rdi
callq 0xef577
leaq 0x98(%rsp), %rax
movq (%rax), %rdi
callq 0xe58e8
cmpb $0x0, 0x4(%rbp)
je 0x99638
movq 0x40(%rsp), %rdx
movq 0xb8(%rsp), %rcx
addq %rdx, %rcx
leaq 0x8ba6d(%rip), %rsi     # 0x124f57
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x40(%rsp), %rbp
addq 0xb8(%rsp), %rbp
movq %rbp, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq 0x18(%rdi), %rax
subq %rbp, %rax
cmpq $0x8, %rax
jl 0x99625
leaq 0x10(%rsp), %rsi
callq 0xe3fc4
cmpq $0xff77, %rax           # imm = 0xFF77
jne 0x99625
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3fc4
movq %rax, 0x30(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3fc4
movq %rax, 0x28(%rsp)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe3fc4
movq %rax, 0x20(%rsp)
movq 0x18(%rbx), %rdi
pushq $0x6
popq %rdx
pushq $0x31
popq %rcx
movq %rbp, %rsi
callq 0xe0f6a
movzwl %ax, %eax
cmpl 0x20(%rsp), %eax
jne 0x99625
leaq 0x8ba69(%rip), %rsi     # 0x124ff9
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x40(%r14), %rbp
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq 0xe0110
movb $0x2, 0x41(%r14)
leaq 0xd0(%rsp), %rsi
pushq $0x40
popq %rdx
movq %rbp, %rdi
movq %rsi, %rbp
xorl %ecx, %ecx
callq 0xe0644
leaq 0x728d5(%rip), %rsi     # 0x10beb7
movq %rbx, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x8ba14(%rip), %rsi     # 0x12500a
movq %rbx, %rdi
movq 0x20(%rsp), %rbp
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
leaq 0x8ba24(%rip), %rsi     # 0x125032
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, 0x40(%r14)
je 0x99638
leaq 0x40(%r14), %rax
movups (%rax), %xmm0
movups %xmm0, 0x18(%r15)
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, %rbp
movq 0x18(%r12), %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %rax, %rcx
callq 0xe45f4
cmpl $0x0, 0x38(%rsp)
je 0x99716
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0xee7bc
leaq 0x6db46(%rip), %rsi     # 0x1071c1
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xdf2a5
jmp 0x996a6
leaq 0x8b933(%rip), %rsi     # 0x124fc4
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r14, %rdi
addq $0x8, %rdi
callq 0x9ab8c
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xe4b94
movq 0x28(%r14), %rsi
movq %rbx, %rdi
callq 0xe4b94
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movq %rbp, %rdi
callq 0xe5591
movq %r12, %rdi
callq 0xe5591
movq %rbx, %rdi
movq %r15, %rsi
callq 0xdfd24
movq %r13, %rdi
callq 0xe0e8d
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, %rdi
callq 0xe0e95
movzwl %ax, %edx
movl %edx, 0x34(%r14)
leaq 0x73c19(%rip), %rsi     # 0x10d345
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movl 0x34(%r14), %eax
cmpl 0x30(%r14), %eax
je 0x996a6
leaq 0x8b833(%rip), %rsi     # 0x124f7e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x996a6