// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: fmtutil_hlp_lz77_codectype1
void fmtutil_hlp_lz77_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 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, 4096);
	sctx->ringbuf->writebyte_cb = lzss_lz77buf_writebytecb;
	sctx->ringbuf->userdata = (void*)sctx;
	de_lz77buffer_clear(sctx->ringbuf, 0x20);

	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==0) { // literal
			u8 b;

			if(sctx->cur_ipos+1 > sctx->endpos) goto unc_done;
			b = dbuf_getbyte_p(dcmpri->f, &sctx->cur_ipos);
			de_lz77buffer_add_literal_byte(sctx->ringbuf, b);
			if(sctx->stop_flag) goto unc_done;
		}
		else { // match
			UI x;
			UI matchpos;
			UI matchlen;

			if(sctx->cur_ipos+2 > sctx->endpos) goto unc_done;
			x = (UI)dbuf_getu16le_p(dcmpri->f, &sctx->cur_ipos);
			matchlen = (x>>12) + 3;
			matchpos = sctx->ringbuf->curpos - ((x & 0x0fff)+1);
			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 %rcx, 0x8(%rsp)
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %rbx
movl $0x48, %esi
callq 0xe969b
movq %rax, %r15
movq %r12, 0x10(%rax)
movq %r13, 0x18(%rax)
movq 0x8(%r12), %rax
leaq 0x28(%r15), %r13
movq %rax, 0x28(%r15)
addq 0x10(%r12), %rax
movq %rax, 0x30(%r15)
movl $0x28, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %rbp
movl $0x1000, %esi           # imm = 0x1000
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x20(%rbp)
movabsq $0x100000000fff, %rcx   # imm = 0x100000000FFF
movq %rcx, 0x14(%rbp)
movq %rbp, 0x20(%r15)
leaq -0x1d1(%rip), %rcx      # 0xfb2fb
movq %rcx, 0x8(%rbp)
movq %r15, (%rbp)
movl $0x1000, %edx           # imm = 0x1000
movq %rax, %rdi
movl $0x20, %esi
callq 0x18140
movl $0x0, 0x10(%rbp)
movb $0x1, 0x38(%r15)
movq %r15, %rbp
addq $0x38, %rbp
movq %rbp, %rdi
callq 0xf2261
cmpl $0x0, 0x3c(%r15)
je 0xfb555
movq %rbp, %rdi
movl $0x1, %esi
callq 0xf221e
movq (%r13), %rcx
testb %al, %al
je 0xfb56b
addq $0x2, %rcx
cmpq 0x30(%r15), %rcx
jg 0xfb5b8
movq (%r12), %rdi
movq %r13, %rsi
callq 0xef1ed
movl %eax, %esi
notl %esi
shrl $0xc, %eax
leal 0x3(%rax), %edx
movq 0x20(%r15), %rdi
orl $0xfffff000, %esi       # imm = 0xFFFFF000
addl 0x10(%rdi), %esi
callq 0xfb3af
jmp 0xfb5a6
movq %r15, %rdi
callq 0xfb34b
movl $0x4, %eax
cmpl $0x0, 0x8(%r15)
jne 0xfb5c5
jmp 0xfb508
cmpq 0x30(%r15), %rcx
jge 0xfb5b8
movq (%r12), %rdi
movq %r13, %rsi
callq 0xef14d
movq 0x20(%r15), %r14
movzbl %al, %ebx
movq %r14, %rdi
movl %ebx, %esi
callq *0x8(%r14)
movq 0x20(%r14), %rax
movl 0x10(%r14), %ecx
movb %bl, (%rax,%rcx)
movl 0x10(%r14), %eax
incl %eax
andl 0x14(%r14), %eax
movl %eax, 0x10(%r14)
xorl %eax, %eax
cmpl $0x0, 0x8(%r15)
setne %al
sete %cl
shll $0x2, %eax
jmp 0xfb5bf
movl $0x4, %eax
xorl %ecx, %ecx
testb %cl, %cl
je 0xfb5c5
xorl %eax, %eax
testl %eax, %eax
je 0xfb501
movq 0x8(%rsp), %rcx
movb $0x1, 0x4(%rcx)
movq 0x28(%r15), %rax
subq 0x8(%r12), %rax
movq %rax, 0x8(%rcx)
movq 0x20(%r15), %rsi
movq 0x10(%rsp), %rbx
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