// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
movq %r8, 0x30(%rsp)
movq $0x0, (%r8)
leaq 0x50636(%rip), %rsi     # 0x14a392
movq %rcx, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r12, %rdi
movl $0x1, %esi
callq 0xe9585
movq %r13, %rdi
movq %r15, %rsi
callq 0xef32a
movq %rax, %rbx
leaq 0x4(%r15), %rsi
movq %r13, %rdi
callq 0xef169
movq %rax, %rbp
leaq 0x6(%r15), %rsi
movq %r13, %rdi
callq 0xef169
leaq 0x1(%rax), %r8
movq %r8, 0x78(%r14)
leaq 0x505fa(%rip), %rsi     # 0x14a3a5
movq %r12, 0x28(%rsp)
movq %r12, %rdi
movl %ebx, %edx
movq %rbp, 0x38(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x78(%r14)
jle 0xf9e8f
addq $0x8, %r15
leaq 0x40(%rsp), %rbx
xorl %r12d, %r12d
xorl %ebp, %ebp
leaq (%r15,%rbp), %rsi
movq %r13, %rdi
callq 0xef169
movl %eax, 0x10(%rsp,%rbp,2)
addq $0x2, %rbp
cmpq $0x8, %rbp
jne 0xf9ddc
movl 0x10(%rsp), %edx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x1c(%rsp), %eax
movl %ecx, %esi
shll $0x8, %esi
andl $0xff0000, %esi         # imm = 0xFF0000
movl %r8d, %edi
andl $0xff00, %edi           # imm = 0xFF00
orl %esi, %edi
movzbl %ah, %esi
leal (%rsi,%rdi), %ebp
addl $0xff000000, %ebp       # imm = 0xFF000000
movl %edx, (%rsp)
movl $0x40, %esi
movq %rbx, %rdi
leaq 0x50599(%rip), %rdx     # 0x14a3d4
movl %eax, %r9d
xorl %eax, %eax
callq 0xe8d67
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
cmpw $0x0, 0x38(%rsp)
jns 0xf9e6a
movl %r12d, 0x10(%rsp)
movl 0x10(%rsp), %eax
cmpq $0xff, %rax
ja 0xf9e7e
movl %ebp, 0x80(%r14,%rax,4)
incq %r12
addq $0x8, %r15
cmpq 0x78(%r14), %r12
jl 0xf9dda
movq 0x28(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x78(%r14), %rax
leaq 0x8(,%rax,8), %rax
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
movl $0x1, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq