// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xd0(%rbp)
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x8(%rbp), %rdi
movl $0x21, %esi
callq 0x15eac0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
leaq 0xd26fa(%rip), %rsi     # 0x1bddc5
callq 0x15cb30
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0xeb6f5
movq -0xc8(%rbp), %rdi
callq 0x15cca0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jl 0xeb70a
movq -0x18(%rbp), %rax
movl $0x2, (%rax)
jmp 0xeb71f
movq -0x18(%rbp), %rax
cmpl $0x1, (%rax)
je 0xeb71d
movq -0x18(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0xeb71f
movq -0x18(%rbp), %rax
cmpl $-0x1, (%rax)
jne 0xeb735
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xed210
movq -0x18(%rbp), %rax
cmpl $-0x1, (%rax)
je 0xeb758
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
leaq 0xd2685(%rip), %rsi     # 0x1bddd4
movb $0x0, %al
callq 0x15e3d0
jmp 0xeb768
movq -0x8(%rbp), %rdi
leaq 0xd267c(%rip), %rsi     # 0x1bdddf
callq 0x15e380
movq -0x8(%rbp), %rdi
leaq 0xed8f7(%rip), %rsi     # 0x1d906a
xorl %eax, %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x166c60
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
callq 0x1773f0
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq -0xb4(%rbp), %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x19140
movl $0x7, -0xb4(%rbp)
movl -0xb0(%rbp), %eax
orl $0x4, %eax
movl %eax, -0xb0(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x98c1e(%rip), %rsi     # 0x184400
leaq -0xb4(%rbp), %rdx
leaq -0x40(%rbp), %rcx
leaq -0xa0(%rbp), %r8
callq 0x1775a0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x15b780
movq %rax, -0xd0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xe0(%rbp)
cmpl $0x0, -0xa0(%rbp)
je 0xeb82e
jmp 0xeba0c
movq -0x20(%rbp), %rax
cmpb $0x0, 0x20(%rax)
je 0xeb83d
jmp 0xeba0c
movq -0xc0(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0xeb857
jmp 0xeba0c
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rsi
subq -0xc0(%rbp), %rsi
movl $0x400, %edi            # imm = 0x400
callq 0x15ccf0
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xeb8a4
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rdx
movq -0xd8(%rbp), %rcx
leaq 0xd255b(%rip), %rsi     # 0x1bddf8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0xd0(%rbp), %rsi
movq -0xc0(%rbp), %rdx
movq -0xd8(%rbp), %rcx
callq 0x164d10
movq -0x20(%rbp), %rdi
movq -0xd0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
callq 0x177620
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xeb90e
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x15f250
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xd0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
callq 0x15f430
movq -0xd8(%rbp), %rax
addq -0xc0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x2, (%rax)
jne 0xeba07
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0xc0(%rbp), %rax
cmpq $0x2, %rax
jge 0xeb94e
jmp 0xeba0c
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x15f340
movl %eax, -0xe8(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1652c0
movl %eax, -0xe4(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl -0xe8(%rbp), %edx
leaq 0xb56ed(%rip), %rsi     # 0x1a1087
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl -0xe4(%rbp), %edx
leaq 0xe4f9a(%rip), %rsi     # 0x1d094c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
cmpl $0x0, -0xb8(%rbp)
jne 0xeba05
movl -0xe8(%rbp), %eax
cmpl -0xe4(%rbp), %eax
je 0xeba05
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rdx
subq $0x2, %rdx
leaq 0xd241b(%rip), %rsi     # 0x1bde0f
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0xb8(%rbp)
jmp 0xeba07
jmp 0xeb812
movq -0x20(%rbp), %rdi
callq 0x1776f0
movq -0x28(%rbp), %rdi
callq 0x167c30
cmpl $0x0, -0xa0(%rbp)
je 0xeba5a
movq -0x8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x177360
movq -0xf0(%rbp), %rdi
movq %rax, %rdx
leaq 0xb45aa(%rip), %rsi     # 0x19fffd
movb $0x0, %al
callq 0x15bfb0
movq -0x20(%rbp), %rdi
callq 0x177720
movq -0x28(%rbp), %rdi
callq 0x1676a0
cmpq $0x0, -0x18(%rbp)
je 0xeba8d
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x15f2e0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x15b820
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)