// File: /workspace/github_c_makefile_star2_O2/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:
cmpb $0x0, 0x20(%rdi)
je 0x4763d
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq 0x28(%rdi), %rbx
cmpl $0x0, 0x20(%rbx)
je 0x4765d
xorl %r14d, %r14d
jmp 0x47721
movq %rsi, %r12
movq %rdi, (%rsp)
leaq 0x38(%rbx), %r13
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x477f6
testl %eax, %eax
jne 0x4770c
movl 0x3c(%rbx), %eax
cmpq %r15, %r14
jge 0x4769a
cmpl $0x1f, %eax
ja 0x4769a
movzbl (%r12,%r14), %esi
incq %r14
movq %r13, %rdi
callq 0xe69c4
jmp 0x4767b
cmpl $0x1, 0x48(%rbx)
je 0x476d0
cmpl $0x9, %eax
jb 0x4771d
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0xe69f9
testl %eax, %eax
je 0x476cd
movq %r13, %rdi
pushq $0x8
popq %rsi
callq 0xe69f9
movq 0x30(%rbx), %rdi
movzbl %al, %esi
callq 0xef336
jmp 0x4766b
movl 0x3c(%rbx), %eax
cmpl $0x9, %eax
jbe 0x47716
movq %r13, %rdi
pushq $0x2
popq %rsi
callq 0xe69f9
movq %rax, %rbp
movq %r13, %rdi
pushq $0x8
popq %rsi
callq 0xe69f9
addl $0x2, %ebp
movq 0x30(%rbx), %rdi
notl %eax
addl 0x10(%rdi), %eax
movl %eax, %esi
movl %ebp, %edx
callq 0xef363
andl $0x0, 0x48(%rbx)
jmp 0x4766b
movq (%rsp), %rax
movb $0x1, 0x20(%rax)
jmp 0x47749
movl $0x1, 0x48(%rbx)
movq (%rsp), %rdi
cmpb $0x0, 0x20(%rdi)
jne 0x47749
cmpq %r15, %r14
jge 0x47749
cmpl $0x0, 0x20(%rbx)
jne 0x47749
movq (%rdi), %rdi
movq 0x8(%rbx), %rdx
movq 0x18(%rbx), %rsi
callq 0xee91c
movl $0x1, 0x20(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq