// File: /workspace/github_c_makefile_star2_O3/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 0xf2d08
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 0xf2d40
leaq 0x438d1(%rip), %rsi     # 0x1365eb
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rax
cmpq %rax, %r13
cmovgeq %rax, %r13
testq %rax, %rax
jle 0xf2f4d
movl 0xc8(%rsp), %eax
decl %eax
movl %eax, 0x1c(%rsp)
movq %rdx, %r12
xorl %ebx, %ebx
movq $0x0, 0x20(%rsp)
movq %r13, 0x28(%rsp)
movq %rbx, %rsi
imulq %r15, %rsi
addq %rdx, %rsi
cmpq 0x18(%rbp), %rsi
jge 0xf2db1
cmpl $0x2, 0x1c(%rsp)
jae 0xf2dc2
xorl %r14d, %r14d
leaq (%r12,%r14), %rsi
movq %rbp, %rdi
callq 0xeead3
movb %al, (%rsp,%r14)
incq %r14
cmpq $0x3, %r14
jne 0xf2d8f
movl 0xc8(%rsp), %edx
jmp 0xf2e09
xorl %r14d, %r14d
cmpq %r13, %rbx
jl 0xf2f33
jmp 0xf2f4d
movl 0xc8(%rsp), %edx
cmpl $0x3, %edx
jne 0xf2dfe
movq %rbp, %rdi
callq 0xeeb67
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 0xf2e09
movb $0x0, 0x2(%rsp)
movw $0x0, (%rsp)
movl 0xd0(%rsp), %eax
testb $0x1, %al
je 0xf2e22
movb (%rsp), %al
movb 0x2(%rsp), %cl
movb %cl, (%rsp)
movb %al, 0x2(%rsp)
cmpl $0x2, %edx
je 0xf2e48
cmpl $0x3, %edx
jne 0xf2e68
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 0xf2e2e
jmp 0xf2e79
xorl %r14d, %r14d
movzbl (%rsp,%r14), %edi
andl $0x3f, %edi
callq 0xec78a
movb %al, 0x4(%rsp,%r14)
incq %r14
cmpq $0x3, %r14
jne 0xf2e4b
jmp 0xf2e79
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), %r14d
orl %ecx, %r14d
orl $0xff000000, %r14d      # imm = 0xFF000000
movl 0xc8(%rsp), %eax
cmpl $0x3, %eax
je 0xf2ed9
cmpl $0x2, %eax
jne 0xf2f19
movzbl (%rsp), %ecx
movzbl 0x1(%rsp), %r8d
movzbl 0x2(%rsp), %r9d
movl $0x40, %esi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x5676b(%rip), %rdx     # 0x14963b
xorl %eax, %eax
callq 0xe8193
jmp 0xf2ef9
movl $0x40, %esi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
leaq 0x4cff0(%rip), %rdx     # 0x13fedd
movq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0xe8193
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
movq %r13, %rcx
movq 0x28(%rsp), %r13
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
jmp 0xf2f29
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
callq 0xe91fc
cmpq %r13, %rbx
movq 0x10(%rsp), %rdx
jge 0xf2f3c
movq 0x30(%rsp), %rax
movl %r14d, (%rax,%rbx,4)
incq %rbx
addq %r15, %r12
cmpq 0x38(%rsp), %rbx
jne 0xf2d75
movl 0xd0(%rsp), %eax
testb $0x8, %al
jne 0xf2f67
movq 0x8(%rsp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq