// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: my_packbits_codec_addbuf
static void my_packbits_codec_addbuf(struct de_dfilter_ctx *dfctx,
	const u8 *buf, i64 buf_len)
{
	int i;
	u8 b;
	struct packbitsctx *rctx = (struct packbitsctx*)dfctx->codec_private;

	if(!rctx) return;
	if(dfctx->dres->errcode) return;
	if(dfctx->finished_flag) return;

	for(i=0; i<buf_len; i++) {
		if(dfctx->dcmpro->len_known &&
			(rctx->nbytes_written >= dfctx->dcmpro->expected_len))
		{
			dfctx->finished_flag = 1;
			break;
		}

		b = buf[i];
		rctx->total_nbytes_processed++;

		switch(rctx->state) {
		case PACKBITS_STATE_NEUTRAL: // this is a code byte
			if(b>128) { // A compressed run
				rctx->repeat_count = 257 - (i64)b;
				rctx->state = PACKBITS_STATE_READING_UNIT_TO_REPEAT;
			}
			else if(b<128) { // An uncompressed run
				rctx->nliteral_bytes_remaining = (1 + (i64)b) * (i64)rctx->nbytes_per_unit;
				rctx->state = PACKBITS_STATE_COPYING_LITERAL;
			}
			// Else b==128. No-op.
			// TODO: Some (but not most) ILBM specs say that code 128 is used to
			// mark the end of compressed data, so maybe there should be options to
			// tell us what to do when code 128 is encountered.
			break;
		case PACKBITS_STATE_COPYING_LITERAL: // This byte is uncompressed
			dbuf_writebyte(dfctx->dcmpro->f, b);
			rctx->nbytes_written++;
			rctx->nliteral_bytes_remaining--;
			if(rctx->nliteral_bytes_remaining<=0) {
				rctx->state = PACKBITS_STATE_NEUTRAL;
			}
			break;
		case PACKBITS_STATE_READING_UNIT_TO_REPEAT:
			if(rctx->nbytes_per_unit==1) { // Optimization for standard PackBits
				dbuf_write_run(dfctx->dcmpro->f, b, rctx->repeat_count);
				rctx->nbytes_written += rctx->repeat_count;
				rctx->state = PACKBITS_STATE_NEUTRAL;
			}
			else {
				rctx->unitbuf[rctx->nbytes_in_unitbuf++] = b;
				if(rctx->nbytes_in_unitbuf >= rctx->nbytes_per_unit) {
					i64 k;

					for(k=0; k<rctx->repeat_count; k++) {
						dbuf_write(dfctx->dcmpro->f, rctx->unitbuf, (i64)rctx->nbytes_per_unit);
					}
					rctx->nbytes_in_unitbuf = 0;
					rctx->nbytes_written += rctx->repeat_count * (i64)rctx->nbytes_per_unit;
					rctx->state = PACKBITS_STATE_NEUTRAL;
				}
			}
			break;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq 0x28(%rdi), %r13
testq %r13, %r13
je 0xeebae
movq %rdi, %r14
movq 0x8(%rdi), %rax
cmpl $0x0, (%rax)
jne 0xeebae
cmpb $0x0, 0x20(%r14)
je 0xeebbd
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %rbx
movq %rsi, %r15
leaq 0x10(%r13), %r12
xorl %ebp, %ebp
testq %rdx, %rdx
cmovleq %rbp, %rbx
movq %rsi, (%rsp)
cmpq %rbx, %rbp
je 0xeebae
movq 0x10(%r14), %rcx
cmpb $0x0, 0x8(%rcx)
je 0xeebf1
movq 0x20(%r13), %rax
cmpq 0x10(%rcx), %rax
jge 0xeece9
movb (%r15,%rbp), %al
incq 0x18(%r13)
movzbl %al, %esi
movl 0x28(%r13), %edx
cmpl $0x2, %edx
je 0xeec56
cmpl $0x1, %edx
je 0xeec2d
testl %edx, %edx
jne 0xeece1
cmpb $-0x7f, %al
jb 0xeecb4
movl $0x101, %eax            # imm = 0x101
subq %rsi, %rax
movq %rax, 0x38(%r13)
pushq $0x2
jmp 0xeecc6
movq (%rcx), %rdi
callq 0xe5bc5
incq 0x20(%r13)
movq 0x30(%r13), %rcx
leaq -0x1(%rcx), %rax
movq %rax, 0x30(%r13)
xorl %eax, %eax
cmpq $0x2, %rcx
jl 0xeecdd
jmp 0xeece1
cmpq $0x1, (%r13)
jne 0xeec75
movq (%rcx), %rdi
movq 0x38(%r13), %rdx
callq 0xe5e40
movq 0x38(%r13), %rax
addq %rax, 0x20(%r13)
xorl %eax, %eax
jmp 0xeecdd
movq 0x8(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
movb %al, 0x10(%r13,%rcx)
movq 0x8(%r13), %rax
cmpq (%r13), %rax
jb 0xeece1
xorl %r15d, %r15d
movq 0x38(%r13), %rax
cmpq %rax, %r15
jge 0xeecc9
movq 0x10(%r14), %rax
movq (%rax), %rdi
movq (%r13), %rdx
movq %r12, %rsi
callq 0xe46ba
incq %r15
jmp 0xeec93
testb %al, %al
js 0xeece1
incq %rsi
imulq (%r13), %rsi
movq %rsi, 0x30(%r13)
pushq $0x1
popq %rax
jmp 0xeecdd
andq $0x0, 0x8(%r13)
imulq (%r13), %rax
addq %rax, 0x20(%r13)
xorl %eax, %eax
movq (%rsp), %r15
movl %eax, 0x28(%r13)
incq %rbp
jmp 0xeebd4
movb $0x1, 0x20(%r14)
jmp 0xeebae