// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/rosprite.c
// Function: do_setup_palette
static void do_setup_palette(deark *c, lctx *d, struct page_ctx *pg)
{
	i64 k;
	de_color clr1, clr2, clr3;

	if(pg->fgbpp>8) {
		return;
	}

	if(pg->has_custom_palette) {
		de_dbg(c, "custom palette at %d, %d entries", (int)pg->custom_palette_pos,
			(int)pg->custom_palette_ncolors);
	}
	de_dbg_indent(c, 1);

	for(k=0; k<256; k++) {
		if(pg->has_custom_palette) {
			if(k<pg->custom_palette_ncolors) {
				// Each palette entry has two colors, which are usually but not always
				// the same.
				// TODO: Figure out what to do if they are different. For now, we'll
				// average them.
				clr1 = dbuf_getRGB(c->infile, pg->custom_palette_pos + 8*k + 1, 0);
				clr2 = dbuf_getRGB(c->infile, pg->custom_palette_pos + 8*k + 4 + 1, 0);
				if(clr1==clr2) {
					pg->pal[k] = clr1;
					de_dbg_pal_entry(c, k, clr1);
				}
				else {
					char tmps[64];

					clr3 = average_color(clr1, clr2);
					pg->pal[k] = clr3;
					de_snprintf(tmps, sizeof(tmps), "(%3d,%3d,%3d),(%3d,%3d,%3d) "DE_CHAR_RIGHTARROW" ",
						(int)DE_COLOR_R(clr1), (int)DE_COLOR_G(clr1), (int)DE_COLOR_B(clr1),
						(int)DE_COLOR_R(clr2), (int)DE_COLOR_G(clr2), (int)DE_COLOR_B(clr2));
					de_dbg_pal_entry2(c, k, clr3, tmps, NULL, NULL);
				}
			}
			else {
				pg->pal[k] = getpal256((int)k);
			}
		}
		else if(pg->fgbpp==4 && k<16) {
			pg->pal[k] = getpal16((int)k);
		}
		else if(pg->fgbpp==2 && k<4) {
			pg->pal[k] = getpal4((int)k);
		}
		else if(pg->fgbpp==1 && k<2) {
			pg->pal[k] = (k==0)?DE_STOCKCOLOR_WHITE:DE_STOCKCOLOR_BLACK;
		}
		else {
			pg->pal[k] = getpal256((int)k);
		}
	}

	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
cmpq $0x8, (%rax)
jle 0x121626
jmp 0x1218e4
movq -0x18(%rbp), %rax
cmpl $0x0, 0xa8(%rax)
je 0x12165f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0xb8(%rax), %rax
movl %eax, %ecx
leaq 0xa4b20(%rip), %rsi     # 0x1c6178
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq $0x0, -0x20(%rbp)
cmpq $0x100, -0x20(%rbp)     # imm = 0x100
jge 0x1218d6
movq -0x18(%rbp), %rax
cmpl $0x0, 0xa8(%rax)
je 0x121809
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0xb8(%rcx), %rax
jge 0x1217e8
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rsi
movq -0x20(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
addq $0x1, %rsi
xorl %edx, %edx
callq 0x165ff0
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0xb0(%rax), %rsi
movq -0x20(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
addq $0x4, %rsi
addq $0x1, %rsi
xorl %edx, %edx
callq 0x165ff0
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jne 0x121734
movl -0x24(%rbp), %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x24(%rbp), %edx
callq 0x15bf00
jmp 0x1217e6
movl -0x24(%rbp), %edi
movl -0x28(%rbp), %esi
callq 0x121ce0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
leaq -0x70(%rbp), %rdi
movl -0x24(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movzbl %al, %ecx
movl -0x24(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movzbl %al, %r8d
movl -0x24(%rbp), %eax
andl $0xff, %eax
movzbl %al, %r9d
movl -0x28(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movzbl %al, %r11d
movl -0x28(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movzbl %al, %r10d
movl -0x28(%rbp), %eax
andl $0xff, %eax
movzbl %al, %eax
movl $0x40, %esi
leaq 0xa49e3(%rip), %rdx     # 0x1c6199
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x2c(%rbp), %edx
leaq -0x70(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
jmp 0x121804
movq -0x20(%rbp), %rax
movl %eax, %edi
callq 0x121da0
movl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
jmp 0x1218c3
movq -0x18(%rbp), %rax
cmpq $0x4, (%rax)
jne 0x12183b
cmpq $0x10, -0x20(%rbp)
jge 0x12183b
movq -0x20(%rbp), %rax
movl %eax, %edi
callq 0x121ec0
movl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
jmp 0x1218c1
movq -0x18(%rbp), %rax
cmpq $0x2, (%rax)
jne 0x12186a
cmpq $0x4, -0x20(%rbp)
jge 0x12186a
movq -0x20(%rbp), %rax
movl %eax, %edi
callq 0x121f00
movl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
jmp 0x1218bf
movq -0x18(%rbp), %rax
cmpq $0x1, (%rax)
jne 0x1218a1
cmpq $0x2, -0x20(%rbp)
jge 0x1218a1
movq -0x20(%rbp), %rcx
movl $0xff000000, %edx       # imm = 0xFF000000
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x0, %rcx
cmovel %eax, %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
jmp 0x1218bd
movq -0x20(%rbp), %rax
movl %eax, %edi
callq 0x121da0
movl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x20(%rbp), %rcx
movl %edx, 0xc0(%rax,%rcx,4)
jmp 0x1218bf
jmp 0x1218c1
jmp 0x1218c3
jmp 0x1218c5
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x121675
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)