// File: /workspace/github_c_makefile_star2_O1/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, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movl $0x48, %esi
callq 0xe969b
movq %rax, %r15
movq %r13, 0x10(%rax)
movq %r12, 0x18(%rax)
movq %rax, %r12
movq 0x8(%r13), %rax
movq %rax, 0x28(%r12)
addq 0x10(%r13), %rax
movq %rax, 0x30(%r12)
movl $0x28, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %rbp
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x20(%rbp)
movabsq $0x100000000fff, %rcx   # imm = 0x100000000FFF
movq %rcx, 0x14(%rbp)
movq %rbp, 0x20(%r12)
leaq 0x301(%rip), %rcx       # 0xfb2fb
movq %rcx, 0x8(%rbp)
movq %r12, (%rbp)
testb $0x2, (%r14)
jne 0xfb026
movl $0x1000, %edx           # imm = 0x1000
movq %rax, %rdi
movl $0x20, %esi
callq 0x18140
movl $0x0, 0x10(%rbp)
jmp 0xfb0da
movl $0x1000, %edx           # imm = 0x1000
movq %rax, %rdi
xorl %esi, %esi
callq 0x18140
movl $0xd, %eax
movb $0x1, %cl
movabsq $0x101010101010101, %rdx # imm = 0x101010101010101
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
cmpl $0xd00, %eax            # imm = 0xD00
jne 0xfb046
xorl %eax, %eax
movq 0x20(%rbp), %rcx
movb %al, 0xd00(%rcx,%rax)
incq %rax
cmpl $0x100, %eax            # imm = 0x100
jne 0xfb06c
movb $-0x1, %al
xorl %ecx, %ecx
movq 0x20(%rbp), %rdx
movb %al, 0xe00(%rdx,%rcx)
decb %al
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0xfb085
movq 0x20(%rbp), %rax
movaps 0x4f467(%rip), %xmm0    # 0x14a510
movups %xmm0, 0xfde(%rax)
movups %xmm0, 0xfd0(%rax)
movups %xmm0, 0xfc0(%rax)
movups %xmm0, 0xfb0(%rax)
movups %xmm0, 0xfa0(%rax)
movups %xmm0, 0xf90(%rax)
movups %xmm0, 0xf80(%rax)
leaq 0x28(%r12), %rbp
movl (%r14), %eax
andl $0x1, %eax
leal 0xfee(,%rax,2), %eax
movq 0x20(%r15), %rcx
movl %eax, 0x10(%rcx)
addq $0x38, %r12
movb $0x1, 0x38(%r15)
movq %r12, %rdi
callq 0xf2261
movq %r12, (%rsp)
movq %r13, 0x10(%rsp)
cmpl $0x0, 0x3c(%r15)
je 0xfb1ae
movq %r12, %rdi
movl $0x1, %esi
callq 0xf221e
movq (%rbp), %rcx
testb %al, %al
je 0xfb1cb
cmpq 0x30(%r15), %rcx
jge 0xfb1d5
movq (%r13), %rdi
movq %rbp, %rsi
callq 0xef14d
cmpl $0x4, (%rbx)
movzbl %al, %r14d
jl 0xfb16b
movq 0x20(%r15), %rax
movl 0x10(%rax), %edx
movq %rbx, %rdi
leaq 0x4f3e3(%rip), %rsi     # 0x14a544
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r15), %r12
movq %r12, %rdi
movl %r14d, %esi
callq *0x8(%r12)
movq 0x20(%r12), %rax
movl 0x10(%r12), %ecx
movb %r14b, (%rax,%rcx)
movl 0x10(%r12), %eax
incl %eax
andl 0x14(%r12), %eax
movl %eax, 0x10(%r12)
xorl %eax, %eax
cmpl $0x0, 0x8(%r15)
setne %al
sete %cl
shll $0x2, %eax
jmp 0xfb277
movq %r15, %rdi
callq 0xfb34b
movl $0x4, %eax
cmpl $0x0, 0x8(%r15)
jne 0xfb281
jmp 0xfb118
addq $0x2, %rcx
cmpq 0x30(%r15), %rcx
jle 0xfb1e1
movl $0x4, %eax
xorl %ecx, %ecx
jmp 0xfb27b
movq (%r13), %rdi
movq %rbp, %rsi
callq 0xef14d
movq %r13, %rcx
movq %rbx, %r13
movzbl %al, %ebx
movq (%rcx), %rdi
movq %rbp, %rsi
callq 0xef14d
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 0xfb253
movq 0x20(%r15), %rax
movl 0x10(%rax), %edx
movl %edx, %r8d
subl %r14d, %r8d
andl $0xfff, %r8d            # imm = 0xFFF
movq %rbx, %rdi
leaq 0x4f30d(%rip), %rsi     # 0x14a553
movl %r14d, %ecx
movl %r12d, %r9d
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r15), %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0xfb3af
xorl %eax, %eax
cmpl $0x0, 0x8(%r15)
setne %al
sete %cl
shll $0x2, %eax
movq 0x10(%rsp), %r13
movq (%rsp), %r12
testb %cl, %cl
je 0xfb281
xorl %eax, %eax
testl %eax, %eax
je 0xfb10d
movq 0x8(%rsp), %rcx
movb $0x1, 0x4(%rcx)
movq 0x28(%r15), %rax
subq 0x8(%r13), %rax
movq %rax, 0x8(%rcx)
movq 0x20(%r15), %rsi
movq %rbx, %rdi
callq 0xfb408
movq %rbx, %rdi
movq %r15, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9752