// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/tiff.c
// Function: handler_colormap
static void handler_colormap(deark *c, lctx *d, const struct taginfo *tg, const struct tagnuminfo *tni)
{
	i64 num_entries;
	i64 i;
	u8 is_8bit_pal;
	struct page_ctx *pg = tg->pg;

	if(tg->pg->have_colormap || tg->datatype!=3) return;
	tg->pg->have_colormap = 1;
	num_entries = tg->valcount / 3;
	de_dbg(c, "ColorMap with %d entries", (int)num_entries);

	is_8bit_pal = check_for_8bit_pal(c, d, tg, num_entries);
	if(is_8bit_pal) {
		detiff_warn(c, d, pg, "Likely bad colormap detected. Trying to correct.");
	}

	for(i=0; i<num_entries; i++) {
		i64 r1, g1, b1;
		de_colorsample r2, g2, b2;
		de_color clr;
		char tmps[80];

		read_tag_value_as_int64(c, d, tg, num_entries*0 + i, &r1);
		read_tag_value_as_int64(c, d, tg, num_entries*1 + i, &g1);
		read_tag_value_as_int64(c, d, tg, num_entries*2 + i, &b1);
		if(is_8bit_pal) {
			r2 = (de_colorsample)r1;
			g2 = (de_colorsample)g1;
			b2 = (de_colorsample)b1;
		}
		else {
			r2 = (de_colorsample)(r1>>8);
			g2 = (de_colorsample)(g1>>8);
			b2 = (de_colorsample)(b1>>8);
		}
		clr = DE_MAKE_RGB(r2, g2, b2);
		if(c->debug_level>=2) {
			de_snprintf(tmps, sizeof(tmps), "(%5d,%5d,%5d) "DE_CHAR_RIGHTARROW" ",
				(int)r1, (int)g1, (int)b1);
			de_dbg_pal_entry2(c, i, clr, tmps, NULL, NULL);
		}
		if(i<256) {
			pg->pal[i] = clr;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq (%rdx), %rax
movq %rax, 0x10(%rsp)
cmpb $0x0, 0x19(%rax)
jne 0xcebf2
movq %rdx, %rbx
cmpl $0x3, 0xc(%rdx)
jne 0xcebf2
movq %rsi, %r14
movq %rdi, %r15
movq 0x10(%rsp), %rax
movb $0x1, 0x19(%rax)
movq 0x18(%rbx), %rax
movabsq $0x5555555555555556, %rcx # imm = 0x5555555555555556
movq %rax, 0x8(%rsp)
imulq %rcx
movq %rdx, %r12
movq %rdx, %rbp
shrq $0x3f, %rbp
leaq (%rdx,%rbp), %r13
leaq 0x70210(%rip), %rsi     # 0x13ec8f
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
leaq (%r12,%rbp), %rax
addq $-0x101, %rax           # imm = 0xFEFF
movb $0x1, 0x7(%rsp)
cmpq $-0xff, %rax
jae 0xcec04
movq 0x8(%rsp), %r12
cmpb $0x0, 0x7(%rsp)
jne 0xceac9
leaq 0x701f1(%rip), %rcx     # 0x13eca8
movq %r15, %rdi
movq %r14, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xcdc5c
cmpq $0x3, %r12
jl 0xcebf2
leaq (,%r13,2), %rax
movq %rax, 0x18(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x38(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
leaq 0x30(%rsp), %r8
callq 0xd03d8
movq %r13, %rcx
addq %rbp, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x28(%rsp), %r8
callq 0xd03d8
movq 0x18(%rsp), %rax
leaq (%rax,%rbp), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x20(%rsp), %r8
callq 0xd03d8
movq 0x30(%rsp), %rax
cmpb $0x0, 0x7(%rsp)
je 0xceb56
shrq $0x8, %rax
movq 0x28(%rsp), %rcx
shrq $0x8, %rcx
movq 0x20(%rsp), %rdx
shrq $0x8, %rdx
jmp 0xceb60
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rdx
shll $0x10, %eax
shll $0x8, %ecx
movzwl %cx, %ecx
orl %eax, %ecx
movzbl %dl, %r12d
orl %ecx, %r12d
orl $0xff000000, %r12d      # imm = 0xFF000000
cmpl $0x2, (%r15)
jl 0xcebd0
movl 0x30(%rsp), %ecx
movl 0x28(%rsp), %r8d
movl 0x20(%rsp), %r9d
movl $0x50, %esi
movq %r14, %rbx
movq %r13, %r14
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x4bad1(%rip), %rdx     # 0x11a678
xorl %eax, %eax
callq 0xe8d67
movq %r15, %rdi
movq %rbp, %rsi
movl %r12d, %edx
movq %r13, %rcx
movq %r14, %r13
movq %rbx, %r14
movq 0x38(%rsp), %rbx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
cmpq $0xff, %rbp
ja 0xcebe6
movq 0x10(%rsp), %rax
movl %r12d, 0xd0(%rax,%rbp,4)
incq %rbp
cmpq %rbp, %r13
jne 0xceae7
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %r12
cmpq $0x3, %r12
jl 0xceaa9
leaq (,%r13,2), %rax
addq %r13, %rax
movq %rax, 0x18(%rsp)
cmpq 0x18(%rbx), %rax
jg 0xceaa9
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
leaq 0x40(%rsp), %r8
callq 0xd03d8
movq 0x40(%rsp), %rax
cmpq $0xff, %rax
jg 0xceaa4
cmpq $0x2, %rax
movzbl %r12b, %r12d
movl $0x1, %eax
cmovgel %eax, %r12d
incq %rbp
cmpq %rbp, 0x18(%rsp)
jne 0xcec32
testb %r12b, %r12b
sete 0x7(%rsp)
jmp 0xceaa4