// File: /workspace/github_c_makefile_star2_O3/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, (%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
movl $0x48, %esi
callq 0xe8b2b
movq %rax, %r14
movq %rax, %r15
movq %r13, 0x10(%rax)
movq %r12, 0x18(%rax)
movq 0x8(%r13), %rax
movq %rax, 0x28(%r14)
addq 0x10(%r13), %rax
movq %rax, 0x30(%r14)
movl $0x28, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %rbp
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x20(%rbp)
movabsq $0x100000000fff, %rcx   # imm = 0x100000000FFF
movq %rcx, 0x14(%rbp)
movq %rbp, 0x20(%r14)
leaq 0x2d3(%rip), %rcx       # 0xfadc7
movq %rcx, 0x8(%rbp)
movq %r14, %r12
movq %r14, (%rbp)
movq (%rsp), %r14
testb $0x2, (%r14)
jne 0xfab27
movl $0x1000, %edx           # imm = 0x1000
movq %rax, %rdi
movl $0x20, %esi
callq 0x18140
movl $0x0, 0x10(%rbp)
jmp 0xfabdb
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 0xfab47
xorl %eax, %eax
movq 0x20(%rbp), %rcx
movb %al, 0xd00(%rcx,%rax)
incq %rax
cmpl $0x100, %eax            # imm = 0x100
jne 0xfab6d
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 0xfab86
movq 0x20(%rbp), %rax
movaps 0x4ff16(%rip), %xmm0    # 0x14aac0
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)
movq %r12, %rdi
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, %rdi
movb $0x1, 0x38(%r15)
movq %rdi, (%rsp)
callq 0xf1bdd
movq %r13, 0x10(%rsp)
cmpl $0x0, 0x3c(%r15)
jne 0xfac28
movq %r15, %rdi
callq 0xfae17
cmpl $0x0, 0x8(%r15)
jne 0xfad55
movq (%rsp), %rdi
movl $0x1, %esi
callq 0xf1b93
movq (%rbp), %rcx
testb %al, %al
je 0xfacb6
cmpq 0x30(%r15), %rcx
jge 0xfad55
movq (%r13), %rdi
movq %rbp, %rsi
callq 0xeeb4b
cmpl $0x4, (%rbx)
movzbl %al, %r14d
jl 0xfac78
movq 0x20(%r15), %rax
movl 0x10(%rax), %edx
movq %rbx, %rdi
leaq 0x4fe86(%rip), %rsi     # 0x14aaf4
movl %r14d, %ecx
xorl %eax, %eax
callq 0xe8617
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)
cmpl $0x0, 0x8(%r15)
je 0xfac0e
jmp 0xfad55
addq $0x2, %rcx
cmpq 0x30(%r15), %rcx
jg 0xfad55
movq (%r13), %rdi
movq %rbp, %rsi
callq 0xeeb4b
movq %r13, %rcx
movq %rbx, %r13
movzbl %al, %ebx
movq (%rcx), %rdi
movq %rbp, %rsi
callq 0xeeb4b
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 0xfad36
movq 0x20(%r15), %rax
movl 0x10(%rax), %edx
movl %edx, %r8d
subl %r14d, %r8d
andl $0xfff, %r8d            # imm = 0xFFF
movq %rbx, %rdi
leaq 0x4fdda(%rip), %rsi     # 0x14ab03
movl %r14d, %ecx
movl %r12d, %r9d
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r15), %rdi
movl %r14d, %esi
movl %r12d, %edx
callq 0xfae7b
cmpl $0x0, 0x8(%r15)
movq 0x10(%rsp), %r13
je 0xfac0e
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 0xfaed4
movq %rbx, %rdi
movq %r15, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2