// File: /workspace/github_c_makefile_star2_O3/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 $0x28, %rsp
movq 0x20(%rdx), %rax
movq %rax, (%rsp)
movq %rdx, 0x8(%rsp)
cmpq $0x0, 0x48(%rdx)
je 0x5b00e
movq %rdi, %r14
leaq 0xc74a3(%rip), %rsi     # 0x12235e
xorl %eax, %eax
callq 0xe8617
movq 0x8(%rsp), %rax
movq 0x48(%rax), %r12
incl 0x10(%r12)
movq 0x18(%r14), %rdi
movq %rsp, %r13
movq %r13, %rsi
callq 0xeebeb
movq %rax, %r15
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xeebeb
movq %rax, 0x20(%rsp)
testq %rax, %rax
jle 0x5b00e
movq $0x0, 0x18(%rsp)
movq %rsp, %rbx
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
addq 0x10(%rcx), %rax
cmpq %rax, (%rsp)
jge 0x5b00e
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xeeb4b
testb %al, %al
je 0x5aff3
movzbl %al, %eax
movl %eax, 0x10(%rsp)
xorl %ebp, %ebp
xorl %edx, %edx
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rax
addq 0x10(%rcx), %rax
cmpq %rax, (%rsp)
jge 0x5b00e
movl %edx, 0x14(%rsp)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xeeb4b
movzbl %al, %eax
addq %rax, %rbp
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xeeb4b
movzbl %al, %r13d
testb %al, %al
jns 0x5afde
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xeeb4b
movzbl %al, %ebx
orq $-0x100, %r13
movq 0x30(%r12), %rdi
movl 0x38(%r12,%rbx,4), %ecx
movq %rbp, %rsi
movq %r15, %rdx
callq 0xf24ba
incq %rbp
incq %r13
jne 0x5af95
jmp 0x5afe0
movq 0x18(%r14), %rdi
movq %rsp, %rsi
callq 0xeeb4b
movq 0x30(%r12), %rdi
movzbl %al, %eax
movl 0x38(%r12,%rax,4), %ecx
movq %rbp, %rsi
movq %r15, %rdx
callq 0xf24ba
incq %rbp
decq %r13
jne 0x5afb4
movl 0x14(%rsp), %edx
incl %edx
cmpl 0x10(%rsp), %edx
movq %rsp, %rbx
jne 0x5af3e
incq %r15
movq 0x18(%rsp), %rcx
incq %rcx
movq %rcx, 0x18(%rsp)
cmpq 0x20(%rsp), %rcx
jne 0x5af08
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq