// File: /workspace/github_c_makefile_star2_O3/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 0xce050
movq %rdx, %rbx
cmpl $0x3, 0xc(%rdx)
jne 0xce050
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rsp), %rax
movb $0x1, 0x19(%rax)
movq 0x18(%rbx), %rax
movabsq $0x5555555555555556, %rcx # imm = 0x5555555555555556
movq %rax, 0x28(%rsp)
imulq %rcx
movq %rdx, %r12
movq %rdx, %rbp
shrq $0x3f, %rbp
leaq (%rdx,%rbp), %r13
leaq 0x7120f(%rip), %rsi     # 0x13f08f
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
leaq (%r12,%rbp), %rax
addq $-0x101, %rax           # imm = 0xFEFF
movb $0x1, %cl
movl %ecx, 0x4(%rsp)
cmpq $-0xff, %rax
jb 0xcdf21
leaq (,%r13,2), %rax
addq %r13, %rax
movq %rax, 0x10(%rsp)
cmpq 0x18(%rbx), %rax
jg 0xcdf21
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
leaq 0x40(%rsp), %r8
callq 0xcf7bf
movq 0x40(%rsp), %rax
cmpq $0xff, %rax
jg 0xcdf21
cmpq $0x2, %rax
movzbl %r12b, %r12d
movl $0x1, %eax
cmovgel %eax, %r12d
incq %rbp
cmpq %rbp, 0x10(%rsp)
jne 0xcdebd
testb %r12b, %r12b
je 0xcdf21
leaq 0x711a1(%rip), %rcx     # 0x13f0a8
movl $0x0, 0x4(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0xcd067
cmpq $0x3, 0x28(%rsp)
jl 0xce050
leaq (,%r13,2), %rax
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x30(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq %rbp, %rcx
leaq 0x38(%rsp), %r8
callq 0xcf7bf
movq %r13, %rcx
addq %rbp, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x20(%rsp), %r8
callq 0xcf7bf
movq 0x10(%rsp), %rax
leaq (%rax,%rbp), %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
leaq 0x18(%rsp), %r8
callq 0xcf7bf
movq 0x38(%rsp), %rcx
cmpb $0x0, 0x4(%rsp)
je 0xcdfb9
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 0xcdfcc
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 0xce02e
movl $0x50, %esi
movq %r14, %rbx
movq %r13, %r14
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x4c793(%rip), %rdx     # 0x11a798
xorl %eax, %eax
callq 0xe8193
movq %r15, %rdi
movq %rbp, %rsi
movl %r12d, %edx
movq %r13, %rcx
movq %r14, %r13
movq %rbx, %r14
movq 0x30(%rsp), %rbx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
cmpq $0xff, %rbp
ja 0xce044
movq 0x8(%rsp), %rax
movl %r12d, 0xd0(%rax,%rbp,4)
incq %rbp
cmpq %rbp, %r13
jne 0xcdf41
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq