// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil.c
// Function: fmtutil_macbitmap_read_colortable
int fmtutil_macbitmap_read_colortable(deark *c, dbuf *f,
	struct fmtutil_macbitmap_info *bi, i64 pos, i64 *bytes_used)
{
	i64 ct_id;
	u32 ct_flags;
	i64 ct_size;
	i64 k, z;
	u32 s[4];
	u8 cr, cg, cb;
	u32 clr;
	char tmps[64];

	*bytes_used = 0;
	de_dbg(c, "color table at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	ct_id = dbuf_getu32be(f, pos);
	ct_flags = (u32)dbuf_getu16be(f, pos+4); // a.k.a. transIndex
	ct_size = dbuf_getu16be(f, pos+6);
	bi->num_pal_entries = ct_size+1;
	de_dbg(c, "color table id=0x%08x, flags=0x%04x, colors=%d", (unsigned int)ct_id,
		(unsigned int)ct_flags, (int)bi->num_pal_entries);

	for(k=0; k<bi->num_pal_entries; k++) {
		for(z=0; z<4; z++) {
			s[z] = (u32)dbuf_getu16be(f, pos+8+8*k+2*z);
		}
		cr = (u8)(s[1]>>8);
		cg = (u8)(s[2]>>8);
		cb = (u8)(s[3]>>8);
		clr = DE_MAKE_RGB(cr,cg,cb);
		de_snprintf(tmps, sizeof(tmps), "(%5d,%5d,%5d,idx=%3d) "DE_CHAR_RIGHTARROW" ",
			(int)s[1], (int)s[2], (int)s[3], (int)s[0]);
		de_dbg_pal_entry2(c, k, clr, tmps, NULL, NULL);

		// Some files don't have the palette indices set. Most PICT decoders ignore
		// the indices if the "device" flag of ct_flags is set, and that seems to
		// work (though it's not clearly documented).
		if(ct_flags & 0x8000U) {
			s[0] = (u32)k;
		}

		if(s[0]<=255) {
			bi->pal[s[0]] = clr;
		}
	}

	de_dbg_indent(c, -1);
	*bytes_used = 8 + 8*bi->num_pal_entries;
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x60509(%rip), %rsi     # 0x1d63f2
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x165470
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x4, %rsi
callq 0x1651d0
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x6, %rsi
callq 0x1651d0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rcx
addq $0x1, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movl -0x34(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rax
movl %eax, %r8d
leaq 0x6049e(%rip), %rsi     # 0x1d6405
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x78(%rcx), %rax
jge 0x176099
movq $0x0, -0x50(%rbp)
cmpq $0x4, -0x50(%rbp)
jge 0x175fd5
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
addq $0x8, %rsi
movq -0x48(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x50(%rbp), %rax
shlq %rax
addq %rax, %rsi
callq 0x1651d0
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, -0x60(%rbp,%rax,4)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x175f90
movl -0x5c(%rbp), %eax
shrl $0x8, %eax
movb %al, -0x61(%rbp)
movl -0x58(%rbp), %eax
shrl $0x8, %eax
movb %al, -0x62(%rbp)
movl -0x54(%rbp), %eax
shrl $0x8, %eax
movb %al, -0x63(%rbp)
movzbl -0x61(%rbp), %eax
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movzbl -0x62(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x63(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x68(%rbp)
leaq -0xb0(%rbp), %rdi
movl -0x5c(%rbp), %ecx
movl -0x58(%rbp), %r8d
movl -0x54(%rbp), %r9d
movl -0x60(%rbp), %eax
movl $0x40, %esi
leaq 0x60405(%rip), %rdx     # 0x1d6434
movl %eax, (%rsp)
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x68(%rbp), %edx
leaq -0xb0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
movl -0x34(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x17606c
movq -0x48(%rbp), %rax
movl %eax, -0x60(%rbp)
cmpl $0xff, -0x60(%rbp)
ja 0x176086
movl -0x68(%rbp), %edx
movq -0x18(%rbp), %rax
movl -0x60(%rbp), %ecx
movl %edx, 0x80(%rax,%rcx,4)
jmp 0x176088
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x175f76
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x78(%rax), %rcx
shlq $0x3, %rcx
addq $0x8, %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movl $0x1, %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)