// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq 0x20(%rdx), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x48(%rdx)
je 0x58058
movq %rdx, %r15
movq %rdi, %r14
leaq 0xbd29f(%rip), %rsi     # 0x11519e
xorl %ebx, %ebx
xorl %eax, %eax
callq 0xde896
movq 0x48(%r15), %r13
incl 0x10(%r13)
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rbp
movq %rbp, %rsi
callq 0xe3fc4
movq %rax, %r12
movq 0x18(%r14), %rdi
movq %rbp, %rsi
callq 0xe3fc4
testq %rax, %rax
cmovleq %rbx, %rax
movq %r15, 0x28(%rsp)
movq %rax, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
cmpq %rax, %rbx
leaq 0x8(%rsp), %rbx
je 0x58058
movq 0x18(%r15), %rax
addq 0x10(%r15), %rax
cmpq %rax, 0x8(%rsp)
jge 0x58058
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xe3f2a
movzbl %al, %eax
movl %eax, 0x14(%rsp)
xorl %ecx, %ecx
xorl %ebp, %ebp
cmpl 0x14(%rsp), %ecx
je 0x58043
movq 0x18(%r15), %rax
addq 0x10(%r15), %rax
cmpq %rax, 0x8(%rsp)
jge 0x58058
movq %rcx, 0x30(%rsp)
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xe3f2a
movzbl %al, %eax
addq %rax, %rbp
movq 0x18(%r14), %rdi
movq %rbx, %rsi
callq 0xe3f2a
movq %rbx, %rsi
movzbl %al, %ebx
testb %al, %al
jns 0x58025
movq 0x18(%r14), %rdi
callq 0xe3f2a
movzbl %al, %r15d
orq $-0x100, %rbx
testq %rbx, %rbx
je 0x5802d
movq 0x30(%r13), %rdi
movl 0x38(%r13,%r15,4), %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xe7287
incq %rbp
incq %rbx
jmp 0x57fdc
movq 0x18(%r14), %rdi
leaq 0x8(%rsp), %rsi
callq 0xe3f2a
movq 0x30(%r13), %rdi
movzbl %al, %eax
movl 0x38(%r13,%rax,4), %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xe7287
incq %rbp
subq $0x1, %rbx
jae 0x57ffd
jmp 0x58032
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %rcx
incl %ecx
leaq 0x8(%rsp), %rbx
jmp 0x57f7e
incq %r12
movq 0x20(%rsp), %rbx
incq %rbx
movq 0x18(%rsp), %rax
jmp 0x57f41
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq