// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/ilbm.c
// Function: decompress_plane_delta_op108
static void decompress_plane_delta_op108(deark *c, lctx *d, struct imgbody_info *ibi,
	struct frame_ctx *frctx, i64 plane_idx, dbuf *inf, i64 codepos1, i64 datapos1,
	i64 endpos)
{
	i64 codepos = codepos1;
	i64 datapos = datapos1;
	const i64 code_size = 2;
	const i64 dataelem_size = 2;
	i64 elems_per_row;
	i64 plane_offset;
	u8 elembuf[2];
	int baddata_flag = 0;

	de_dbg2(c, "delta108 plane %d at (%"I64_FMT",%"I64_FMT")", (int)plane_idx,
		codepos1, datapos1);

	elems_per_row = (ibi->bytes_per_row_per_plane + (dataelem_size-1) ) / dataelem_size;
	if(elems_per_row<1) goto done;
	plane_offset = plane_idx * ibi->bytes_per_row_per_plane;

	while(1) {
		i64 elemnum;
		i64 count_code;
		i64 xpos, ypos;
		i64 dstpos;

		if(codepos+code_size > endpos) goto done;
		elemnum = dbuf_getu16be_p(inf, &codepos);

		if(elemnum == 0xffff) { // Stop.
			goto done;
		}

		ypos = elemnum / elems_per_row;
		xpos = elemnum % elems_per_row;
		dstpos = plane_offset + ypos * ibi->frame_buffer_rowspan + dataelem_size*xpos;

		if(codepos+code_size > endpos) goto done;
		count_code = dbuf_geti16be_p(inf, &codepos);

		if(count_code < 0) { // an uncompressed run
			i64 count = -count_code;
			i64 k;

			frctx->change_flag = 1;

			if(datapos + dataelem_size > endpos) {
				baddata_flag = 1;
				goto done;
			}
			dbuf_read(inf, elembuf, datapos, dataelem_size);
			datapos += dataelem_size;

			for(k=0; k<count; k++) {
				if(ypos >= ibi->height) {
					baddata_flag = 1;
					goto done;
				}

				dbuf_write_at(frctx->frame_buffer, dstpos, elembuf, dataelem_size);
				ypos++;
				dstpos += ibi->frame_buffer_rowspan;
			}
		}
		else { // an RLE run
			i64 count = count_code;
			i64 k;

			if(count > 0) frctx->change_flag = 1;

			for(k=0; k<count; k++) {
				if(ypos >= ibi->height) {
					baddata_flag = 1;
					goto done;
				}
				if(datapos + dataelem_size > endpos) {
					baddata_flag = 1;
					goto done;
				}
				dbuf_read(inf, elembuf, datapos, dataelem_size);
				datapos += dataelem_size;

				dbuf_write_at(frctx->frame_buffer, dstpos, elembuf, dataelem_size);
				ypos++;
				dstpos += ibi->frame_buffer_rowspan;
			}
		}
	}

done:
	if(baddata_flag && !d->errflag) {
		de_err(c, "Delta decompression failed");
		d->errflag = 1;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
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 0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x2, -0x48(%rbp)
movq $0x2, -0x50(%rbp)
movl $0x0, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
leaq 0xfee2c(%rip), %rsi     # 0x1b4bab
movb $0x0, %al
callq 0x15b350
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
addq $0x1, %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, -0x58(%rbp)
cmpq $0x1, -0x58(%rbp)
jge 0xb5dac
jmp 0xb604e
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
imulq 0x40(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
cmpq 0x20(%rbp), %rax
jle 0xb5dd0
jmp 0xb604e
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movq %rax, -0x70(%rbp)
cmpq $0xffff, -0x70(%rbp)    # imm = 0xFFFF
jne 0xb5df0
jmp 0xb604e
movq -0x70(%rbp), %rax
cqto
idivq -0x58(%rbp)
movq %rax, -0x88(%rbp)
movq -0x70(%rbp), %rax
cqto
idivq -0x58(%rbp)
movq %rdx, -0x80(%rbp)
movq -0x60(%rbp), %rax
movq -0x88(%rbp), %rcx
movq -0x18(%rbp), %rdx
imulq 0x48(%rdx), %rcx
addq %rcx, %rax
movq -0x80(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x38(%rbp), %rax
addq $0x2, %rax
cmpq 0x20(%rbp), %rax
jle 0xb5e4a
jmp 0xb604e
movq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1653b0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jge 0xb5f53
xorl %eax, %eax
subq -0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x40(%rbp), %rax
addq $0x2, %rax
cmpq 0x20(%rbp), %rax
jle 0xb5e98
movl $0x1, -0x68(%rbp)
jmp 0xb604e
movq -0x30(%rbp), %rdi
leaq -0x62(%rbp), %rsi
movq -0x40(%rbp), %rdx
movl $0x2, %ecx
callq 0x164d10
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x98(%rbp), %rax
jge 0xb5f4e
movq -0x88(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jl 0xb5ef2
movl $0x1, -0x68(%rbp)
jmp 0xb604e
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x90(%rbp), %rsi
leaq -0x62(%rbp), %rdx
movl $0x2, %ecx
callq 0x1683f0
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xb5ec5
jmp 0xb6049
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, -0xa8(%rbp)
jle 0xb5f73
movq -0x20(%rbp), %rax
movl $0x1, 0xc(%rax)
movq $0x0, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0xa8(%rbp), %rax
jge 0xb6047
movq -0x88(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jl 0xb5faf
movl $0x1, -0x68(%rbp)
jmp 0xb604e
movq -0x40(%rbp), %rax
addq $0x2, %rax
cmpq 0x20(%rbp), %rax
jle 0xb5fc9
movl $0x1, -0x68(%rbp)
jmp 0xb604e
movq -0x30(%rbp), %rdi
leaq -0x62(%rbp), %rsi
movq -0x40(%rbp), %rdx
movl $0x2, %ecx
callq 0x164d10
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x90(%rbp), %rsi
leaq -0x62(%rbp), %rdx
movl $0x2, %ecx
callq 0x1683f0
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
addq -0x90(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
jmp 0xb5f7e
jmp 0xb6049
jmp 0xb5dbd
cmpl $0x0, -0x68(%rbp)
je 0xb607b
movq -0x10(%rbp), %rax
cmpl $0x0, 0x1c(%rax)
jne 0xb607b
movq -0x8(%rbp), %rdi
leaq 0xfea61(%rip), %rsi     # 0x1b4aca
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movl $0x1, 0x1c(%rax)
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)