// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x81cab
jmp 0x81f84
movq -0x8(%rbp), %rdi
leaq 0x12c136(%rip), %rsi    # 0x1addec
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x81f82
movl $0x0, -0x44(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x18(%rbp), %rdx
addq 0x18(%rdx), %rcx
cmpq %rcx, %rax
jl 0x81d2e
jmp 0x81f84
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x1652c0
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
shrl $0xe, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jne 0x81d59
movl -0x4c(%rbp), %eax
movl %eax, -0x44(%rbp)
jmp 0x81db4
cmpl $0x3, -0x50(%rbp)
jne 0x81d76
movl -0x4c(%rbp), %eax
movl %eax, %ecx
movl $0x10000, %eax          # imm = 0x10000
subq %rcx, %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
jmp 0x81dad
cmpl $0x2, -0x50(%rbp)
jne 0x81dab
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x40(%rbp), %rax
movq (%rax), %rsi
subq $0x1, %rsi
movq -0x20(%rbp), %rdx
movq -0x40(%rbp), %rax
movl -0x4c(%rbp), %ecx
andl $0xff, %ecx
movl %ecx, %ecx
movl 0x38(%rax,%rcx,4), %ecx
callq 0x16b310
jmp 0x81dad
jmp 0x81daf
jmp 0x81d10
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x81f65
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x18(%rbp), %rdx
addq 0x18(%rdx), %rcx
cmpq %rcx, %rax
jl 0x81de5
jmp 0x81f84
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movb %al, -0x61(%rbp)
movzbl -0x61(%rbp), %eax
cmpl $0x80, %eax
jge 0x81e9b
movzbl -0x61(%rbp), %eax
shlq %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jge 0x81e96
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x5c(%rbp)
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x40(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl 0x38(%rax,%rcx,4), %ecx
callq 0x16b310
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x81e3b
jmp 0x81f55
movzbl -0x61(%rbp), %eax
movl %eax, %ecx
movl $0x100, %eax            # imm = 0x100
subq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x5c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x60(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jge 0x81f53
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x40(%rbp), %rax
movl -0x5c(%rbp), %ecx
movl 0x38(%rax,%rcx,4), %ecx
callq 0x16b310
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x40(%rbp), %rax
movl -0x60(%rbp), %ecx
movl 0x38(%rax,%rcx,4), %ecx
callq 0x16b310
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x81ee3
jmp 0x81f55
jmp 0x81f57
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x81dbb
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x81cf3
jmp 0x81f84
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)