// File: /workspace/github_c_makefile_star2_O1/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 0xfac88
movq %rdi, %r14
movq 0x8(%rdi), %rax
cmpl $0x0, (%rax)
jne 0xfac88
movq %rdx, %rbx
cmpb $0x0, 0x20(%r14)
setne %al
testq %rdx, %rdx
setle %cl
orb %al, %cl
jne 0xfac88
movq %rsi, %r15
leaq 0x10(%r13), %r12
xorl %ebp, %ebp
movq %rsi, (%rsp)
movq 0x10(%r14), %rcx
cmpb $0x0, 0x8(%rcx)
je 0xfab76
movq 0x20(%r13), %rax
cmpq 0x10(%rcx), %rax
jge 0xfac83
movb (%r15,%rbp), %al
incq 0x18(%r13)
movzbl %al, %esi
movl 0x28(%r13), %edx
cmpl $0x2, %edx
je 0xfabde
cmpl $0x1, %edx
je 0xfabb5
testl %edx, %edx
jne 0xfac75
cmpb $-0x7f, %al
jb 0xfac5c
movl $0x101, %eax            # imm = 0x101
subq %rsi, %rax
movq %rax, 0x38(%r13)
movl $0x2, %eax
jmp 0xfac71
movq (%rcx), %rdi
callq 0xf11be
incq 0x20(%r13)
movq 0x30(%r13), %rcx
leaq -0x1(%rcx), %rax
movq %rax, 0x30(%r13)
xorl %eax, %eax
cmpq $0x2, %rcx
jl 0xfac71
jmp 0xfac75
cmpq $0x1, (%r13)
jne 0xfabfd
movq (%rcx), %rdi
movq 0x38(%r13), %rdx
callq 0xf14bd
movq 0x38(%r13), %rax
addq %rax, 0x20(%r13)
xorl %eax, %eax
jmp 0xfac71
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 0xfac75
movq 0x38(%r13), %rax
testq %rax, %rax
jle 0xfac43
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movq (%rax), %rdi
movq (%r13), %rdx
movq %r12, %rsi
callq 0xefb82
incq %r15
movq 0x38(%r13), %rax
cmpq %rax, %r15
jl 0xfac24
movq $0x0, 0x8(%r13)
imulq (%r13), %rax
addq %rax, 0x20(%r13)
xorl %eax, %eax
movq (%rsp), %r15
jmp 0xfac71
testb %al, %al
js 0xfac75
incq %rsi
imulq (%r13), %rsi
movq %rsi, 0x30(%r13)
movl $0x1, %eax
movl %eax, 0x28(%r13)
incq %rbp
cmpq %rbx, %rbp
jne 0xfab5e
jmp 0xfac88
movb $0x1, 0x20(%r14)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq