// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x8(%rsp)
pushq $0x10
popq %rsi
callq 0xdecaf
movq %rax, %r14
orl $-0x1, (%rax)
pushq $0x21
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, 0x8(%r14)
leaq 0x8ae43(%rip), %rsi     # 0x125122
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x9a2f9
movq %rax, %rdi
callq 0xdfb36
movl %eax, (%r14)
jmp 0x9a2fc
movl (%r14), %eax
cmpl $0x1, %eax
jle 0x9a309
pushq $0x2
popq %rdx
movl %edx, (%r14)
jmp 0x9a312
jne 0x9a596
pushq $0x1
popq %rdx
leaq 0x8ae18(%rip), %rsi     # 0x125131
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe0824
leaq 0xa5c60(%rip), %rsi     # 0x13ff8a
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xe4e62
movq %rax, %r13
movq %rax, %rdi
callq 0xe589c
leaq 0x90(%rsp), %r15
leaq 0x30(%rsp), %r12
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
callq 0xee7ee
leaq 0x1c(%rsp), %rdx
andq $0x0, 0x8(%rdx)
andl $0x0, 0x10(%rdx)
movq %r13, 0x10(%rsp)
movq %r13, (%r15)
movabsq $0x400000007, %rax      # imm = 0x400000007
movq %rax, (%rdx)
leaq 0x5c0df(%rip), %rsi     # 0xf6467
movq %rbx, %rdi
movq %r15, %rcx
movq %r12, %r8
callq 0xee930
movq %rax, %r13
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r12
xorl %r15d, %r15d
movl $0x0, 0x4(%rsp)
cmpl $0x0, 0x30(%rsp)
jne 0x9a518
cmpb $0x0, 0x20(%r13)
jne 0x9a518
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rsi
subq %r15, %rsi
jle 0x9a518
movl $0x400, %edi            # imm = 0x400
callq 0xdfb45
movq %rax, %rbp
cmpl $0x2, (%r14)
jne 0x9a405
movq %rbx, %rdi
leaq 0x8ad5d(%rip), %rsi     # 0x125155
movq %r15, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq 0x8(%rsp), %rdx
movq %r12, %rsi
movq %rbp, %rcx
callq 0xe3cb7
movq %r13, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xee97f
cmpl $0x2, (%r14)
jne 0x9a509
movq 0x8(%r14), %rdi
callq 0xe0e44
movq 0x8(%r14), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0xe0ecf
addq 0x8(%rsp), %rbp
cmpl $0x2, (%r14)
movq %rbp, 0x8(%rsp)
jne 0x9a50e
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rax
subq %rbp, %rax
cmpq $0x2, %rax
jl 0x9a518
movq 0x8(%r14), %rdi
callq 0xe0e95
movl %eax, %ebp
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe3fc4
movq %rax, %r15
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq %rbx, %rdi
leaq 0x6ddb4(%rip), %rsi     # 0x108257
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
leaq 0x9d526(%rip), %rsi     # 0x1379dc
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
cmpl $0x0, 0x4(%rsp)
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jne 0x9a50e
movl $0x0, 0x4(%rsp)
cmpl %r15d, %ebp
je 0x9a50e
movq 0x8(%rsp), %rdx
addq $-0x2, %rdx
movq %rbx, %rdi
leaq 0x8ac73(%rip), %rsi     # 0x12516c
xorl %eax, %eax
callq 0xdf05a
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
jmp 0x9a50e
addq %rbp, 0x8(%rsp)
movq 0x8(%rsp), %r15
jmp 0x9a3b4
movq %r13, %rdi
callq 0xee9d1
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0xe58e8
cmpl $0x0, 0x30(%rsp)
je 0x9a555
leaq 0x30(%rsp), %rsi
movq %rbx, %rdi
callq 0xee7bc
leaq 0x6cc79(%rip), %rsi     # 0x1071c1
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xdf2a5
movq %r13, %rdi
callq 0xee9dd
movq %r15, %rdi
callq 0xe5591
movq 0x8(%r14), %rdi
callq 0xe0e8d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movq %rbx, %rdi
movq %r12, %rsi
callq 0xded32
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orl $-0x1, (%r14)
movq 0x18(%rbx), %rax
cmpq $0x401, 0x18(%rax)      # imm = 0x401
jg 0x9a5bc
leaq 0x8ab8d(%rip), %rsi     # 0x12513c
movq %rbx, %rdi
callq 0xe07f7
jmp 0x9a323
movq 0x8(%r14), %rdi
callq 0xe0e44
movq 0x8(%r14), %rdi
movq 0x18(%rbx), %rsi
movl $0x400, %ecx            # imm = 0x400
xorl %edx, %edx
callq 0xe0f33
movq 0x8(%r14), %rdi
callq 0xe0e95
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movl $0x400, %esi            # imm = 0x400
callq 0xe3fa3
xorl %edx, %edx
cmpl %eax, %ebp
sete %dl
incl %edx
jmp 0x9a304