// File: /workspace/github_c_makefile_star2_O2/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, 0x8(%rsp)
cmpb $0x0, 0x19(%rax)
jne 0xc60ae
movq %rdx, %rbx
cmpl $0x3, 0xc(%rdx)
jne 0xc60ae
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rsp), %rax
movb $0x1, 0x19(%rax)
movq 0x18(%rbx), %rax
pushq $0x3
popq %rcx
cqto
idivq %rcx
movq %rax, %r12
leaq 0x6be84(%rip), %rsi     # 0x131d5f
movl %r12d, %edx
xorl %eax, %eax
callq 0xde896
leaq -0x101(%r12), %rax
movb $0x1, %cl
movl %ecx, 0x4(%rsp)
cmpq $-0xff, %rax
movq %r12, 0x10(%rsp)
jb 0xc5f7c
leaq (%r12,%r12,2), %r13
cmpq 0x18(%rbx), %r13
jg 0xc5f7c
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq %r12, %r13
je 0xc5f4a
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %r12, %rcx
leaq 0x40(%rsp), %r8
callq 0xc7374
movq 0x40(%rsp), %rax
cmpq $0xff, %rax
jg 0xc5f77
cmpq $0x2, %rax
movzbl %bpl, %ebp
pushq $0x1
popq %rax
cmovgel %eax, %ebp
incq %r12
jmp 0xc5f0f
testb %bpl, %bpl
movq 0x10(%rsp), %r12
je 0xc5f7c
leaq 0x6be1d(%rip), %rcx     # 0x131d78
movl $0x0, 0x4(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0xc52e6
jmp 0xc5f7c
movq 0x10(%rsp), %r12
testq %r12, %r12
movl $0x0, %r13d
cmovgq %r12, %r13
leaq (%r12,%r12), %rax
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x28(%rsp)
cmpq %rbp, %r13
je 0xc60ae
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
leaq 0x38(%rsp), %r8
callq 0xc7374
leaq (%r12,%rbp), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x20(%rsp), %r8
callq 0xc7374
movq 0x30(%rsp), %rax
leaq (%rax,%rbp), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x18(%rsp), %r8
callq 0xc7374
movq 0x38(%rsp), %rcx
cmpb $0x0, 0x4(%rsp)
je 0xc6018
movq %rcx, %rax
shrq $0x8, %rax
movq 0x20(%rsp), %r8
movq %r8, %rdx
shrq $0x8, %rdx
movq 0x18(%rsp), %r9
movq %r9, %rsi
shrq $0x8, %rsi
jmp 0xc602b
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r9
movq %rcx, %rax
movq %r8, %rdx
movq %r9, %rsi
shll $0x10, %eax
shll $0x8, %edx
movzwl %dx, %edx
orl %eax, %edx
movzbl %sil, %r12d
orl %edx, %r12d
orl $0xff000000, %r12d      # imm = 0xFF000000
cmpl $0x2, (%r15)
jl 0xc608b
movq %r14, %rbx
movq %r13, %r14
leaq 0x40(%rsp), %r13
movq %r13, %rdi
pushq $0x50
popq %rsi
leaq 0x47646(%rip), %rdx     # 0x10d6a8
xorl %eax, %eax
callq 0xde40c
movq %r15, %rdi
movq %rbp, %rsi
movl %r12d, %edx
movq %r13, %rcx
movq %r14, %r13
movq %rbx, %r14
movq 0x28(%rsp), %rbx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
cmpq $0xff, %rbp
ja 0xc60a1
movq 0x8(%rsp), %rax
movl %r12d, 0xd0(%rax,%rbp,4)
incq %rbp
movq 0x10(%rsp), %r12
jmp 0xc5f99
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq