// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/fli.c
// Function: do_chunk_delta_fli
static void do_chunk_delta_fli(deark *c, lctx *d, struct chunk_info_type *ci)
{
	i64 ypos;
	i64 num_encoded_lines;
	i64 line_idx;
	i64 pos = ci->dpos;
	struct image_ctx_type *ictx;

	if(!ci->ictx) goto done;
	de_dbg(c, "doing delta (FLI-style) decompression");
	ictx = ci->ictx;
	ictx->use_count++;

	ypos = de_getu16le_p(&pos);
	num_encoded_lines = de_getu16le_p(&pos);

	for(line_idx=0; line_idx<num_encoded_lines; line_idx++) {
		UI npackets;
		UI pkidx;
		i64 xpos = 0;

		if(pos >= ci->pos + ci->len) goto done;

		npackets = (UI)de_getbyte_p(&pos);

		for(pkidx=0; pkidx<npackets; pkidx++) {
			UI clridx;
			u8 code;
			i64 count;
			i64 k;
			i64 skip_count;

			if(pos >= ci->pos + ci->len) goto done;
			skip_count = (i64)de_getbyte_p(&pos);
			xpos += skip_count;
			code = de_getbyte_p(&pos);
			if(code<128) { // "positive" = run of uncompressed pixels
				count = (i64)code;
				for(k=0; k<count; k++) {
					clridx = (UI)de_getbyte_p(&pos);
					de_bitmap_setpixel_rgb(ictx->img, xpos, ypos, ictx->pal[clridx]);
					xpos++;
				}
			}
			else { // "negative" = RLE
				clridx = (UI)de_getbyte_p(&pos);
				count = (i64)256 - (i64)code;
				for(k=0; k<count; k++) {
					de_bitmap_setpixel_rgb(ictx->img, xpos, ypos, ictx->pal[clridx]);
					xpos++;
				}
			}
		}

		ypos++;
	}

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq 0x20(%rdx), %rax
movq %rax, 0x8(%rsp)
movq %rdx, 0x18(%rsp)
cmpq $0x0, 0x48(%rdx)
je 0x5b257
movq %rdi, %rbx
leaq 0xc70ad(%rip), %rsi     # 0x122156
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rsp), %rax
movq 0x48(%rax), %r12
incl 0x10(%r12)
movq 0x18(%rbx), %rdi
leaq 0x8(%rsp), %r13
movq %r13, %rsi
callq 0xef1ed
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ed
testq %rax, %rax
jle 0x5b257
xorl %edi, %edi
leaq 0x8(%rsp), %rsi
movq %rbx, 0x10(%rsp)
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %rdx
addq 0x10(%rcx), %rdx
cmpq %rdx, 0x8(%rsp)
jge 0x5b22b
movq %rdi, 0x30(%rsp)
movq 0x18(%rbx), %rdi
callq 0xef14d
testb %al, %al
je 0x5b210
movzbl %al, %eax
movl %eax, 0x20(%rsp)
xorl %ebp, %ebp
xorl %ebx, %ebx
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdx
addq 0x10(%rax), %rdx
movq %rcx, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
cmpq %rdx, %rcx
setl %al
movl %eax, 0x24(%rsp)
jge 0x5b1f8
movq 0x10(%rsp), %r13
movq 0x18(%r13), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0xef14d
movzbl %al, %eax
addq %rax, %rbp
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %r14d
testb %al, %al
jns 0x5b1f6
movq 0x18(%r13), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef14d
movzbl %al, %r13d
orq $-0x100, %r14
movq 0x30(%r12), %rdi
movl 0x38(%r12,%r13,4), %ecx
movq %rbp, %rsi
movq %r15, %rdx
callq 0xf2afd
incq %rbp
incq %r14
jne 0x5b1a6
jmp 0x5b1f8
movq 0x10(%rsp), %rax
movq 0x18(%rax), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef14d
movq 0x30(%r12), %rdi
movzbl %al, %eax
movl 0x38(%r12,%rax,4), %ecx
movq %rbp, %rsi
movq %r15, %rdx
callq 0xf2afd
incq %rbp
decq %r14
jne 0x5b1c5
movq 0x38(%rsp), %rax
cmpq %rax, 0x40(%rsp)
jge 0x5b22f
incl %ebx
cmpl 0x20(%rsp), %ebx
jne 0x5b131
incq %r15
movb $0x1, %cl
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
jmp 0x5b247
xorl %ecx, %ecx
jmp 0x5b247
movq 0x10(%rsp), %rbx
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl 0x24(%rsp), %ecx
testb %cl, %cl
je 0x5b257
incq %rdi
cmpq %rax, %rdi
jne 0x5b0f8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq