// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/palmbitmap.c
// Function: read_BitmapType_colortable
static int read_BitmapType_colortable(deark *c, lctx *d, struct page_ctx *pg,
	i64 pos1, i64 *bytes_consumed)
{
	i64 num_entries_raw;
	i64 num_entries;
	i64 k;
	i64 pos = pos1;
	unsigned int idx;
	char tmps[32];

	de_dbg(c, "color table at %d", (int)pos1);
	de_dbg_indent(c, 1);

	num_entries_raw = dbuf_getu16x(c->infile, pos1, d->is_le);
	num_entries = num_entries_raw;
	// TODO: Documentation says "High bits (numEntries > 256) reserved."
	// What exactly does that mean?
	if(num_entries_raw>256) {
		// Files with "4096" entries have been observed, but they actually have 256
		// entries.
		if(num_entries_raw!=4096) {
			de_warn(c, "This image's color table type might not be supported correctly");
		}
		num_entries = 256;
	}
	if(num_entries==num_entries_raw) {
		de_dbg(c, "number of entries: %d", (int)num_entries);
	}
	else {
		de_dbg(c, "number of entries: 0x%04x (assuming %d)", (unsigned int)num_entries_raw,
			(int)num_entries);
	}

	pos += 2;

	if(num_entries>0) {
		// The only custom palettes I've seen in the wild have either 0 (!) or
		// 256 entries.
		// TODO: It might be better to treat all <=8 bit images as paletted:
		// Start with a default palette, then overlay it with any custom
		// palette entries that exist.
		pg->has_custom_pal = 1;
	}

	*bytes_consumed = 2+4*num_entries;

	for(k=0; k<num_entries && k<256; k++) {
		idx = (unsigned int)de_getbyte(pos);
		de_snprintf(tmps, sizeof(tmps), ",idx=%u", idx);
		// Not entirely sure if we should set entry #k, or entry #idx.
		// idx is documented as "The index of this color in the color table."
		pg->custom_pal[idx] = dbuf_getRGB(c->infile, pos+1, 0);
		de_dbg_pal_entry2(c, k, pg->custom_pal[idx], NULL, tmps, NULL);
		pos += 4;
	}

	de_dbg_indent(c, -1);
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rax
movl %eax, %edx
leaq 0xae070(%rip), %rsi     # 0x1ab7b8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x10(%rbp), %rax
movl (%rax), %edx
callq 0x165820
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x100, -0x30(%rbp)     # imm = 0x100
jle 0xfd7ae
cmpq $0x1000, -0x30(%rbp)    # imm = 0x1000
je 0xfd7a6
movq -0x8(%rbp), %rdi
leaq 0xc34f8(%rip), %rsi     # 0x1c0c97
movb $0x0, %al
callq 0x15bc10
movq $0x100, -0x38(%rbp)     # imm = 0x100
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jne 0xfd7d2
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0xa50d1(%rip), %rsi     # 0x1a289a
movb $0x0, %al
callq 0x15b0d0
jmp 0xfd7f0
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0xc34ed(%rip), %rsi     # 0x1c0cd6
movb $0x0, %al
callq 0x15b0d0
movq -0x48(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
jle 0xfd80e
movq -0x18(%rbp), %rax
movl $0x1, 0x30(%rax)
movq -0x38(%rbp), %rcx
shlq $0x2, %rcx
addq $0x2, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rcx
xorl %eax, %eax
cmpq -0x38(%rbp), %rcx
movb %al, -0x71(%rbp)
jge 0xfd846
cmpq $0x100, -0x40(%rbp)     # imm = 0x100
setl %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0xfd852
jmp 0xfd8e8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x165060
movzbl %al, %eax
movl %eax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
movl -0x4c(%rbp), %ecx
movl $0x20, %esi
leaq 0xc3482(%rip), %rdx     # 0x1c0cfe
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
addq $0x1, %rsi
xorl %edx, %edx
callq 0x165ff0
movl %eax, %edx
movq -0x18(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl %edx, 0x50(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x18(%rbp), %rax
movl -0x4c(%rbp), %ecx
movl 0x50(%rax,%rcx,4), %edx
leaq -0x70(%rbp), %r8
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %rcx
callq 0x15bdb0
movq -0x48(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xfd829
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movl $0x1, %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)