// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdx, %r14
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
testq %r8, %r8
je 0x101a35
movl (%r8), %eax
movl %eax, 0x4(%rsp)
jmp 0x101a3d
movl $0x0, 0x4(%rsp)
movl $0xd0, %esi
movq %rbp, %rdi
callq 0xe969b
movq %rax, %rbx
leaq 0x492be(%rip), %rax     # 0x14ad12
movq %rax, 0x20(%rbx)
movq %rbp, (%rbx)
movq 0x10(%rsp), %rcx
movq %rcx, 0x8(%rbx)
movq %r14, 0x10(%rbx)
movq %r15, 0x18(%rsp)
movq %r15, 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)
leaq 0xa8(%rbx), %r15
movl $0x7, %esi
movl $0x7, %edx
movq %rbp, %rdi
callq 0x105165
movq %rax, 0xa8(%rbx)
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
xorl %r14d, %r14d
leaq 0x49930(%rip), %r12     # 0x14b3f7
leaq 0x49922(%rip), %rax     # 0x14b3f0
movzbl (%r14,%rax), %edx
movzbl (%r14,%r12), %ecx
movq (%r15), %rax
movq 0x8(%rax), %rsi
movq %rbp, %rdi
movl %r14d, %r8d
callq 0x104901
incq %r14
cmpq $0x7, %r14
jne 0x101ac7
movq %r15, 0x8(%rsp)
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq (%rbx), %rdi
movl $0x800, %esi            # imm = 0x800
callq 0xfb2c3
movq %rax, 0x70(%rbx)
movq %rbx, (%rax)
leaq 0x276(%rip), %rcx       # 0x101d96
movq %rcx, 0x8(%rax)
xorl %r14d, %r14d
movq %rbp, 0x20(%rsp)
testl %r14d, %r14d
je 0x101b3d
testb $0x2, 0x4(%rsp)
je 0x101b5a
jmp 0x101d3d
testb $0x1, 0x4(%rsp)
sete %al
movq 0x10(%rsp), %rcx
cmpq $0x0, 0x10(%rcx)
setle %cl
testb %al, %cl
jne 0x101d3d
movq 0x40(%rbx), %rdx
movq %rbp, %rdi
leaq 0x491b1(%rip), %rsi     # 0x14ad19
xorl %eax, %eax
callq 0xe9461
movq %rbp, %rdi
movl $0x1, %esi
callq 0xe9585
movq (%rbx), %r12
movq 0x8(%rsp), %r15
cmpb $0x0, 0x50(%rbx)
jne 0x101b90
cmpl $0x0, 0x30(%rbx)
je 0x101bc4
movl $0x4, %ebp
cmpl $0x0, 0x30(%rbx)
jne 0x101d04
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq %r12, %rdi
leaq 0x496f7(%rip), %rcx     # 0x14b2a8
xorl %eax, %eax
callq 0xfa71e
movl $0x1, 0x30(%rbx)
jmp 0x101d04
movq %r13, %rdi
movl $0x1, %esi
callq 0xf2271
movq %r13, %rdi
testl %eax, %eax
je 0x101c24
movl $0x1, %esi
callq 0xf2271
movq %r13, %rdi
testl %eax, %eax
je 0x101c65
movl $0x7, %esi
callq 0xf2271
movq %rax, %rbp
testl %ebp, %ebp
jne 0x101c76
movq (%rbx), %rax
movl $0x4, %ebp
cmpl $0x4, (%rax)
jl 0x101cf5
xorl %r15d, %r15d
movq %r12, %rdi
leaq 0x1c981(%rip), %rsi     # 0x11e599
xorl %eax, %eax
callq 0xe9226
jmp 0x101cf8
movl $0x8, %esi
callq 0xf2271
movq %rax, %r15
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0x101c4e
movzbl %r15b, %edx
movq %r12, %rdi
leaq 0x48905(%rip), %rsi     # 0x14a54c
xorl %eax, %eax
callq 0xe9226
movq 0x70(%rbx), %rdi
movzbl %r15b, %esi
callq 0xfb382
movq 0x8(%rsp), %r15
jmp 0x101d02
movl $0xb, %esi
callq 0xf2271
movq %rax, %rbp
testl %ebp, %ebp
je 0x101ce9
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1026f6
cmpl $0x6, %eax
jae 0x101c8f
movl %eax, %r15d
addl $0x2, %r15d
jmp 0x101cb3
movl $0x8, %r15d
movq %r13, %rdi
movl $0x4, %esi
callq 0xf2271
addl %eax, %r15d
cmpl $0x80000001, %r15d      # imm = 0x80000001
jae 0x101ce9
cmpl $0xe, %eax
ja 0x101c95
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0x101cd1
movq %r12, %rdi
leaq 0x496e8(%rip), %rsi     # 0x14b3ad
movl %ebp, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x70(%rbx), %rdi
movl 0x10(%rdi), %esi
subl %ebp, %esi
movl %r15d, %edx
callq 0xfb3af
xorl %ebp, %ebp
movb $0x1, %r15b
jmp 0x101cf8
movl $0x1, 0x30(%rbx)
movl $0x4, %ebp
xorl %r15d, %r15d
testb %r15b, %r15b
movq 0x8(%rsp), %r15
je 0x101d04
xorl %ebp, %ebp
testl %ebp, %ebp
je 0x101b84
movq %r13, %rdi
callq 0xf22f1
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpl $0x0, 0x30(%rbx)
jne 0x101d3d
incl %r14d
movq 0x40(%rbx), %rax
cmpq 0x48(%rbx), %rax
jl 0x101b2c
cmpl $0x0, 0x30(%rbx)
je 0x101d5f
movq 0x20(%rbx), %rdx
leaq 0x48fb5(%rip), %rcx     # 0x14ad03
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0xfa71e
jmp 0x101d80
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 0xffe9f