// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/dms.c
// Function: quicklz77_codec_addbuf
static void quicklz77_codec_addbuf(struct de_dfilter_ctx *dfctx,
	const u8 *buf, i64 buf_len)
{
	struct medium_ctx *mctx = (struct medium_ctx *)dfctx->codec_private;
	struct medium_state_machine *mdst = &mctx->mdst;
	i64 bufpos = 0;
	UI n;
	UI length;
	UI ocode, lcode;

	if(dfctx->finished_flag || mctx->errflag) {
		goto done;
	}

	while(1) {
		if(medium_have_enough_output(mctx)) {
			dfctx->finished_flag = 1;
			goto done;
		}

		// Top off the bitbuf, if possible. 32 is an arbitrary number that's
		// large enough for this format.
		while(bufpos<buf_len && mctx->bbll.nbits_in_bitbuf<32) {
			de_bitbuf_lowlevel_add_byte(&mctx->bbll, buf[bufpos++]);
		}

		if(mdst->state==MEDLZST_WAITING_FOR_OCODE1) goto read_ocode1;

		// Minimum useful number of bits is 9.
		if(mctx->bbll.nbits_in_bitbuf < 9) goto done;

		n = (UI)de_bitbuf_lowlevel_get_bits(&mctx->bbll, 1);
		if(n) { // literal byte
			u8 b;

			b = (u8)de_bitbuf_lowlevel_get_bits(&mctx->bbll, 8);
			de_lz77buffer_add_literal_byte(mctx->ringbuf, (u8)b);
			continue;
		}

	read_ocode1:
		if(mctx->bbll.nbits_in_bitbuf < 10) {
			mdst->state = MEDLZST_WAITING_FOR_OCODE1;
			goto done;
		}

		lcode = (UI)de_bitbuf_lowlevel_get_bits(&mctx->bbll, 2);
		ocode = (UI)de_bitbuf_lowlevel_get_bits(&mctx->bbll, 8);
		length = lcode + 2;
		de_lz77buffer_copy_from_hist(mctx->ringbuf, mctx->ringbuf->curpos - 1 - ocode, length);
		mdst->state = MEDLZST_NEUTRAL;
	}

done:
	if(!dfctx->finished_flag && !mctx->errflag) {
		if(bufpos<buf_len) {
			// It shouldn't be possible to get here. If we do, it means some input
			// bytes will be lost.
			de_dfilter_set_generic_error(dfctx->c, mctx->dres, mctx->modname);
			mctx->errflag = 1;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq 0x28(%rdi), %rbp
cmpb $0x0, 0x20(%rdi)
jne 0x4998c
cmpl $0x0, 0x20(%rbp)
je 0x499c6
xorl %r14d, %r14d
cmpb $0x0, 0x20(%rdi)
jne 0x499b7
cmpq %rbx, %r14
jge 0x499b7
cmpl $0x0, 0x20(%rbp)
jne 0x499b7
movq (%rdi), %rdi
movq 0x8(%rbp), %rdx
movq 0x18(%rbp), %rsi
callq 0xfa815
movl $0x1, 0x20(%rbp)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %r15
movq %rdi, (%rsp)
leaq 0x38(%rbp), %r12
xorl %r14d, %r14d
movq 0x10(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x499ec
movq 0x28(%rbp), %rcx
cmpq 0x10(%rax), %rcx
jge 0x49a9d
cmpq %rbx, %r14
jge 0x49a0f
cmpl $0x1f, 0x3c(%rbp)
ja 0x49a0f
movzbl (%r15,%r14), %esi
incq %r14
movq %r12, %rdi
callq 0xf21e9
cmpq %r14, %rbx
jne 0x499f1
movq %rbx, %r14
cmpl $0x1, 0x48(%rbp)
je 0x49a47
cmpl $0x9, 0x3c(%rbp)
jb 0x49a94
movq %r12, %rdi
movl $0x1, %esi
callq 0xf221e
testl %eax, %eax
je 0x49a47
movq %r12, %rdi
movl $0x8, %esi
callq 0xf221e
movq 0x30(%rbp), %rdi
movzbl %al, %esi
callq 0xfb382
jmp 0x499d4
cmpl $0x9, 0x3c(%rbp)
jbe 0x49a8d
movq %r12, %rdi
movl $0x2, %esi
callq 0xf221e
movq %rax, %r13
movq %r12, %rdi
movl $0x8, %esi
callq 0xf221e
addl $0x2, %r13d
movq 0x30(%rbp), %rdi
notl %eax
addl 0x10(%rdi), %eax
movl %eax, %esi
movl %r13d, %edx
callq 0xfb3af
movl $0x0, 0x48(%rbp)
jmp 0x499d4
movl $0x1, 0x48(%rbp)
movq (%rsp), %rdi
jmp 0x4998f
movq (%rsp), %rdi
movb $0x1, 0x20(%rdi)
jmp 0x4998f