// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movq %rdi, %rbx
movq $0x0, 0x8(%rsp)
movl $0x10, %esi
callq 0xe8b2b
movq %rax, %r14
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq %rbx, %rdi
movl $0x21, %esi
callq 0xeb499
movq %rax, 0x8(%r14)
leaq 0x923f1(%rip), %rsi     # 0x13240e
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0xa0037
movq %rax, %rdi
callq 0xe9c2a
movl %eax, (%r14)
jmp 0xa003a
movl (%r14), %eax
movl $0x2, %edx
cmpl $0x1, %eax
jle 0xa0049
movl %edx, (%r14)
jmp 0xa0054
movl $0x1, %edx
jne 0xa02e1
leaq 0x923c2(%rip), %rsi     # 0x13241d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb152
leaq 0xad42e(%rip), %rsi     # 0x14d49a
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xefda1
movq %rax, %r12
movq %rax, %rdi
callq 0xf0855
leaq 0x90(%rsp), %r15
leaq 0x30(%rsp), %rbp
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %rbp, %rcx
callq 0xfa1dc
movq %r12, 0x10(%rsp)
movq %r12, (%r15)
leaq 0x1c(%rsp), %rdx
movq $0x0, 0x8(%rdx)
movl $0x0, 0x10(%rdx)
movabsq $0x400000007, %rax      # imm = 0x400000007
movq %rax, (%rdx)
leaq 0x6234b(%rip), %rsi     # 0x10241b
movq %rbx, %rdi
movq %r15, %rcx
movq %rbp, %r8
callq 0xfa325
movq %rax, %r13
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r12
cmpl $0x0, (%rbp)
je 0xa0175
movq %r13, %rdi
callq 0xfa3c8
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0xf08ba
cmpl $0x0, 0x30(%rsp)
je 0xa0134
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0xfa1a4
leaq 0x740fa(%rip), %rsi     # 0x114221
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9295
movq %r13, %rdi
callq 0xfa3d4
movq %r15, %rdi
callq 0xf052b
movq 0x8(%r14), %rdi
callq 0xeb858
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe8be2
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x4(%rsp)
cmpb $0x0, 0x20(%r13)
jne 0xa00f7
movq 0x8(%rsp), %r15
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rsi
subq %r15, %rsi
jle 0xa00f7
movl $0x400, %edi            # imm = 0x400
callq 0xe9c3b
movq %rax, %rbp
cmpl $0x2, (%r14)
jne 0xa01c8
movq %rbx, %rdi
leaq 0x92286(%rip), %rsi     # 0x132441
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rdx
movq %r12, %rsi
movq %rbp, %rcx
callq 0xee8d4
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xfa376
cmpl $0x2, (%r14)
jne 0xa02cc
movq 0x8(%r14), %rdi
callq 0xeb7fe
movq 0x8(%r14), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xeb8ad
movl (%r14), %eax
addq 0x8(%rsp), %rbp
movq %rbp, 0x8(%rsp)
cmpl $0x2, %eax
jne 0xa02d1
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
subq %rbp, %rax
cmpq $0x2, %rax
jl 0xa00f7
movq 0x8(%r14), %rdi
callq 0xeb865
movl %eax, %ebp
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xeebeb
movq %rax, %r15
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
leaq 0x75079(%rip), %rsi     # 0x1152e3
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
leaq 0xa4baf(%rip), %rsi     # 0x144e2c
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $0x0, 0x4(%rsp)
jne 0xa02c2
movl $0x0, 0x4(%rsp)
cmpl %r15d, %ebp
je 0xa02d1
movq 0x8(%rsp), %rdx
addq $-0x2, %rdx
movq %rbx, %rdi
leaq 0x9219d(%rip), %rsi     # 0x132458
xorl %eax, %eax
callq 0xe8faa
movl $0x1, 0x4(%rsp)
jmp 0xa02d1
addq %rbp, 0x8(%rsp)
cmpl $0x0, 0x30(%rsp)
je 0xa017d
jmp 0xa00f7
movl $0xffffffff, (%r14)     # imm = 0xFFFFFFFF
movq 0x18(%rbx), %rax
cmpq $0x401, 0x18(%rax)      # imm = 0x401
jg 0xa030a
leaq 0x9212b(%rip), %rsi     # 0x132428
movq %rbx, %rdi
callq 0xeb125
jmp 0xa0065
movq 0x8(%r14), %rdi
callq 0xeb7fe
movq 0x8(%r14), %rdi
movq 0x18(%rbx), %rsi
movl $0x400, %ecx            # imm = 0x400
xorl %edx, %edx
callq 0xeb944
movq 0x8(%r14), %rdi
callq 0xeb865
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0xeebc8
xorl %edx, %edx
cmpl %eax, %ebp
sete %dl
incl %edx
jmp 0xa0044