// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/src/deark-bitmap.c
// Function: de_read_simple_palette
void de_read_simple_palette(deark *c, dbuf *f, i64 fpos,
	i64 ncolors_to_read, i64 entryspan,
	de_color *pal, i64 ncolors_to_save, UI paltype, UI flags)
{
	i64 k;
	i64 i;
	i64 pos = fpos;
	UI clr1 = 0;
	u8 samp1[3];
	u8 samp2[3];
	char tmps[64];

	if(flags & DE_RDPALFLAG_INITPAL) {
		de_zeromem(pal, (size_t)ncolors_to_save*sizeof(de_color));
	}

	if(!(flags & DE_RDPALFLAG_NOHEADER)) {
		de_dbg(c, "palette at %"I64_FMT, fpos);
		de_dbg_indent(c, 1);
	}

	if(ncolors_to_save > ncolors_to_read) {
		ncolors_to_save = ncolors_to_read;
	}

	for(k=0; k<ncolors_to_read; k++) {
		de_color clr;

		pos = fpos + k*entryspan;
		if(pos >= f->len) {
			if(k < ncolors_to_save) {
				pal[k] = 0;
				continue;
			}
			goto done;
		}

		switch(paltype) {
		case DE_RDPALTYPE_24BIT:
		case DE_RDPALTYPE_VGA18BIT:
			for(i=0; i<3; i++) {
				samp1[i] = dbuf_getbyte(f, pos+i);
			}
			break;
		case DE_RDPALTYPE_AMIGA12BIT:
			clr1 = (UI)dbuf_getu16be(f, pos);
			samp1[0] = (u8)((clr1>>8)&0x0f);
			samp1[1] = (u8)((clr1>>4)&0x0f);
			samp1[2] = (u8)(clr1&0x0f);
			break;
		default:
			for(i=0; i<3; i++) {
				samp1[i] = 0;
			}
		}

		if(flags & DE_RDPALFLAG_BGR) {
			u8 tmpsamp;
			tmpsamp = samp1[0];
			samp1[0] = samp1[2];
			samp1[2] = tmpsamp;
		}

		switch(paltype) {
		case DE_RDPALTYPE_VGA18BIT:
			for(i=0; i<3; i++) {
				samp2[i] = de_scale_63_to_255(samp1[i] & 0x3f);
			}
			break;
		case DE_RDPALTYPE_AMIGA12BIT:
			for(i=0; i<3; i++) {
				samp2[i] = 17 * samp1[i];
			}
			break;
		default:
			for(i=0; i<3; i++) {
				samp2[i] = samp1[i];
			}
			break;
		}

		clr = DE_MAKE_RGB(samp2[0], samp2[1], samp2[2]);

		switch(paltype) {
		case DE_RDPALTYPE_VGA18BIT:
			de_snprintf(tmps, sizeof(tmps), "(%2u,%2u,%2u) "DE_CHAR_RIGHTARROW" ",
				(UI)samp1[0], (UI)samp1[1], (UI)samp1[2]);
			de_dbg_pal_entry2(c, k, clr, tmps, NULL, NULL);
			break;
		case DE_RDPALTYPE_AMIGA12BIT:
			de_snprintf(tmps, sizeof(tmps), "0x%04x "DE_CHAR_RIGHTARROW" ", clr1);
			de_dbg_pal_entry2(c, k, clr, tmps, NULL, NULL);
			break;
		default:
			de_dbg_pal_entry(c, k, clr);
			break;
		}

		if(k < ncolors_to_save) {
			pal[k] = clr;
		}
	}

done:
	if(!(flags & DE_RDPALFLAG_NOHEADER)) {
		de_dbg_indent(c, -1);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x30(%rsp)
movq %r8, %r15
movq %rcx, %r12
movq %rsi, %rbp
movq %rdi, 0x8(%rsp)
movl 0xd0(%rsp), %eax
movq 0xc0(%rsp), %rcx
testb $0x10, %al
movq %rdx, 0x10(%rsp)
je 0xe7a25
leaq (,%rcx,4), %rdx
movq 0x30(%rsp), %rdi
xorl %esi, %esi
movq %rcx, %rbx
callq 0x18140
movq %rbx, %rcx
movq 0x10(%rsp), %rdx
movl 0xc8(%rsp), %esi
movl 0xd0(%rsp), %eax
testb $0x8, %al
jne 0xe7a6f
leaq 0x418ad(%rip), %rsi     # 0x1292eb
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
movq %rcx, %r14
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %r14, %rcx
movl 0xc8(%rsp), %esi
movq 0x10(%rsp), %rdx
cmpq %r12, %rcx
cmovgeq %r12, %rcx
xorl %eax, %eax
testq %r12, %r12
cmovleq %rax, %r12
leal -0x1(%rsi), %eax
movl %eax, 0x1c(%rsp)
movq %rdx, %r13
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
movq %rcx, 0x38(%rsp)
movq %r12, 0x28(%rsp)
cmpq %r12, %rbx
je 0xe7c92
movq %rbx, %rsi
imulq %r15, %rsi
addq %rdx, %rsi
cmpq 0x18(%rbp), %rsi
jge 0xe7add
cmpl $0x2, 0x1c(%rsp)
jae 0xe7aee
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0xe7b2a
leaq (%r14,%r13), %rsi
movq %rbp, %rdi
callq 0xe3eb4
movb %al, 0x2(%rsp,%r14)
incq %r14
jmp 0xe7ac1
xorl %r14d, %r14d
cmpq %rcx, %rbx
jl 0xe7c68
jmp 0xe7c92
movl 0xc8(%rsp), %r14d
cmpl $0x3, %r14d
jne 0xe7c7c
movq %rbp, %rdi
callq 0xe3f46
movl %eax, %ecx
shrl $0x8, %ecx
andb $0xf, %cl
movb %cl, 0x2(%rsp)
movl %eax, %ecx
shrb $0x4, %cl
movb %cl, 0x3(%rsp)
movq %rax, 0x20(%rsp)
andb $0xf, %al
movb %al, 0x4(%rsp)
jmp 0xe7b32
movl 0xc8(%rsp), %r14d
movl 0xd0(%rsp), %eax
testb $0x1, %al
je 0xe7b4d
movb 0x2(%rsp), %al
movb 0x4(%rsp), %cl
movb %cl, 0x2(%rsp)
movb %al, 0x4(%rsp)
cmpl $0x2, %r14d
je 0xe7bea
xorl %eax, %eax
cmpl $0x3, %r14d
jne 0xe7b81
cmpq $0x3, %rax
je 0xe7b87
movzbl 0x2(%rsp,%rax), %ecx
imull $0x11, %ecx, %ecx
movb %cl, 0x5(%rsp,%rax)
incq %rax
jmp 0xe7b5f
movb 0x2(%rsp,%rax), %cl
movb %cl, 0x5(%rsp,%rax)
incq %rax
cmpq $0x3, %rax
jne 0xe7b76
movzbl 0x5(%rsp), %eax
shll $0x10, %eax
movzbl 0x6(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x7(%rsp), %r14d
orl %ecx, %r14d
orl $0xff000000, %r14d      # imm = 0xFF000000
movl 0xc8(%rsp), %eax
cmpl $0x3, %eax
je 0xe7c0b
cmpl $0x2, %eax
jne 0xe7c49
movzbl 0x2(%rsp), %ecx
movzbl 0x3(%rsp), %r8d
movzbl 0x4(%rsp), %r9d
leaq 0x40(%rsp), %r12
movq %r12, %rdi
pushq $0x40
popq %rsi
leaq 0x5458a(%rip), %rdx     # 0x13c16b
xorl %eax, %eax
callq 0xde40c
jmp 0xe7c29
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0xe7b87
movzbl 0x2(%rsp,%r14), %edi
andl $0x3f, %edi
callq 0xe1cec
movb %al, 0x5(%rsp,%r14)
incq %r14
jmp 0xe7bed
leaq 0x40(%rsp), %r12
movq %r12, %rdi
pushq $0x40
popq %rsi
leaq 0x4af90(%rip), %rdx     # 0x132bad
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0xde40c
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
movq %r12, %rcx
movq 0x28(%rsp), %r12
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
jmp 0xe7c59
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0xdf23b
movq 0x38(%rsp), %rcx
cmpq %rcx, %rbx
movq 0x10(%rsp), %rdx
jge 0xe7c71
movq 0x30(%rsp), %rax
movl %r14d, (%rax,%rbx,4)
incq %rbx
addq %r15, %r13
jmp 0xe7a9e
xorl %eax, %eax
cmpq $0x3, %rax
je 0xe7b32
movb $0x0, 0x2(%rsp,%rax)
incq %rax
jmp 0xe7c7e
movl 0xd0(%rsp), %eax
testb $0x8, %al
jne 0xe7caa
pushq $-0x1
popq %rsi
movq 0x8(%rsp), %rdi
callq 0xdeba7
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq