// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/fli.c
// Function: do_chunk_colormap
static void do_chunk_colormap(deark *c, lctx *d, struct chunk_info_type *ci)
{
	i64 npackets;
	i64 pknum;
	i64 pos = ci->dpos;
	int bps;
	UI num_entries_total = 0;
	UI next_idx = 0;

	if(!ci->ictx) return;
	if(ci->chunktype==CHUNKTYPE_COLORMAP256)
		bps = 8;
	else
		bps = 6;
	npackets = de_getu16le_p(&pos);

	for(pknum=0; pknum<npackets; pknum++) {
		UI num_entries_to_skip;
		UI num_entries_to_set;
		UI k;

		if(pos >= ci->pos + ci->len)  goto done;
		num_entries_to_skip = (UI)de_getbyte_p(&pos);
		next_idx += num_entries_to_skip;
		num_entries_to_set = (UI)de_getbyte_p(&pos);
		if(num_entries_to_set==0) num_entries_to_set = 256;
		num_entries_total += num_entries_to_set;

		for(k=0; k<num_entries_to_set; k++) {
			u8 samp[3];
			u32 clr;
			UI z;

			for(z=0; z<3; z++) {
				samp[z] = de_getbyte_p(&pos);
				if(bps==6) {
					samp[z] = de_scale_63_to_255(samp[z]);
				}
			}

			clr = DE_MAKE_RGB(samp[0], samp[1], samp[2]);
			if(next_idx < 256) {
				ci->ictx->pal[next_idx] = clr;
				de_dbg_pal_entry(c, (i64)next_idx, clr);
			}
			next_idx++;
		}
	}
done:
	de_dbg(c, "number of entries: %u", num_entries_total);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x48(%rax)
jne 0x8165e
jmp 0x817fb
movq -0x18(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x81670
movl $0x8, -0x34(%rbp)
jmp 0x81677
movl $0x6, -0x34(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x817e4
movq -0x30(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x18(%rbp), %rdx
addq 0x18(%rdx), %rcx
cmpq %rcx, %rax
jl 0x816c0
jmp 0x817e6
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0x81704
movl $0x100, -0x44(%rbp)     # imm = 0x100
movl -0x44(%rbp), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jae 0x817d1
movl $0x0, -0x54(%rbp)
cmpl $0x3, -0x54(%rbp)
jae 0x81770
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165170
movb %al, %cl
movl -0x54(%rbp), %eax
movb %cl, -0x4b(%rbp,%rax)
cmpl $0x6, -0x34(%rbp)
jne 0x81763
movl -0x54(%rbp), %eax
movzbl -0x4b(%rbp,%rax), %edi
callq 0x161710
movb %al, %cl
movl -0x54(%rbp), %eax
movb %cl, -0x4b(%rbp,%rax)
jmp 0x81765
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x81727
movzbl -0x4b(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movzbl -0x4a(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x49(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x50(%rbp)
cmpl $0x100, -0x3c(%rbp)     # imm = 0x100
jae 0x817ba
movl -0x50(%rbp), %edx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x3c(%rbp), %ecx
movl %edx, 0x38(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %eax
movl %eax, %esi
movl -0x50(%rbp), %edx
callq 0x15bf00
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x81714
jmp 0x817d3
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x81694
jmp 0x817e6
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %edx
leaq 0x12c5a4(%rip), %rsi    # 0x1add98
movb $0x0, %al
callq 0x15b0d0
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)