// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movq %rdi, %r15
testq %r8, %r8
je 0x1012a8
movl (%r8), %eax
movl %eax, 0xc(%rsp)
jmp 0x1012b0
movl $0x0, 0xc(%rsp)
movl $0xd0, %esi
movq %r15, %rdi
callq 0xe8b2b
movq %rax, %rbx
leaq 0x4a042(%rip), %rax     # 0x14b309
movq %rax, 0x20(%rbx)
movq %r15, (%rbx)
movq 0x18(%rsp), %rcx
movq %rcx, 0x8(%rbx)
movq %r14, 0x10(%rbx)
movq %r12, 0x20(%rsp)
movq %r12, 0x18(%rbx)
leaq 0x38(%rbx), %r13
movb $0x0, 0x58(%rbx)
movq (%rcx), %rax
movq %rax, 0x38(%rbx)
movq 0x8(%rcx), %rax
movq %rax, 0x40(%rbx)
addq 0x10(%rcx), %rax
movq %rax, 0x48(%rbx)
movl $0x7, %esi
movl $0x7, %edx
movq %r15, %rdi
callq 0x104d0e
movq %rax, 0xa8(%rbx)
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
xorl %r14d, %r14d
leaq 0x4a6ad(%rip), %r12     # 0x14b9e0
leaq 0x4a6ad(%rip), %rbp     # 0x14b9e7
movzbl (%r14,%r12), %edx
movzbl (%r14,%rbp), %ecx
movq 0xa8(%rbx), %rax
movq 0x8(%rax), %rsi
movq %r15, %rdi
movl %r14d, %r8d
callq 0x1044c1
incq %r14
cmpq $0x7, %r14
jne 0x10133a
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq (%rbx), %rdi
movl $0x800, %esi            # imm = 0x800
callq 0xfad8f
movq %rax, 0x70(%rbx)
movq %rbx, (%rax)
leaq 0x244(%rip), %rcx       # 0x1015cf
movq %rcx, 0x8(%rax)
xorl %r14d, %r14d
testl %r14d, %r14d
je 0x1013a3
testb $0x2, 0xc(%rsp)
je 0x1013c0
jmp 0x101576
testb $0x1, 0xc(%rsp)
sete %al
movq 0x18(%rsp), %rcx
cmpq $0x0, 0x10(%rcx)
setle %cl
testb %al, %cl
jne 0x101576
movq 0x40(%rbx), %rdx
movq %r15, %rdi
leaq 0x49f42(%rip), %rsi     # 0x14b310
xorl %eax, %eax
callq 0xe889f
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
cmpb $0x0, 0x50(%rbx)
jne 0x101503
cmpl $0x0, 0x30(%rbx)
jne 0x10152b
movq %r13, %rdi
movl $0x1, %esi
callq 0xf1bed
movq %r13, %rdi
testl %eax, %eax
je 0x101439
movl $0x1, %esi
callq 0xf1bed
movq %r13, %rdi
testl %eax, %eax
je 0x101477
movl $0x7, %esi
callq 0xf1bed
movq %rax, %rbp
testl %ebp, %ebp
jne 0x10148c
jmp 0x101559
movl $0x8, %esi
callq 0xf1bed
movq %rax, %r12
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0x101465
movzbl %r12b, %edx
movq 0x10(%rsp), %rdi
leaq 0x4969e(%rip), %rsi     # 0x14aafc
xorl %eax, %eax
callq 0xe8617
movq 0x70(%rbx), %rdi
movzbl %r12b, %esi
callq 0xfae4e
jmp 0x1013ea
movl $0xb, %esi
callq 0xf1bed
movq %rax, %rbp
testl %ebp, %ebp
je 0x101524
movq 0xa8(%rbx), %rsi
movq %rbx, %rdi
callq 0x101e47
cmpl $0x6, %eax
jae 0x1014a9
movl %eax, %r12d
addl $0x2, %r12d
jmp 0x1014cd
movl $0x8, %r12d
movq %r13, %rdi
movl $0x4, %esi
callq 0xf1bed
addl %eax, %r12d
cmpl $0x80000001, %r12d      # imm = 0x80000001
jae 0x101524
cmpl $0xe, %eax
ja 0x1014af
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0x1014ed
movq 0x10(%rsp), %rdi
leaq 0x4a4c3(%rip), %rsi     # 0x14b9a4
movl %ebp, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x70(%rbx), %rdi
movl 0x10(%rdi), %esi
subl %ebp, %esi
movl %r12d, %edx
callq 0xfae7b
jmp 0x1013ea
cmpl $0x0, 0x30(%rbx)
jne 0x10152b
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq 0x10(%rsp), %rdi
leaq 0x4a382(%rip), %rcx     # 0x14b89f
xorl %eax, %eax
callq 0xfa21a
movl $0x1, 0x30(%rbx)
movq %r13, %rdi
callq 0xf1c84
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpl $0x0, 0x30(%rbx)
jne 0x10157c
incl %r14d
movq 0x40(%rbx), %rax
cmpq 0x48(%rbx), %rax
jl 0x101392
jmp 0x101576
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0x10152b
movq 0x10(%rsp), %rdi
leaq 0x1d133(%rip), %rsi     # 0x11e6a0
xorl %eax, %eax
callq 0xe8617
jmp 0x10152b
cmpl $0x0, 0x30(%rbx)
je 0x101598
movq 0x20(%rbx), %rdx
leaq 0x49d73(%rip), %rcx     # 0x14b2fa
movq %r15, %rdi
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0xfa21a
jmp 0x1015b9
movq 0x40(%rbx), %rax
movq 0x8(%rbx), %rcx
subq 0x8(%rcx), %rax
movq 0x18(%rbx), %rcx
xorl %edx, %edx
testq %rax, %rax
cmovgq %rax, %rdx
movq %rdx, 0x8(%rcx)
movb $0x1, 0x4(%rcx)
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xff6fb