// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/lbr.c
// Function: de_run_lzwcom
static void de_run_lzwcom(deark *c, de_module_params *mparams)
{
	struct lzwcom_ctx *d = NULL;
	struct de_dfilter_ctx *dfctx = NULL;
	dbuf *outf = NULL;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;
	struct de_lzw_params delzwp;
	int errflag = 0;
	i64 pos = 0;
	const char *s;
	u8 *rbuf = NULL;

	d = de_malloc(c, sizeof(struct lzwcom_ctx));
	d->ver = -1;
	d->crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_ARC);

	s = de_get_ext_option(c, "lzwcom:version");
	if(s) {
		d->ver = de_atoi(s);
	}
	if(d->ver>=2) d->ver = 2;
	else if(d->ver!=1) d->ver = -1;

	if(d->ver == -1) {
		lzwcom_detect_version(c, d);
	}
	if(d->ver != -1) {
		de_declare_fmtf(c, "LZWCOM v%d", d->ver);
	}
	else {
		de_declare_fmt(c, "LZWCOM (unknown version)");
	}

	outf = dbuf_create_output_file(c, "unc", NULL, 0);
	dbuf_enable_wbuffer(outf);
	de_dfilter_init_objects(c, NULL, &dcmpro, &dres);
	dcmpro.f = outf;

	de_zeromem(&delzwp, sizeof(struct de_lzw_params));
	delzwp.fmt = DE_LZWFMT_ARC5;
	delzwp.flags |= DE_LZWFLAG_TOLERATETRAILINGJUNK;
	dfctx = de_dfilter_create(c, dfilter_lzw_codec, (void*)&delzwp, &dcmpro, &dres);
	rbuf = de_malloc(c, 1024);

	while(1) {
		i64 block_dlen;
		i64 block_pos = pos;

		if(dres.errcode) break;
		if(dfctx->finished_flag) break;
		if(pos >= c->infile->len) break;
		block_dlen = de_min_int(1024, c->infile->len - pos);

		if(d->ver==2) {
			de_dbg(c, "block at %"I64_FMT", dlen=%"I64_FMT, block_pos, block_dlen);
		}

		dbuf_read(c->infile, rbuf, pos, block_dlen);
		de_dfilter_addbuf(dfctx, rbuf, block_dlen);

		// Oddly, this format includes CRCs of the *compressed* bytes, instead of
		// of the decompressed bytes. So it doesn't detect incorrect decompression.
		if(d->ver==2) {
			de_crcobj_reset(d->crco);
			de_crcobj_addbuf(d->crco, rbuf, block_dlen);
		}

		pos += block_dlen;

		if(d->ver==2) {
			u32 crc_reported, crc_calc;

			if(c->infile->len - pos < 2) break;
			crc_calc = de_crcobj_getval(d->crco);
			crc_reported = (u32)de_getu16le_p(&pos);
			de_dbg_indent(c, 1);
			de_dbg(c, "crc (calculated): 0x%04x", (UI)crc_calc);
			de_dbg(c, "crc (reported): 0x%04x", (UI)crc_reported);
			de_dbg_indent(c,- 1);
			if(!errflag && crc_calc!=crc_reported) {
				de_warn(c, "CRC check failed at %"I64_FMT". This might not be an LZWCOM v2 file.", pos-2);
				errflag = 1;
			}
		}
	}

	de_dfilter_finish(dfctx);
	dbuf_flush(outf);
	if(dres.errcode) {
		de_err(c, "Decompression failed: %s", de_dfilter_get_errmsg(c, &dres));
	}

	de_dfilter_destroy(dfctx);
	dbuf_close(outf);
	if(d) {
		de_crcobj_destroy(d->crco);
		de_free(c, d);
	}
	de_free(c, rbuf);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
