// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ilbm.c
// Function: decompress_plane_delta_op3
static void decompress_plane_delta_op3(deark *c, lctx *d, struct imgbody_info *ibi,
	struct frame_ctx *frctx, i64 plane, i64 pos1, i64 maxlen)
{
	i64 endpos = pos1+maxlen;
	i64 pos = pos1;
	i64 elemnum = 0;
	i64 elemsize = 2;
	i64 elems_per_row;
	i64 elems_total;
	i64 plane_offset;
	i64 dstpos;
	u8 elembuf[2];

	de_dbg2(c, "delta3 plane at %"I64_FMT", maxlen=%"I64_FMT, pos1, maxlen);

	elems_per_row = (ibi->bytes_per_row_per_plane + (elemsize-1) ) / elemsize;
	if(elems_per_row<1) goto done;
	elems_total = elems_per_row * ibi->height;
	plane_offset = plane * ibi->bytes_per_row_per_plane;

	while(1) {
		i64 code;
		i64 offset;

		if(elemnum >= elems_total) break;
		if(pos+2 >= endpos) goto done;
		code = de_geti16be_p(&pos);

		if(code == -1) { // Stop.
			break;
		}
		else if(code >= 0) { // Skip some number of elements, then write one element.
			frctx->change_flag = 1;
			offset = code;
			elemnum += offset;
			de_read(elembuf, pos, elemsize);
			pos += elemsize;
			dstpos = delta3_calc_elem_pos(elemnum, elemsize, elems_per_row, plane_offset,
				ibi->frame_buffer_rowspan);
			dbuf_write_at(frctx->frame_buffer, dstpos, elembuf, elemsize);
		}
		else { // Skip some number of elements, then write multiple elements.
			i64 count;
			i64 k;

			offset = -(code+2);
			elemnum += offset;
			count = de_getu16be_p(&pos);
			if(count>0) {
				frctx->change_flag = 1;
			}
			for(k=0; k<count; k++) {
				de_read(elembuf, pos, elemsize);
				pos += elemsize;
				elemnum++;
				dstpos = delta3_calc_elem_pos(elemnum, elemsize, elems_per_row, plane_offset,
					ibi->frame_buffer_rowspan);
				dbuf_write_at(frctx->frame_buffer, dstpos, elembuf, elemsize);
			}
		}
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq 0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x2, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
movq 0x10(%rbp), %rcx
leaq 0xff954(%rip), %rsi     # 0x1b4a22
movb $0x0, %al
callq 0x15b350
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x50(%rbp), %rcx
subq $0x1, %rcx
addq %rcx, %rax
cqto
idivq -0x50(%rbp)
movq %rax, -0x58(%rbp)
cmpq $0x1, -0x58(%rbp)
jge 0xb50fe
jmp 0xb52f3
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
imulq 0x8(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
imulq 0x40(%rcx), %rax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x60(%rbp), %rax
jl 0xb512f
jmp 0xb52f1
movq -0x40(%rbp), %rax
addq $0x2, %rax
cmpq -0x38(%rbp), %rax
jl 0xb5142
jmp 0xb52f3
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1653b0
movq %rax, -0x80(%rbp)
cmpq $-0x1, -0x80(%rbp)
jne 0xb5163
jmp 0xb52f1
cmpq $0x0, -0x80(%rbp)
jl 0xb51f7
movq -0x20(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x72(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x164d10
movq -0x50(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %r8
callq 0xb5300
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x70(%rbp), %rsi
leaq -0x72(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x1683f0
jmp 0xb52ea
movq -0x80(%rbp), %rcx
addq $0x2, %rcx
xorl %eax, %eax
subq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165210
movq %rax, -0x90(%rbp)
cmpq $0x0, -0x90(%rbp)
jle 0xb5247
movq -0x20(%rbp), %rax
movl $0x1, 0xc(%rax)
movq $0x0, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jge 0xb52e8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x72(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x164d10
movq -0x50(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x68(%rbp), %rcx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %r8
callq 0xb5300
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x70(%rbp), %rsi
leaq -0x72(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x1683f0
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0xb5252
jmp 0xb52ea
jmp 0xb52ec
jmp 0xb5120
jmp 0xb52f3
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)