// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x48, %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x20(%rax), %eax
cmpl $0x0, %eax
jne 0x669cb
movq -0x20(%rbp), %rax
cmpl $0x0, 0x20(%rax)
je 0x669d0
jmp 0x66b16
jmp 0x669d2
movq -0x20(%rbp), %rdi
callq 0x66cc0
cmpl $0x0, %eax
je 0x669ed
movq -0x8(%rbp), %rax
movb $0x1, 0x20(%rax)
jmp 0x66b16
jmp 0x669ef
movq -0x30(%rbp), %rcx
xorl %eax, %eax
cmpq -0x18(%rbp), %rcx
movb %al, -0x42(%rbp)
jge 0x66a0c
movq -0x20(%rbp), %rax
cmpl $0x20, 0x3c(%rax)
setb %al
movb %al, -0x42(%rbp)
movb -0x42(%rbp), %al
testb $0x1, %al
jne 0x66a15
jmp 0x66a3b
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
movq -0x10(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movzbl (%rax,%rcx), %esi
callq 0x16a230
jmp 0x669ef
movq -0x28(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x66a46
jmp 0x66a9d
movq -0x20(%rbp), %rax
cmpl $0x9, 0x3c(%rax)
jae 0x66a55
jmp 0x66b16
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
movl $0x1, %esi
callq 0x16a2a0
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x66a9b
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
movl $0x8, %esi
callq 0x16a2a0
movb %al, -0x41(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdi
movzbl -0x41(%rbp), %esi
callq 0x178880
jmp 0x669d2
jmp 0x66a9d
movq -0x20(%rbp), %rax
cmpl $0xa, 0x3c(%rax)
jae 0x66ab3
movq -0x28(%rbp), %rax
movl $0x1, (%rax)
jmp 0x66b16
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
movl $0x2, %esi
callq 0x16a2a0
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
addq $0x38, %rdi
movl $0x8, %esi
callq 0x16a2a0
movl %eax, -0x3c(%rbp)
movl -0x40(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x30(%rax), %rax
movl 0x10(%rax), %esi
subl $0x1, %esi
subl -0x3c(%rbp), %esi
movl -0x38(%rbp), %edx
callq 0x1788e0
movq -0x28(%rbp), %rax
movl $0x0, (%rax)
jmp 0x669d2
movq -0x8(%rbp), %rax
cmpb $0x0, 0x20(%rax)
jne 0x66b5d
movq -0x20(%rbp), %rax
cmpl $0x0, 0x20(%rax)
jne 0x66b5d
movq -0x30(%rbp), %rax
cmpq -0x18(%rbp), %rax
jge 0x66b5b
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x177560
movq -0x20(%rbp), %rax
movl $0x1, 0x20(%rax)
jmp 0x66b5d
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)