// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movzbl 0x19(%rax), %eax
cmpl $0x0, %eax
jne 0x1313e0
movq -0x18(%rbp), %rax
cmpl $0x3, 0xc(%rax)
je 0x1313e5
jmp 0x1315ac
movq -0x18(%rbp), %rax
movq (%rax), %rax
movb $0x1, 0x19(%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl $0x3, %ecx
cqto
idivq %rcx
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
leaq 0x998e4(%rip), %rsi     # 0x1cacfb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x133fa0
movb %al, -0x31(%rbp)
cmpb $0x0, -0x31(%rbp)
je 0x131456
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
leaq 0x998c5(%rip), %rcx     # 0x1cad14
movb $0x0, %al
callq 0x12dfd0
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x1315ac
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
imulq $0x0, -0x28(%rbp), %rcx
addq -0x30(%rbp), %rcx
leaq -0x48(%rbp), %r8
callq 0x132fa0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
shlq $0x0, %rcx
addq -0x30(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x132fa0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x28(%rbp), %rcx
shlq %rcx
addq -0x30(%rbp), %rcx
leaq -0x58(%rbp), %r8
callq 0x132fa0
cmpb $0x0, -0x31(%rbp)
je 0x1314e8
movq -0x48(%rbp), %rax
movb %al, -0x59(%rbp)
movq -0x50(%rbp), %rax
movb %al, -0x5a(%rbp)
movq -0x58(%rbp), %rax
movb %al, -0x5b(%rbp)
jmp 0x131509
movq -0x48(%rbp), %rax
sarq $0x8, %rax
movb %al, -0x59(%rbp)
movq -0x50(%rbp), %rax
sarq $0x8, %rax
movb %al, -0x5a(%rbp)
movq -0x58(%rbp), %rax
sarq $0x8, %rax
movb %al, -0x5b(%rbp)
movzbl -0x59(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movzbl -0x5a(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x5b(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x60(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x13157d
leaq -0xb0(%rbp), %rdi
movq -0x48(%rbp), %rax
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %eax, %r8d
movq -0x58(%rbp), %rax
movl %eax, %r9d
movl $0x50, %esi
leaq 0x74e79(%rip), %rdx     # 0x1a63d0
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x60(%rbp), %edx
leaq -0xb0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
cmpq $0x100, -0x30(%rbp)     # imm = 0x100
jge 0x131599
movl -0x60(%rbp), %edx
movq -0x40(%rbp), %rax
movq -0x30(%rbp), %rcx
movl %edx, 0xd0(%rax,%rcx,4)
jmp 0x13159b
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x13145e
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)