// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/dms.c
// Function: mediumlz77_codec_addbuf
static void mediumlz77_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 offset_rel;
	UI length;

	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;
		if(mdst->state==MEDLZST_WAITING_FOR_OCODE2) goto read_ocode2;

		// Make sure we have enough source data to read the flag bit,
		// plus either the literal byte or the first_code.
		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;
		}

		// TODO: This seems overly complicated. Is there a simpler way to
		// implement this?

		mdst->first_code = (UI)de_bitbuf_lowlevel_get_bits(&mctx->bbll, 8);
		fmtutil_get_lzhuf_d_code_and_len(mdst->first_code, &mdst->d_code1, &mdst->d_len1);

	read_ocode1:
		mdst->ocode1_nbits = mdst->d_len1;
		if(mctx->bbll.nbits_in_bitbuf < mdst->ocode1_nbits) {
			mdst->state = MEDLZST_WAITING_FOR_OCODE1;
			goto done;
		}
		mdst->ocode1 = (UI)de_bitbuf_lowlevel_get_bits(&mctx->bbll, mdst->ocode1_nbits);

		mdst->tmp_code = ((mdst->first_code << mdst->ocode1_nbits) | mdst->ocode1) & 0xff;
		fmtutil_get_lzhuf_d_code_and_len(mdst->tmp_code, &mdst->d_code2, &mdst->d_len2);

	read_ocode2:
		mdst->ocode2_nbits = mdst->d_len2;
		if(mctx->bbll.nbits_in_bitbuf < mdst->ocode2_nbits) {
			mdst->state = MEDLZST_WAITING_FOR_OCODE2;
			goto done;
		}
		mdst->ocode2 = (UI)de_bitbuf_lowlevel_get_bits(&mctx->bbll, mdst->ocode2_nbits);

		offset_rel = (mdst->d_code2 << 8) | (((mdst->tmp_code << mdst->ocode2_nbits) | mdst->ocode2) & 0xff);
		length = mdst->d_code1 + 3;
		de_lz77buffer_copy_from_hist(mctx->ringbuf, mctx->ringbuf->curpos - 1 - offset_rel, 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 0x49af9
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %rbx
movq 0x28(%rdi), %rbp
cmpl $0x0, 0x20(%rbp)
je 0x49b1c
xorl %r14d, %r14d
jmp 0x49c91
movq %rsi, %r15
movq %rdi, (%rsp)
leaq 0x38(%rbp), %r12
leaq 0x64(%rbp), %rax
movq %rax, 0x10(%rsp)
leaq 0x68(%rbp), %rax
movq %rax, 0x8(%rsp)
leaq 0x6c(%rbp), %rax
movq %rax, 0x20(%rsp)
leaq 0x70(%rbp), %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
movq 0x10(%rbp), %rax
cmpb $0x0, 0x8(%rax)
je 0x49b66
movq 0x28(%rbp), %rcx
cmpq 0x10(%rax), %rcx
jge 0x49c7b
cmpq %rbx, %r14
jge 0x49b89
cmpl $0x1f, 0x3c(%rbp)
ja 0x49b89
movzbl (%r15,%r14), %esi
incq %r14
movq %r12, %rdi
callq 0xf1b5e
cmpq %r14, %rbx
jne 0x49b6b
movq %rbx, %r14
movl 0x48(%rbp), %eax
cmpl $0x1, %eax
je 0x49beb
cmpl $0x2, %eax
je 0x49c26
cmpl $0x9, 0x3c(%rbp)
jb 0x49c8d
movq %r12, %rdi
movl $0x1, %esi
callq 0xf1b93
movq %rax, %r13
movq %r12, %rdi
movl $0x8, %esi
callq 0xf1b93
testl %r13d, %r13d
je 0x49bd7
movq 0x30(%rbp), %rdi
movzbl %al, %esi
callq 0xfae4e
jmp 0x49b4e
movl %eax, 0x4c(%rbp)
movl %eax, %edi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x10674c
movl 0x3c(%rbp), %eax
movl 0x68(%rbp), %esi
movl %esi, 0x50(%rbp)
cmpl %esi, %eax
jb 0x49c85
movq %r12, %rdi
callq 0xf1b93
movl %eax, 0x54(%rbp)
movl 0x4c(%rbp), %edx
movb 0x50(%rbp), %cl
shll %cl, %edx
orl %eax, %edx
movzbl %dl, %edi
movl %edi, 0x60(%rbp)
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
callq 0x10674c
movl 0x3c(%rbp), %eax
movl 0x70(%rbp), %esi
movl %esi, 0x58(%rbp)
cmpl %esi, %eax
jb 0x49c74
movq %r12, %rdi
callq 0xf1b93
movl %eax, 0x5c(%rbp)
movl 0x6c(%rbp), %edx
shll $0x8, %edx
movl 0x60(%rbp), %esi
movb 0x58(%rbp), %cl
shll %cl, %esi
orl %eax, %esi
movzbl %sil, %esi
orl %edx, %esi
movl 0x64(%rbp), %edx
addl $0x3, %edx
movq 0x30(%rbp), %rdi
notl %esi
addl 0x10(%rdi), %esi
callq 0xfae7b
movl $0x0, 0x48(%rbp)
jmp 0x49b4e
movl $0x2, %eax
jmp 0x49c8a
movq (%rsp), %rax
movb $0x1, 0x20(%rax)
jmp 0x49cb9
movl $0x1, %eax
movl %eax, 0x48(%rbp)
movq (%rsp), %rdi
cmpb $0x0, 0x20(%rdi)
jne 0x49cb9
cmpq %rbx, %r14
jge 0x49cb9
cmpl $0x0, 0x20(%rbp)
jne 0x49cb9
movq (%rdi), %rdi
movq 0x8(%rbp), %rdx
movq 0x18(%rbp), %rsi
callq 0xfa311
movl $0x1, 0x20(%rbp)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq