// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, 0x20(%rsp)
movq %rdi, %rbp
testq %r8, %r8
je 0xf5302
movl (%r8), %eax
movl %eax, 0xc(%rsp)
jmp 0xf530a
movl $0x0, 0xc(%rsp)
movl $0xd0, %esi
movq %rbp, %rdi
callq 0xdecaf
movq %rax, %rbx
leaq 0x48b18(%rip), %rax     # 0x13de39
movq %rax, 0x20(%rbx)
movq %rbp, (%rbx)
movq 0x20(%rsp), %rcx
movq %rcx, 0x8(%rbx)
movq %r14, 0x10(%rbx)
movq %r15, 0x30(%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), %rax
movq %rax, 0x28(%rsp)
pushq $0x7
popq %rdx
movq %rbp, %rdi
movq %rdx, %rsi
callq 0xf8601
movq %rax, 0xa8(%rbx)
pushq $0x1
popq %rsi
movq %rbp, %rdi
callq 0xdeba7
xorl %r14d, %r14d
leaq 0x4917d(%rip), %r15     # 0x13e510
leaq 0x4917d(%rip), %r12     # 0x13e517
cmpq $0x7, %r14
je 0xf53c6
movzbl (%r14,%r15), %edx
movzbl (%r14,%r12), %ecx
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x8(%rax), %rsi
movq %rbp, %rdi
movl %r14d, %r8d
callq 0xf7e56
incq %r14
jmp 0xf539a
pushq $-0x1
popq %rsi
movq %rbp, %rdi
callq 0xdeba7
movq (%rbx), %rdi
movl $0x800, %esi            # imm = 0x800
callq 0xef27c
movq %rax, 0x70(%rbx)
movq %rbx, (%rax)
leaq 0x247(%rip), %rcx       # 0xf5633
movq %rcx, 0x8(%rax)
pushq $0x1
popq %r12
xorl %edx, %edx
testl %edx, %edx
je 0xf5406
testb $0x2, 0xc(%rsp)
je 0xf5423
jmp 0xf55da
testb $0x1, 0xc(%rsp)
sete %al
movq 0x20(%rsp), %rcx
cmpq $0x0, 0x10(%rcx)
setle %cl
testb %al, %cl
jne 0xf55da
movl %edx, 0x1c(%rsp)
movq 0x40(%rbx), %rdx
movq %rbp, %rdi
leaq 0x48a0b(%rip), %rsi     # 0x13de40
xorl %eax, %eax
callq 0xdea83
movq %rbp, %rdi
movl %r12d, %esi
callq 0xdeba7
movq (%rbx), %rax
movq %rax, 0x10(%rsp)
cmpb $0x0, 0x50(%rbx)
movl 0x30(%rbx), %eax
jne 0xf5595
testl %eax, %eax
jne 0xf5566
movq %r13, %rdi
movl %r12d, %esi
callq 0xe6a4a
movq %r13, %rdi
testl %eax, %eax
je 0xf549a
movl %r12d, %esi
callq 0xe6a4a
movq %r13, %rdi
testl %eax, %eax
je 0xf54d6
pushq $0x7
popq %rsi
callq 0xe6a4a
movq %rax, %r14
testl %r14d, %r14d
jne 0xf54ea
jmp 0xf55bd
pushq $0x8
popq %rsi
callq 0xe6a4a
movq %rax, %r14
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0xf54c4
movzbl %r14b, %edx
movq 0x10(%rsp), %rdi
leaq 0x4816f(%rip), %rsi     # 0x13d62c
xorl %eax, %eax
callq 0xde896
movq 0x70(%rbx), %rdi
movzbl %r14b, %esi
callq 0xef336
jmp 0xf544f
pushq $0xb
popq %rsi
callq 0xe6a4a
movq %rax, %r14
testl %r14d, %r14d
je 0xf55b4
movq %rbp, %r15
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
callq 0xf5df8
cmpl $0x6, %eax
jae 0xf5506
movl %eax, %ebp
addl $0x2, %ebp
jmp 0xf5523
pushq $0x8
popq %rbp
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0xe6a4a
addl %eax, %ebp
cmpl $0x80000001, %ebp       # imm = 0x80000001
jae 0xf555c
cmpl $0xe, %eax
ja 0xf5509
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0xf5543
movq 0x10(%rsp), %rdi
leaq 0x48f9d(%rip), %rsi     # 0x13e4d4
movl %r14d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x70(%rbx), %rdi
movl 0x10(%rdi), %esi
subl %r14d, %esi
movl %ebp, %edx
callq 0xef363
movq %r15, %rbp
jmp 0xf544f
movl $0x1, 0x30(%rbx)
movq %r15, %rbp
movq %r13, %rdi
callq 0xe6abe
movq %rbp, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
cmpl $0x0, 0x30(%rbx)
jne 0xf55e0
movl 0x1c(%rsp), %edx
incl %edx
movq 0x40(%rbx), %rax
cmpq 0x48(%rbx), %rax
jl 0xf53f6
jmp 0xf55da
testl %eax, %eax
jne 0xf5566
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq 0x10(%rsp), %rdi
leaq 0x48e22(%rip), %rcx     # 0x13e3cf
xorl %eax, %eax
callq 0xee823
movl $0x1, 0x30(%rbx)
jmp 0xf5566
movq (%rbx), %rax
cmpl $0x4, (%rax)
jl 0xf5566
movq 0x10(%rsp), %rdi
leaq 0x1bfff(%rip), %rsi     # 0x1115d0
xorl %eax, %eax
callq 0xde896
jmp 0xf5566
cmpl $0x0, 0x30(%rbx)
je 0xf55fc
movq 0x20(%rbx), %rdx
leaq 0x4883f(%rip), %rcx     # 0x13de2a
movq %rbp, %rdi
movq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0xee823
jmp 0xf561d
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 $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf3885