// File: /workspace/github_c_makefile_star2_O3/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 0x510ee(%rip), %rsi     # 0x14a942
movq %rcx, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r12, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %r13, %rdi
movq %r15, %rsi
callq 0xeed0d
movq %rax, %rbx
leaq 0x4(%r15), %rsi
movq %r13, %rdi
callq 0xeeb67
movq %rax, %rbp
leaq 0x6(%r15), %rsi
movq %r13, %rdi
callq 0xeeb67
leaq 0x1(%rax), %r8
movq %r8, 0x78(%r14)
leaq 0x510b2(%rip), %rsi     # 0x14a955
movq %r12, 0x28(%rsp)
movq %r12, %rdi
movl %ebx, %edx
movq %rbp, 0x38(%rsp)
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe8617
movq %r14, 0x8(%rsp)
cmpq $0x0, 0x78(%r14)
jle 0xf9996
addq $0x8, %r15
xorl %r12d, %r12d
xorl %ebx, %ebx
leaq (%r15,%rbx), %rsi
movq %r13, %rdi
callq 0xeeb67
movl %eax, 0x10(%rsp,%rbx,2)
addq $0x2, %rbx
cmpq $0x8, %rbx
jne 0xf98d4
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)
movl $0x40, %esi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
leaq 0x5104c(%rip), %rdx     # 0x14a984
movl %eax, %r9d
xorl %eax, %eax
callq 0xe8193
movq 0x28(%rsp), %rdi
movq %r12, %rsi
movl %ebp, %edx
movq %r14, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
cmpw $0x0, 0x38(%rsp)
jns 0xf996a
movl %r12d, 0x10(%rsp)
movl %r12d, %ebx
cmpl $0xff, %ebx
ja 0xf9980
movl %ebx, %eax
movq 0x8(%rsp), %rcx
movl %ebp, 0x80(%rcx,%rax,4)
incq %r12
addq $0x8, %r15
movq 0x8(%rsp), %rax
cmpq 0x78(%rax), %r12
jl 0xf98d2
movq 0x28(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x8(%rsp), %rax
movq 0x78(%rax), %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