// File: /workspace/github_c_makefile_star2_O2/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, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
movq %r8, 0x30(%rsp)
andq $0x0, (%r8)
leaq 0x4f59b(%rip), %rsi     # 0x13d472
movq %rcx, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0xdeba7
movq %r13, %rdi
movq %r12, %rsi
callq 0xe404f
movq %rax, %rbx
leaq 0x4(%r12), %rsi
movq %r13, %rdi
callq 0xe3f46
movq %rax, %rbp
leaq 0x6(%r12), %rsi
movq %r13, %rdi
callq 0xe3f46
leaq 0x1(%rax), %r8
movq %r14, 0x8(%rsp)
movq %r8, 0x78(%r14)
leaq 0x4f55a(%rip), %rsi     # 0x13d485
movq %r15, 0x28(%rsp)
movq %r15, %rdi
movl %ebx, %edx
movq %rbp, 0x38(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0xde896
addq $0x8, %r12
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
cmpq 0x78(%rax), %r15
jge 0xee013
xorl %ebx, %ebx
cmpq $0x8, %rbx
je 0xedf77
leaq (%r12,%rbx), %rsi
movq %r13, %rdi
callq 0xe3f46
movl %eax, 0x10(%rsp,%rbx,2)
addq $0x2, %rbx
jmp 0xedf5b
movl 0x10(%rsp), %ebx
movl 0x14(%rsp), %ecx
movl 0x18(%rsp), %r8d
movl 0x1c(%rsp), %eax
movl %ecx, %edx
shll $0x8, %edx
andl $0xff0000, %edx         # imm = 0xFF0000
movl %r8d, %esi
andl $0xff00, %esi           # imm = 0xFF00
orl %edx, %esi
movzbl %ah, %edx
leal (%rdx,%rsi), %ebp
addl $0xff000000, %ebp       # imm = 0xFF000000
movl %ebx, (%rsp)
leaq 0x40(%rsp), %r14
movq %r14, %rdi
pushq $0x40
popq %rsi
leaq 0x4f4f5(%rip), %rdx     # 0x13d4b4
movl %eax, %r9d
xorl %eax, %eax
callq 0xde40c
movq 0x28(%rsp), %rdi
movq %r15, %rsi
movl %ebp, %edx
movq %r14, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
cmpw $0x0, 0x38(%rsp)
jns 0xedff1
movl %r15d, 0x10(%rsp)
movl %r15d, %ebx
cmpl $0xff, %ebx
ja 0xee007
movl %ebx, %eax
movq 0x8(%rsp), %rcx
movl %ebp, 0x80(%rcx,%rax,4)
incq %r15
addq $0x8, %r12
jmp 0xedf4a
pushq $-0x1
popq %rsi
movq 0x28(%rsp), %rdi
callq 0xdeba7
movq 0x8(%rsp), %rax
movq 0x78(%rax), %rax
leaq 0x8(,%rax,8), %rax
movq 0x30(%rsp), %rcx
movq %rax, (%rcx)
pushq $0x1
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq