// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/abk.c
// Function: do_read_sprite_or_pic_palette
static void do_read_sprite_or_pic_palette(deark *c, lctx *d, struct amosbank *bk)
{
	i64 k;
	UI n;
	u8 cr, cg, cb;
	u8 cr1, cg1, cb1;
	i64 pos;
	i64 colors_used;
	char tmps[64];

	pos = bk->pal_pos;
	de_dbg(c, "palette at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	colors_used = de_pow2(bk->max_planes);

	for(k=0; k<32; k++) {
		n = (UI)dbuf_getu16be(bk->f, pos+k*2);
		cr1 = (u8)((n>>8)&0xf);
		cg1 = (u8)((n>>4)&0xf);
		cb1 = (u8)(n&0xf);
		cr = cr1*17;
		cg = cg1*17;
		cb = cb1*17;
		bk->pal[k] = DE_MAKE_RGB(cr, cg, cb);
		de_snprintf(tmps, sizeof(tmps), "0x%04x (%2d,%2d,%2d) "DE_CHAR_RIGHTARROW" ",
			n, (int)cr1, (int)cg1, (int)cb1);
		de_dbg_pal_entry2(c, k, bk->pal[k], tmps, NULL,
			(k>=colors_used)?" [unused]":"");

		// Set up colors #32-63 for 6-plane "Extra Half-Brite" mode.
		// For normal images (<=5 planes), these colors won't be used.
		bk->pal[k+32] = DE_MAKE_RGB(cr/2, cg/2, cb/2);
	}

	bk->pal[0] = DE_SET_ALPHA(bk->pal[0], 0); // First color is transparent.
	// (Don't know if pal[32] should be transparent also.)

	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0x1a5bdb(%rip), %rsi    # 0x1c1eed
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x18(%rbp), %rax
movq 0x488(%rax), %rdi
callq 0x15ce50
movq %rax, -0x40(%rbp)
movq $0x0, -0x20(%rbp)
cmpq $0x20, -0x20(%rbp)
jge 0x1c4aa
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rax
shlq %rax
addq %rax, %rsi
callq 0x1651d0
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xf, %eax
movb %al, -0x28(%rbp)
movl -0x24(%rbp), %eax
shrl $0x4, %eax
andl $0xf, %eax
movb %al, -0x29(%rbp)
movl -0x24(%rbp), %eax
andl $0xf, %eax
movb %al, -0x2a(%rbp)
movzbl -0x28(%rbp), %eax
imull $0x11, %eax, %eax
movb %al, -0x25(%rbp)
movzbl -0x29(%rbp), %eax
imull $0x11, %eax, %eax
movb %al, -0x26(%rbp)
movzbl -0x2a(%rbp), %eax
imull $0x11, %eax, %eax
movb %al, -0x27(%rbp)
movzbl -0x25(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0x26(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x27(%rbp), %eax
orl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0x70(%rax,%rcx,4)
leaq -0x80(%rbp), %rdi
movl -0x24(%rbp), %ecx
movzbl -0x28(%rbp), %r8d
movzbl -0x29(%rbp), %r9d
movzbl -0x2a(%rbp), %eax
movl $0x40, %esi
leaq 0x180b76(%rip), %rdx    # 0x19cf6a
movl %eax, (%rsp)
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x70(%rax,%rcx,4), %edx
leaq -0x80(%rbp), %rcx
movq -0x20(%rbp), %r8
movq -0x40(%rbp), %r10
leaq 0x1b47ab(%rip), %r9     # 0x1d0bd0
leaq 0x180b58(%rip), %rax    # 0x19cf84
cmpq %r10, %r8
cmovgeq %rax, %r9
xorl %eax, %eax
movl %eax, %r8d
callq 0x15bdb0
movzbl -0x25(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
shll $0x10, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, -0x88(%rbp)
movzbl -0x26(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x88(%rbp), %eax
shll $0x8, %ecx
orl %ecx, %eax
movl %eax, -0x84(%rbp)
movzbl -0x27(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl -0x84(%rbp), %edx
orl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xf0(%rax,%rcx,4)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x1c343
movq -0x18(%rbp), %rax
movl 0x70(%rax), %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
orl $0x0, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x70(%rax)
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax,%rax)