// File: /workspace/github_c_makefile_star2_O1/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, 0x38(%rsp)
movq %rsi, %rbp
movq %rdi, 0x8(%rsp)
movl 0xd0(%rsp), %eax
movq 0xc0(%rsp), %r13
testb $0x10, %al
movq %rdx, 0x10(%rsp)
je 0xf3349
leaq (,%r13,4), %rdx
movq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x18140
movq 0x10(%rsp), %rdx
movl 0xd0(%rsp), %eax
testb $0x8, %al
jne 0xf3381
leaq 0x42e4d(%rip), %rsi     # 0x1361a8
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rax
cmpq %rax, %r13
cmovgeq %rax, %r13
testq %rax, %rax
jle 0xf358c
movl 0xc8(%rsp), %eax
decl %eax
movl %eax, 0x1c(%rsp)
movq %rdx, %r12
xorl %r14d, %r14d
movq $0x0, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq %r14, %rsi
imulq %r15, %rsi
addq %rdx, %rsi
cmpq 0x18(%rbp), %rsi
jge 0xf33f1
cmpl $0x2, 0x1c(%rsp)
jae 0xf3403
xorl %ebx, %ebx
leaq (%r12,%rbx), %rsi
movq %rbp, %rdi
callq 0xef0d5
movb %al, (%rsp,%rbx)
incq %rbx
cmpq $0x3, %rbx
jne 0xf33d0
movl 0xc8(%rsp), %edx
jmp 0xf344a
movb $0x1, %al
xorl %ebx, %ebx
cmpq %r13, %r14
jl 0xf356c
jmp 0xf3577
movl 0xc8(%rsp), %edx
cmpl $0x3, %edx
jne 0xf343f
movq %rbp, %rdi
callq 0xef169
movl 0xc8(%rsp), %edx
movl %eax, %ecx
shrl $0x8, %ecx
andb $0xf, %cl
movb %cl, (%rsp)
movl %eax, %ecx
shrb $0x4, %cl
movb %cl, 0x1(%rsp)
movq %rax, 0x20(%rsp)
andb $0xf, %al
movb %al, 0x2(%rsp)
jmp 0xf344a
movb $0x0, 0x2(%rsp)
movw $0x0, (%rsp)
movl 0xd0(%rsp), %eax
testb $0x1, %al
je 0xf3463
movb (%rsp), %al
movb 0x2(%rsp), %cl
movb %cl, (%rsp)
movb %al, 0x2(%rsp)
cmpl $0x2, %edx
je 0xf3489
cmpl $0x3, %edx
jne 0xf34a6
xorl %eax, %eax
movzbl (%rsp,%rax), %ecx
movl %ecx, %edx
shll $0x4, %edx
addl %ecx, %edx
movb %dl, 0x4(%rsp,%rax)
incq %rax
cmpq $0x3, %rax
jne 0xf346f
jmp 0xf34b7
xorl %ebx, %ebx
movzbl (%rsp,%rbx), %edi
andl $0x3f, %edi
callq 0xecde3
movb %al, 0x4(%rsp,%rbx)
incq %rbx
cmpq $0x3, %rbx
jne 0xf348b
jmp 0xf34b7
movb 0x2(%rsp), %al
movb %al, 0x6(%rsp)
movzwl (%rsp), %eax
movw %ax, 0x4(%rsp)
movzbl 0x4(%rsp), %eax
shll $0x10, %eax
movzbl 0x5(%rsp), %ecx
shll $0x8, %ecx
orl %eax, %ecx
movzbl 0x6(%rsp), %ebx
orl %ecx, %ebx
orl $0xff000000, %ebx       # imm = 0xFF000000
movl 0xc8(%rsp), %eax
cmpl $0x3, %eax
je 0xf3514
cmpl $0x2, %eax
jne 0xf3553
movzbl (%rsp), %ecx
movzbl 0x1(%rsp), %r8d
movzbl 0x2(%rsp), %r9d
movl $0x40, %esi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x55b98(%rip), %rdx     # 0x1490a3
xorl %eax, %eax
callq 0xe8d67
jmp 0xf3534
movl $0x40, %esi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x4c5ad(%rip), %rdx     # 0x13fad5
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0xe8d67
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
movq %r13, %rcx
movq 0x28(%rsp), %r13
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
jmp 0xf3562
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0xe9d68
cmpq %r13, %r14
movq 0x10(%rsp), %rdx
jge 0xf3575
movq 0x30(%rsp), %rax
movl %ebx, (%rax,%r14,4)
xorl %eax, %eax
testb %al, %al
jne 0xf358c
incq %r14
addq %r15, %r12
cmpq 0x38(%rsp), %r14
jne 0xf33b7
movl 0xd0(%rsp), %eax
testb $0x8, %al
jne 0xf35a6
movq 0x8(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq