// File: /workspace/github_c_makefile_star2_O3/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 0xfa78d
movq %rdi, %r14
movq 0x8(%rdi), %rax
cmpl $0x0, (%rax)
jne 0xfa78d
movq %rdx, %rbx
cmpb $0x0, 0x20(%r14)
setne %al
testq %rdx, %rdx
setle %cl
orb %al, %cl
jne 0xfa78d
movq %rsi, %r15
leaq 0x10(%r13), %r12
xorl %ebp, %ebp
movq 0x10(%r14), %rcx
cmpb $0x0, 0x8(%rcx)
je 0xfa67a
movq 0x20(%r13), %rax
cmpq 0x10(%rcx), %rax
jge 0xfa788
movb (%r15,%rbp), %al
incq 0x18(%r13)
movzbl %al, %esi
movl 0x28(%r13), %edx
cmpl $0x2, %edx
je 0xfa6e2
cmpl $0x1, %edx
je 0xfa6b9
testl %edx, %edx
jne 0xfa77a
cmpb $-0x7f, %al
jb 0xfa761
movl $0x101, %eax            # imm = 0x101
subq %rsi, %rax
movq %rax, 0x38(%r13)
movl $0x2, %eax
jmp 0xfa776
movq (%rcx), %rdi
callq 0xf0bd6
incq 0x20(%r13)
movq 0x30(%r13), %rcx
leaq -0x1(%rcx), %rax
movq %rax, 0x30(%r13)
xorl %eax, %eax
cmpq $0x2, %rcx
jl 0xfa776
jmp 0xfa77a
cmpq $0x1, (%r13)
jne 0xfa6fb
movq (%rcx), %rdi
movq 0x38(%r13), %rdx
callq 0xf0eba
movq 0x38(%r13), %rax
jmp 0xfa759
movq 0x8(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%r13)
movb %al, 0x10(%r13,%rcx)
movq (%r13), %rax
cmpq %rax, 0x8(%r13)
jb 0xfa77a
movq 0x38(%r13), %rcx
testq %rcx, %rcx
jle 0xfa74d
movq %r15, (%rsp)
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movq (%rax), %rdi
movq (%r13), %rdx
movq %r12, %rsi
callq 0xef547
incq %r15
movq 0x38(%r13), %rcx
cmpq %rcx, %r15
jl 0xfa726
movq (%r13), %rax
movq (%rsp), %r15
imulq %rcx, %rax
movq $0x0, 0x8(%r13)
addq %rax, 0x20(%r13)
xorl %eax, %eax
jmp 0xfa776
testb %al, %al
js 0xfa77a
incq %rsi
imulq (%r13), %rsi
movq %rsi, 0x30(%r13)
movl $0x1, %eax
movl %eax, 0x28(%r13)
incq %rbp
cmpq %rbx, %rbp
jne 0xfa662
jmp 0xfa78d
movb $0x1, 0x20(%r14)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq