// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/gemras.c
// Function: read_palette_ximg
static void read_palette_ximg(deark *c, lctx *d)
{
	i64 pal_entries_in_file;
	i64 pal_entries_to_read;
	i64 i;
	i64 cr1, cg1, cb1;
	u8 cr, cg, cb;
	int range_warned = 0;
	char tmps[64];

	pal_entries_in_file = (d->header_size_in_bytes-22)/3;
	if(pal_entries_in_file<1) return;
	if(d->nplanes<=8)
		pal_entries_to_read = de_pow2(d->nplanes);
	else
		pal_entries_to_read = 0;
	if(pal_entries_to_read>pal_entries_in_file)
		pal_entries_to_read = pal_entries_in_file;
	if(pal_entries_to_read>256)
		pal_entries_to_read = 256;

	if(pal_entries_in_file<1) return;

	de_dbg(c, "palette at %d", 22);
	de_dbg_indent(c, 1);
	for(i=0; i<pal_entries_to_read; i++) {
		cr1 = de_getu16be(22 + 6*i);
		cg1 = de_getu16be(22 + 6*i + 2);
		cb1 = de_getu16be(22 + 6*i + 4);

		cr = de_scale_1000_to_255(cr1);
		cg = de_scale_1000_to_255(cg1);
		cb = de_scale_1000_to_255(cb1);

		d->pal[i] = DE_MAKE_RGB(cr, cg, cb);

		de_snprintf(tmps, sizeof(tmps), "(%4d,%4d,%4d) "DE_CHAR_RIGHTARROW" ",
			(int)cr1, (int)cg1, (int)cb1);
		de_dbg_pal_entry2(c, (int)i, d->pal[i], tmps, NULL, NULL);

		// TODO: Maybe some out-of-range colors have special meaning?
		if(!range_warned && (cr1>1000 || cg1>1000 || cb1>1000)) {
			de_warn(c, "Bad palette color #%d: is (%d,%d,%d), max=(1000,1000,1000).",
				(int)i, (int)cr1, (int)cg1, (int)cb1);
			range_warned=1;
		}
	}
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
subq $0x16, %rax
movl $0x3, %ecx
cqto
idivq %rcx
movq %rax, -0x18(%rbp)
cmpq $0x1, -0x18(%rbp)
jge 0x71920
jmp 0x71b24
movq -0x10(%rbp), %rax
cmpq $0x8, 0x20(%rax)
jg 0x7193e
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x15ce50
movq %rax, -0x20(%rbp)
jmp 0x71946
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpq -0x18(%rbp), %rax
jle 0x71958
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x100, -0x20(%rbp)     # imm = 0x100
jle 0x7196a
movq $0x100, -0x20(%rbp)     # imm = 0x100
cmpq $0x1, -0x18(%rbp)
jge 0x71976
jmp 0x71b24
movq -0x8(%rbp), %rdi
leaq 0x14a5cf(%rip), %rsi    # 0x1bbf50
movl $0x16, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x71b16
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
imulq $0x6, -0x28(%rbp), %rsi
addq $0x16, %rsi
callq 0x1651d0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
imulq $0x6, -0x28(%rbp), %rsi
addq $0x16, %rsi
addq $0x2, %rsi
callq 0x1651d0
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
imulq $0x6, -0x28(%rbp), %rsi
addq $0x16, %rsi
addq $0x4, %rsi
callq 0x1651d0
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rdi
callq 0x161760
movb %al, -0x41(%rbp)
movq -0x38(%rbp), %rdi
callq 0x161760
movb %al, -0x42(%rbp)
movq -0x40(%rbp), %rdi
callq 0x161760
movb %al, -0x43(%rbp)
movzbl -0x41(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0x42(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x43(%rbp), %eax
orl %eax, %edx
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movl %edx, 0x70(%rax,%rcx,4)
leaq -0x90(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %eax, %r8d
movq -0x40(%rbp), %rax
movl %eax, %r9d
movl $0x40, %esi
leaq 0x12dd96(%rip), %rdx    # 0x19f810
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movslq %eax, %rsi
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x70(%rax,%rcx,4), %edx
leaq -0x90(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
cmpl $0x0, -0x48(%rbp)
jne 0x71b03
cmpq $0x3e8, -0x30(%rbp)     # imm = 0x3E8
jg 0x71ad0
cmpq $0x3e8, -0x38(%rbp)     # imm = 0x3E8
jg 0x71ad0
cmpq $0x3e8, -0x40(%rbp)     # imm = 0x3E8
jle 0x71b03
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl %eax, %edx
movq -0x30(%rbp), %rax
movl %eax, %ecx
movq -0x38(%rbp), %rax
movl %eax, %r8d
movq -0x40(%rbp), %rax
movl %eax, %r9d
leaq 0x139912(%rip), %rsi    # 0x1ab407
movb $0x0, %al
callq 0x15bc10
movl $0x1, -0x48(%rbp)
jmp 0x71b05
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x719a3
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)