// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/macpaint.c
// Function: decompress_packbits_for_macpaint
static void decompress_packbits_for_macpaint(deark *c, lctx *d,
	struct de_dfilter_in_params *dcmpri, struct de_dfilter_out_params *dcmpro,
	struct de_dfilter_results *dres)
{
	i64 pos;
	u8 b, b2;
	i64 count;
	i64 endpos;
	i64 xpos_b = 0;
	i64 nbytes_written = 0;

	pos = dcmpri->pos;
	endpos = dcmpri->pos + dcmpri->len;
	d->row_issue_flag = 0;

	while(1) {
		if(dcmpro->len_known && nbytes_written >= dcmpro->expected_len) {
			goto done; // Decompressed the requested amount of dst data.
		}
		if(pos+2 > endpos) { // Min item size is 2 bytes
			goto done; // Reached the end of source data
		}

		b = dbuf_getbyte_p(dcmpri->f, &pos);

		if(b<=127) { // An uncompressed run
			count = 1 + (i64)b;
			if(pos+count > endpos) {
				pos--;
				goto done;
			}

			xpos_b += count;
			dbuf_copy(dcmpri->f, pos, count, dcmpro->f);
			pos += count;
			nbytes_written += count;
		}
		else if(b>=129) { // A compressed run
			count = 257 - (i64)b;
			xpos_b += count;
			b2 = dbuf_getbyte_p(dcmpri->f, &pos);
			dbuf_write_run(dcmpro->f, b2, count);
			nbytes_written += count;
		}

		if(xpos_b==MACPAINT_WIDTH_BYTES) {
			xpos_b = 0;
		}
		else if(xpos_b > MACPAINT_WIDTH_BYTES) {
			d->row_issue_flag = 1;
			xpos_b = xpos_b % MACPAINT_WIDTH_BYTES;
		}
	}

done:
	dbuf_flush(dcmpro->f);
	dres->bytes_consumed = pos - dcmpri->pos;
	dres->bytes_consumed_valid = 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x18(%rbp), %rcx
addq 0x10(%rcx), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movb $0x0, 0x7(%rax)
movq -0x20(%rbp), %rax
movzbl 0x8(%rax), %eax
cmpl $0x0, %eax
je 0xd9e44
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0xd9e44
jmp 0xd9f78
movq -0x30(%rbp), %rax
addq $0x2, %rax
cmpq -0x48(%rbp), %rax
jle 0xd9e57
jmp 0xd9f78
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x31(%rbp)
movzbl -0x31(%rbp), %eax
cmpl $0x7f, %eax
jg 0xd9edf
movzbl -0x31(%rbp), %eax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
addq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jle 0xd9e9e
movq -0x30(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0xd9f78
movq -0x40(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
callq 0x166070
movq -0x40(%rbp), %rax
addq -0x30(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0xd9f3f
movzbl -0x31(%rbp), %eax
cmpl $0x81, %eax
jl 0xd9f3d
movzbl -0x31(%rbp), %eax
movl %eax, %ecx
movl $0x101, %eax            # imm = 0x101
subq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, -0x32(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movb -0x32(%rbp), %al
movq -0x40(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
movq -0x40(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0xd9f3f
cmpq $0x48, -0x50(%rbp)
jne 0xd9f50
movq $0x0, -0x50(%rbp)
jmp 0xd9f73
cmpq $0x48, -0x50(%rbp)
jle 0xd9f71
movq -0x10(%rbp), %rax
movb $0x1, 0x7(%rax)
movq -0x50(%rbp), %rax
movl $0x48, %ecx
cqto
idivq %rcx
movq %rdx, -0x50(%rbp)
jmp 0xd9f73
jmp 0xd9e24
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x167c30
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
subq 0x8(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movb $0x1, 0x4(%rax)
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)