movq $0x0, 0x10(%rsp)
movl $0x10, %esi
callq 0xe969b
movq %rax, %r14
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq %rbx, %rdi
movl $0x21, %esi
callq 0xeba56
movq %rax, 0x8(%r14)
leaq 0x917bf(%rip), %rsi     # 0x13208a
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0xa08e3
movq %rax, %rdi
callq 0xea781
movl %eax, (%r14)
movl $0x2, %eax
cmpl $0x1, (%r14)
jg 0xa08f5
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
je 0xa08f8
movl %eax, (%r14)
cmpl $-0x1, (%r14)
jne 0xa0953
movq 0x18(%rbx), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq $0x402, 0x18(%rax)      # imm = 0x402
jl 0xa0950
movq 0x8(%r14), %rdi
callq 0xebdc3
movq 0x8(%r14), %rdi
movq 0x18(%rbx), %rsi
movl $0x400, %ecx            # imm = 0x400
xorl %edx, %edx
callq 0xebf09
movq 0x8(%r14), %rdi
callq 0xebe2a
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0xef1ca
xorl %ecx, %ecx
cmpl %eax, %ebp
sete %cl
incl %ecx
movl %ecx, (%r14)
movl (%r14), %edx
cmpl $-0x1, %edx
je 0xa096e
leaq 0x91737(%rip), %rsi     # 0x132099
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb703
jmp 0xa097d
leaq 0x9172f(%rip), %rsi     # 0x1320a4
movq %rbx, %rdi
callq 0xeb6d6
leaq 0xac4f6(%rip), %rsi     # 0x14ce7a
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xf037a
movq %rax, %r13
movq %rax, %rdi
callq 0xf0e3f
leaq 0xb0(%rsp), %r15
leaq 0x50(%rsp), %r12
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
callq 0xfa6e0
movq %r13, 0x28(%rsp)
movq %r13, (%r15)
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
movl $0x0, 0x10(%rdx)
movl $0x7, (%rdx)
orb $0x4, 0x4(%rdx)
leaq 0x62328(%rip), %rsi     # 0x102d0b
movq %rbx, %rdi
movq %r15, %rcx
movq %r12, %r8
callq 0xfa829
movq %rax, %r13
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r12
movl $0x0, 0xc(%rsp)
cmpl $0x0, 0x50(%rsp)
jne 0xa0b4a
cmpb $0x0, 0x20(%r13)
jne 0xa0b4a
movq 0x10(%rsp), %r15
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rsi
subq %r15, %rsi
jle 0xa0b4a
movl $0x400, %edi            # imm = 0x400
callq 0xea792
movq %rax, %rbp
cmpl $0x2, (%r14)
jne 0xa0a62
movq %rbx, %rdi
leaq 0x91668(%rip), %rsi     # 0x1320bd
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rdx
movq %r12, %rsi
movq %rbp, %rcx
callq 0xeeee8
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xfa87a
cmpl $0x2, (%r14)
jne 0xa0aa2
movq 0x8(%r14), %rdi
callq 0xebdc3
movq 0x8(%r14), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xebe72
addq 0x10(%rsp), %rbp
movq %rbp, 0x10(%rsp)
movb $0x1, %r15b
cmpl $0x2, (%r14)
jne 0xa0b4d
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
subq %rbp, %rax
cmpq $0x2, %rax
setge %r15b
jl 0xa0b4d
movq %r12, 0x20(%rsp)
movq 0x8(%r14), %rdi
callq 0xebe2a
movl %eax, %ebp
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %rsi
callq 0xef1ed
movq %rax, %r12
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %rbx, %rdi
leaq 0x7472d(%rip), %rsi     # 0x115233
movl %ebp, 0x1c(%rsp)
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
leaq 0xa3e2f(%rip), %rsi     # 0x14494c
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $0x0, 0xc(%rsp)
je 0xa0b58
movl $0x1, 0xc(%rsp)
movq 0x20(%rsp), %r12
jmp 0xa0b4d
xorl %r15d, %r15d
testb %r15b, %r15b
jne 0xa0a0c
jmp 0xa0b90
movl $0x0, 0xc(%rsp)
cmpl %r12d, 0x1c(%rsp)
movq 0x20(%rsp), %r12
je 0xa0b4d
movq 0x10(%rsp), %rdx
addq $-0x2, %rdx
movq %rbx, %rdi
leaq 0x91555(%rip), %rsi     # 0x1320d4
xorl %eax, %eax
callq 0xe9b27
movl $0x1, 0xc(%rsp)
jmp 0xa0b4d
movq %r13, %rdi
callq 0xfa8cc
movq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0xf0ea4
cmpl $0x0, 0x50(%rsp)
je 0xa0bcd
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0xfa6a8
leaq 0x735b1(%rip), %rsi     # 0x114171
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9df7
movq %r13, %rdi
callq 0xfa8d8
movq %r15, %rdi
callq 0xf0b13
movq 0x8(%r14), %rdi
callq 0xebe1d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe9752
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq