// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/fli.c
// Function: do_chunk_delta_flc
static void do_chunk_delta_flc(deark *c, lctx *d, struct chunk_info_type *ci)
{
	i64 ypos = 0;
	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 (FLC-style) decompression");
	ictx = ci->ictx;
	ictx->use_count++;

	num_encoded_lines = de_getu16le_p(&pos);

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

		// Read code words until we find one the high 2 bits == 0.
		while(1) {
			if(pos >= ci->pos + ci->len) goto done;

			wcode = (UI)de_getu16le_p(&pos);
			wcode_hi = wcode>>14;

			if(wcode_hi==0) {
				npackets = wcode;
				break;
			}
			else if(wcode_hi==3) {
				ypos += (i64)65536 - (i64)wcode;
			}
			else if(wcode_hi==2) {
				// Set the "last byte of current line".
				// (UNTESTED) This feature is only expected to be used if the
				// screen width is odd, and I haven't found such a file.
				de_bitmap_setpixel_rgb(ictx->img, ictx->w-1, ypos, ictx->pal[wcode & 0x00ff]);
			}
		}

		for(pkidx=0; pkidx<npackets; pkidx++) {
			UI clridx, clridx2;
			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 = 2 * (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
				count = (i64)256 - (i64)code;
				clridx = (UI)de_getbyte_p(&pos);
				clridx2 = (UI)de_getbyte_p(&pos);
				for(k=0; k<count; k++) {
					de_bitmap_setpixel_rgb(ictx->img, xpos, ypos, ictx->pal[clridx]);
					xpos++;
					de_bitmap_setpixel_rgb(ictx->img, xpos, ypos, ictx->pal[clridx2]);
					xpos++;
				}
			}
		}

		ypos++;
	}

done:
	;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq 0x20(%rdx), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x48(%rdx)
je 0x5b4b9
movq %rdx, %r14
movq %rdi, %r12
leaq 0xc6ee7(%rip), %rsi     # 0x12217c
xorl %eax, %eax
callq 0xe9226
movq 0x48(%r14), %r13
incl 0x10(%r13)
movq 0x18(%r12), %rdi
leaq 0x8(%rsp), %rsi
callq 0xef1ed
testq %rax, %rax
jle 0x5b4b9
movq %rax, %rbx
xorl %r15d, %r15d
leaq 0x8(%rsp), %rbp
xorl %eax, %eax
movq %r14, 0x28(%rsp)
movq %r12, 0x30(%rsp)
movq %rbx, 0x20(%rsp)
movq %rax, 0x38(%rsp)
movq 0x18(%r14), %rax
addq 0x10(%r14), %rax
cmpq %rax, 0x8(%rsp)
jge 0x5b33a
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef1ed
cmpl $0x3fff, %eax           # imm = 0x3FFF
jbe 0x5b356
movl %eax, %ecx
shrl $0xe, %ecx
cmpl $0x2, %ecx
je 0x5b31d
cmpl $0x3, %ecx
jne 0x5b2dd
movl %eax, %eax
subq %rax, %r15
addq $0x10000, %r15          # imm = 0x10000
jmp 0x5b2dd
movq (%r13), %rsi
movq 0x30(%r13), %rdi
decq %rsi
movzbl %al, %eax
movl 0x38(%r13,%rax,4), %ecx
movq %r15, %rdx
callq 0xf2afd
jmp 0x5b2dd
xorl %eax, %eax
testb %al, %al
movq 0x38(%rsp), %rax
je 0x5b4b9
incq %rax
cmpq %rbx, %rax
jne 0x5b2d8
jmp 0x5b4b9
movq %r15, 0x10(%rsp)
testl %eax, %eax
je 0x5b48d
xorl %r15d, %r15d
xorl %ebx, %ebx
movq %rax, 0x40(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%r14), %rcx
addq 0x10(%r14), %rcx
movq %rax, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
cmpq %rcx, %rax
setl %al
movl %eax, 0x18(%rsp)
movl %ebx, 0x1c(%rsp)
jge 0x5b469
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef14d
movzbl %al, %eax
addq %rax, %r15
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef14d
testb %al, %al
js 0x5b3fa
je 0x5b469
addb %al, %al
movzbl %al, %ebx
movq 0x10(%rsp), %r14
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef14d
movq 0x30(%r13), %rdi
movzbl %al, %eax
movl 0x38(%r13,%rax,4), %ecx
movq %r15, %rsi
movq %r14, %rdx
callq 0xf2afd
incq %r15
decq %rbx
jne 0x5b3cc
jmp 0x5b469
movzbl %al, %r14d
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef14d
movl %eax, %ebx
movq 0x18(%r12), %rdi
movq %rbp, %rsi
callq 0xef14d
movzbl %bl, %ebx
movzbl %al, %r12d
orq $-0x100, %r14
movq 0x10(%rsp), %rbp
movq 0x30(%r13), %rdi
movl 0x38(%r13,%rbx,4), %ecx
movq %r15, %rsi
movq %rbp, %rdx
callq 0xf2afd
movq 0x30(%r13), %rdi
movl 0x38(%r13,%r12,4), %ecx
leaq 0x1(%r15), %rsi
movq %rbp, %rdx
callq 0xf2afd
addq $0x2, %r15
incq %r14
jne 0x5b42d
movq 0x30(%rsp), %r12
leaq 0x8(%rsp), %rbp
movq 0x48(%rsp), %rax
cmpq %rax, 0x50(%rsp)
movl 0x1c(%rsp), %ebx
jge 0x5b4a1
incl %ebx
movq 0x40(%rsp), %rax
cmpl %eax, %ebx
movq 0x28(%rsp), %r14
jne 0x5b36d
movq 0x10(%rsp), %r15
incq %r15
movb $0x1, %al
movq 0x20(%rsp), %rbx
jmp 0x5b33c
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r15
movq 0x28(%rsp), %r14
movl 0x18(%rsp), %eax
jmp 0x5b33c
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq