// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil-lzh.c
// Function: fmtutil_lzstac_codectype1
void fmtutil_lzstac_codectype1(deark *c, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
	void *codec_private_params)
{
	struct lzh_ctx *cctx = NULL;
	int block_count = 0;
	UI flags = 0;
	static const u8 lzstac_lencodes[7] = {
		// The first 6 codes are for lengths 2-7, and the last is special.
		0x00,0x01,0x02,0x0c,0x0d,0x0e,0x0f
	};
	static const u8 lzstac_lenlengths[7] = {
		0x02,0x02,0x02,0x04,0x04,0x04,0x04
	};

	if(codec_private_params) {
		struct de_lzstac_params *params;

		params = (struct de_lzstac_params*)codec_private_params;
		flags = params->flags;
	}

	cctx = de_malloc(c, sizeof(struct lzh_ctx));
	cctx->modname = "LZStac";
	cctx->c = c;
	cctx->dcmpri = dcmpri;
	cctx->dcmpro = dcmpro;
	cctx->dres = dres;

	cctx->bitrd.bbll.is_lsb = 0;
	cctx->bitrd.f = dcmpri->f;
	cctx->bitrd.curpos = dcmpri->pos;
	cctx->bitrd.endpos = dcmpri->pos + dcmpri->len;

	lzh_make_tree_from_bytearrays(cctx, &cctx->matchlengths_tree,
		lzstac_lencodes, lzstac_lenlengths, 7, NULL);

	cctx->ringbuf = de_lz77buffer_create(cctx->c, 2048);
	cctx->ringbuf->userdata = (void*)cctx;
	cctx->ringbuf->writebyte_cb = lzh_lz77buf_writebytecb_flagerrors;

	while(1) {
		if(block_count==0 && (dcmpri->len<=0) && !(flags & 0x1)) {
			// No input data (0 blocks), and it is allowed. Stop.
			break;
		}

		if(block_count>0 && (flags & 0x2)) {
			// More than 1 block not allowed, so stop.
			break;
		}

		de_dbg3(c, "LZS block at %"I64_FMT, cctx->bitrd.curpos);
		de_dbg_indent(c, 1);
		lzstac_decompress_block(cctx);
		de_dbg_indent(c, -1);

		block_count++;
		if(cctx->err_flag) break;
		if(cctx->bitrd.curpos >= cctx->bitrd.endpos) break;
	}

	if(cctx->err_flag) {
		de_dfilter_set_errorf(c, dres, cctx->modname, "Decoding error");
		goto done;
	}

	cctx->dres->bytes_consumed = cctx->bitrd.curpos - cctx->dcmpri->pos;
	if(cctx->dres->bytes_consumed<0) {
		cctx->dres->bytes_consumed = 0;
	}
	cctx->dres->bytes_consumed_valid = 1;

done:
	destroy_lzh_ctx(cctx);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1806da
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl $0xd0, %esi
callq 0x15b780
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
leaq 0x5662e(%rip), %rcx     # 0x1d6d25
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x30(%rbp), %rax
movb $0x0, 0x58(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x10(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x30(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0xa8, %rsi
leaq 0x56c91(%rip), %rdx     # 0x1d7410
leaq 0x56c91(%rip), %rcx     # 0x1d7417
movl $0x7, %r8d
xorl %eax, %eax
movl %eax, %r9d
callq 0x180900
movq -0x30(%rbp), %rax
movq (%rax), %rdi
movl $0x800, %esi            # imm = 0x800
callq 0x1785d0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x70(%rax)
movq -0x30(%rbp), %rcx
movq -0x30(%rbp), %rax
movq 0x70(%rax), %rax
movq %rcx, (%rax)
movq -0x30(%rbp), %rax
movq 0x70(%rax), %rax
leaq 0x280(%rip), %rcx       # 0x180a50
movq %rcx, 0x8(%rax)
cmpl $0x0, -0x34(%rbp)
jne 0x1807f5
movq -0x10(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jg 0x1807f5
movl -0x38(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x1807f5
jmp 0x180875
cmpl $0x0, -0x34(%rbp)
jle 0x180808
movl -0x38(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x180808
jmp 0x180875
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rdx
leaq 0x56511(%rip), %rsi     # 0x1d6d2c
movb $0x0, %al
callq 0x15b400
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x30(%rbp), %rdi
callq 0x180ad0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x18085c
jmp 0x180875
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x30(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jl 0x180870
jmp 0x180875
jmp 0x1807d4
movq -0x30(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0x18089f
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rdx
leaq 0x56480(%rip), %rcx     # 0x1d6d16
movb $0x0, %al
callq 0x177450
jmp 0x1808ea
movq -0x30(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
subq 0x8(%rax), %rcx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
cmpq $0x0, 0x8(%rax)
jge 0x1808de
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq $0x0, 0x8(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movb $0x1, 0x4(%rax)
movq -0x30(%rbp), %rdi
callq 0x17f000
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)