// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: fmtutil_lzss1_codectype1
void fmtutil_lzss1_codectype1(deark *c, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
	void *codec_private_params)
{
#define LZSS_BUFSIZE 4096
	struct de_lzss1_params *params = (struct de_lzss1_params*)codec_private_params;
	struct lzss_ctx *sctx = NULL;

	sctx = de_malloc(c, sizeof(struct lzss_ctx));
	sctx->dcmpri = dcmpri;
	sctx->dcmpro = dcmpro;
	sctx->cur_ipos = dcmpri->pos;
	sctx->endpos = dcmpri->pos + dcmpri->len;
	sctx->ringbuf = de_lz77buffer_create(c, LZSS_BUFSIZE);
	sctx->ringbuf->writebyte_cb = lzss_lz77buf_writebytecb;
	sctx->ringbuf->userdata = (void*)sctx;

	if(params->flags & 0x2) {
		lzss_init_window_lz5(sctx->ringbuf);
	}
	else {
		de_lz77buffer_clear(sctx->ringbuf, 0x20);
	}
	sctx->ringbuf->curpos = (params->flags & 0x1) ? (LZSS_BUFSIZE-16) : (LZSS_BUFSIZE-18);

	sctx->bbll.is_lsb = 1;
	de_bitbuf_lowlevel_empty(&sctx->bbll);

	while(1) {
		u8 bit;

		if(sctx->bbll.nbits_in_bitbuf==0) {
			lzss_fill_bitbuf(c, sctx);
			if(sctx->stop_flag) goto unc_done;
		}

		bit = (u8)de_bitbuf_lowlevel_get_bits(&sctx->bbll, 1);
		if(bit) { // literal
			u8 b;

			if(sctx->cur_ipos+1 > sctx->endpos) goto unc_done;
			b = dbuf_getbyte_p(dcmpri->f, &sctx->cur_ipos);
			if(c->debug_level>=4) {
				de_dbg(c, "bpos=%u lit %u", sctx->ringbuf->curpos, (UI)b);
			}
			de_lz77buffer_add_literal_byte(sctx->ringbuf, b);
			if(sctx->stop_flag) goto unc_done;
		}
		else { // match
			UI x0, x1;
			UI matchpos;
			UI matchlen;

			if(sctx->cur_ipos+2 > sctx->endpos) goto unc_done;
			x0 = (UI)dbuf_getbyte_p(dcmpri->f, &sctx->cur_ipos);
			x1 = (UI)dbuf_getbyte_p(dcmpri->f, &sctx->cur_ipos);
			matchpos = ((x1 & 0xf0) << 4) | x0;
			matchlen = (x1 & 0x0f) + 3;
			if(c->debug_level>=4) {
				de_dbg(c, "bpos=%u match mpos=%u(%u) len=%u", sctx->ringbuf->curpos,
					matchpos, (UI)((LZSS_BUFSIZE-1)&(sctx->ringbuf->curpos-matchpos)), matchlen);
			}
			de_lz77buffer_copy_from_hist(sctx->ringbuf, matchpos, matchlen);
			if(sctx->stop_flag) goto unc_done;
		}
	}

unc_done:
	dres->bytes_consumed_valid = 1;
	dres->bytes_consumed = sctx->cur_ipos - dcmpri->pos;
	de_lz77buffer_destroy(c, sctx->ringbuf);
	de_free(c, sctx);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %r14
movq %rcx, (%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
pushq $0x48
popq %rsi
callq 0xdecaf
movq %rax, %r15
movq %r13, 0x10(%rax)
movq %r12, 0x18(%rax)
movq %r13, %r12
movq %rax, %r13
movq 0x8(%r12), %rax
movq %rax, 0x28(%r13)
addq 0x10(%r12), %rax
movq %rax, 0x30(%r13)
movq %rbx, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0xef27c
movq %rax, %rbp
movq %rax, 0x20(%r13)
leaq 0x29b(%rip), %rax       # 0xef2b2
movq %rax, 0x8(%rbp)
movq %r13, (%rbp)
testb $0x2, (%r14)
movq 0x20(%rbp), %rdi
jne 0xef03d
movl 0x18(%rbp), %edx
pushq $0x20
popq %rsi
callq 0x18140
andl $0x0, 0x10(%rbp)
jmp 0xef0ee
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x18140
pushq $0xd
popq %rax
movb $0x1, %cl
movabsq $0x101010101010101, %rdx # imm = 0x101010101010101
cmpl $0xd00, %eax            # imm = 0xD00
je 0xef07e
movq 0x20(%rbp), %rsi
movzbl %cl, %ecx
movq %rcx, %rdi
imulq %rdx, %rdi
movq %rdi, 0x5(%rsi,%rax)
movq %rdi, (%rsi,%rax)
addq $0xd, %rax
incb %cl
jmp 0xef058
xorl %eax, %eax
cmpl $0x100, %eax            # imm = 0x100
je 0xef097
movq 0x20(%rbp), %rcx
movb %al, 0xd00(%rcx,%rax)
incq %rax
jmp 0xef080
movb $-0x1, %al
xorl %ecx, %ecx
movq 0x20(%rbp), %rdx
cmpq $0x100, %rcx            # imm = 0x100
je 0xef0b6
movb %al, 0xe00(%rdx,%rcx)
decb %al
incq %rcx
jmp 0xef09b
movaps 0x4e533(%rip), %xmm0    # 0x13d5f0
movups %xmm0, 0xfde(%rdx)
movups %xmm0, 0xfd0(%rdx)
movups %xmm0, 0xfc0(%rdx)
movups %xmm0, 0xfb0(%rdx)
movups %xmm0, 0xfa0(%rdx)
movups %xmm0, 0xf90(%rdx)
movups %xmm0, 0xf80(%rdx)
leaq 0x28(%r13), %rbp
movl (%r14), %eax
andl $0x1, %eax
leal 0xfee(,%rax,2), %eax
movq 0x20(%r15), %rcx
movl %eax, 0x10(%rcx)
addq $0x38, %r13
movb $0x1, 0x38(%r15)
movq %r13, %rdi
callq 0xe6a40
movq %r12, 0x10(%rsp)
movq %r13, 0x8(%rsp)
cmpl $0x0, 0x3c(%r15)
jne 0xef13b
movq %r15, %rdi
callq 0xef2ff
cmpl $0x0, 0x8(%r15)
jne 0xef242
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0xe69f9
movq (%rbp), %rcx
testb %al, %al
je 0xef1a0
cmpq 0x30(%r15), %rcx
jge 0xef242
movq (%r12), %rdi
movq %rbp, %rsi
callq 0xe3f2a
cmpl $0x4, (%rbx)
movzbl %al, %r14d
jl 0xef188
movq 0x20(%r15), %rax
movl 0x10(%rax), %edx
movq %rbx, %rdi
leaq 0x4e4a6(%rip), %rsi     # 0x13d624
movl %r14d, %ecx
xorl %eax, %eax
callq 0xde896
movq 0x20(%r15), %rdi
movl %r14d, %esi
callq 0xef336
cmpl $0x0, 0x8(%r15)
je 0xef121
jmp 0xef242
addq $0x2, %rcx
cmpq 0x30(%r15), %rcx
jg 0xef242
movq (%r12), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movq %rbx, %r13
movzbl %al, %ebx
movq (%r12), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %r12d
movl %r12d, %r14d
shll $0x4, %r14d
andl $0xf00, %r14d           # imm = 0xF00
orl %ebx, %r14d
movq %r13, %rbx
andl $0xf, %r12d
addl $0x3, %r12d
cmpl $0x4, (%r13)
jl 0xef21e
movq 0x20(%r15), %rax
movl 0x10(%rax), %edx
movl %edx, %r8d
subl %r14d, %r8d
andl $0xfff, %r8d            # imm = 0xFFF
movq %rbx, %rdi
leaq 0x4e422(%rip), %rsi     # 0x13d633
movl %r14d, %ecx
movl %r12d, %r9d
xorl %eax, %eax
callq 0xde896
movq 0x20(%r15), %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0xef363
cmpl $0x0, 0x8(%r15)
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
je 0xef121
movq (%rsp), %rcx
movb $0x1, 0x4(%rcx)
movq 0x28(%r15), %rax
subq 0x8(%r12), %rax
movq %rax, 0x8(%rcx)
movq 0x20(%r15), %rsi
movq %rbx, %rdi
callq 0xef395
movq %rbx, %rdi
movq %r15, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xded32