// File: /workspace/github_c_makefile_star2_O2/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
pushq %rax
movq %rcx, (%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %rbx
pushq $0x48
popq %rsi
callq 0xdecaf
movq %rax, %r15
movq %r12, 0x10(%rax)
movq %r14, 0x18(%rax)
movq 0x8(%r12), %rax
leaq 0x28(%r15), %r13
movq %rax, 0x28(%r15)
addq 0x10(%r12), %rax
movq %rax, 0x30(%r15)
movq %rbx, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0xef27c
movq %rax, %r14
movq %rax, 0x20(%r15)
leaq -0x17d(%rip), %rax      # 0xef2b2
movq %rax, 0x8(%r14)
movq %r15, (%r14)
movq 0x20(%r14), %rdi
movl 0x18(%r14), %edx
pushq $0x20
popq %rsi
callq 0x18140
andl $0x0, 0x10(%r14)
movb $0x1, 0x38(%r15)
movq %r15, %rbp
addq $0x38, %rbp
movq %rbp, %rdi
callq 0xe6a40
pushq $0x1
popq %r14
cmpl $0x0, 0x3c(%r15)
jne 0xef479
movq %r15, %rdi
callq 0xef2ff
cmpl $0x0, 0x8(%r15)
jne 0xef4e9
movq %rbp, %rdi
movl %r14d, %esi
callq 0xe69f9
movq (%r13), %rcx
testb %al, %al
je 0xef4c0
addq $0x2, %rcx
cmpq 0x30(%r15), %rcx
jg 0xef4e9
movq (%r12), %rdi
movq %r13, %rsi
callq 0xe3fc4
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 0xef363
jmp 0xef4de
cmpq 0x30(%r15), %rcx
jge 0xef4e9
movq (%r12), %rdi
movq %r13, %rsi
callq 0xe3f2a
movq 0x20(%r15), %rdi
movzbl %al, %esi
callq 0xef336
cmpl $0x0, 0x8(%r15)
je 0xef463
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 $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xded32