// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/mscompress.c
// Function: do_decompress_MSZIP
static void do_decompress_MSZIP(deark *c, struct de_dfilter_in_params *dcmpri1,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres)
{
	const char *modname = "mszip";
	i64 pos = dcmpri1->pos;
	int saved_indent_level;
	struct de_dfilter_in_params dcmpri2;
	struct de_lz77buffer *ringbuf = NULL;

	de_dbg_indent_save(c, &saved_indent_level);

	// The ring buffer has to persist between blocks. So create our own, and
	// tell the deflate codec to use it.
	ringbuf = de_lz77buffer_create(c, 32768);

	dcmpri2.f = dcmpri1->f;

	while(1) {
		i64 blkpos;
		i64 blklen_raw;
		i64 blk_dlen;
		i64 outlen_before;
		i64 unc_bytes_this_block;
		UI sig;
		struct de_deflate_params inflparams;

		if(pos > dcmpri1->pos + dcmpri1->len -4) {
			goto done;
		}
		blkpos = pos;
		de_dbg(c, "MSZIP block at %"I64_FMT, blkpos);
		de_dbg_indent(c, 1);
		blklen_raw = dbuf_getu16le_p(dcmpri1->f, &pos);
		blk_dlen = blklen_raw - 2;
		sig = (UI)dbuf_getu16be_p(dcmpri1->f, &pos);
		if(sig != 0x434b) { // "CK"
			de_dfilter_set_errorf(c, dres, modname, "Failed to find MSZIP block "
				"at %"I64_FMT, blkpos);
			goto done;
		}
		de_dbg(c, "block dpos: %"I64_FMT", dlen: %d", pos, (int)blk_dlen);
		if(blk_dlen < 0) goto done;
		dcmpri2.pos = pos;
		dcmpri2.len = blk_dlen;
		de_zeromem(&inflparams, sizeof(struct de_deflate_params));
		inflparams.flags = 0;
		inflparams.ringbuf_to_use = ringbuf;
		outlen_before = dcmpro->f->len;

		fmtutil_deflate_codectype1(c, &dcmpri2, dcmpro, dres, (void*)&inflparams);
		dbuf_flush(dcmpro->f);
		if(dres->errcode) goto done;

		pos += blk_dlen;
		unc_bytes_this_block = dcmpro->f->len - outlen_before;
		de_dbg(c, "decompressed to: %"I64_FMT, unc_bytes_this_block);
		if(unc_bytes_this_block < 32768) break; // Presumably we're done.

		de_dbg_indent(c, -1);
	}

done:
	dres->bytes_consumed_valid = 1;
	dres->bytes_consumed = pos - dcmpri1->pos;
	de_lz77buffer_destroy(c, ringbuf);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
leaq 0xda25c(%rip), %rax     # 0x1bcdde
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x8000, %esi           # imm = 0x8000
callq 0x1785d0
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x10(%rbp), %rdx
addq 0x10(%rdx), %rcx
subq $0x4, %rcx
cmpq %rcx, %rax
jle 0xe2be6
jmp 0xe2d78
movq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rdx
leaq 0xda1f6(%rip), %rsi     # 0x1bcdf3
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
subq $0x2, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165210
movl %eax, -0x8c(%rbp)
cmpl $0x434b, -0x8c(%rbp)    # imm = 0x434B
je 0xe2c77
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x68(%rbp), %r8
leaq 0xda179(%rip), %rcx     # 0x1bcde4
movb $0x0, %al
callq 0x177450
jmp 0xe2d78
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq -0x78(%rbp), %rax
movl %eax, %ecx
leaq 0xda17a(%rip), %rsi     # 0x1bce06
movb $0x0, %al
callq 0x15b0d0
cmpq $0x0, -0x78(%rbp)
jge 0xe2c9f
jmp 0xe2d78
movq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0xa0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x19140
movl $0x0, -0xa0(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq -0x58(%rbp), %rsi
leaq -0xa0(%rbp), %r8
callq 0x17f0d0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x167c30
movq -0x20(%rbp), %rax
cmpl $0x0, (%rax)
je 0xe2d19
jmp 0xe2d78
movq -0x78(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq 0x18(%rax), %rax
subq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rdi
movq -0x88(%rbp), %rdx
leaq 0xda0d3(%rip), %rsi     # 0x1bce20
movb $0x0, %al
callq 0x15b0d0
cmpq $0x8000, -0x88(%rbp) # imm = 0x8000
jge 0xe2d63
jmp 0xe2d76
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0xe2bc4
jmp 0xe2d78
movq -0x20(%rbp), %rax
movb $0x1, 0x4(%rax)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
subq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rsi
callq 0x178950
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x15b5a0
addq $0xa0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)