// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl 0x20(%rbp), %eax
andl $0x10, %eax
cmpl $0x0, %eax
je 0x16c1ba
movq -0x30(%rbp), %rdi
movq 0x10(%rbp), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x19140
movl 0x20(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x16c1e9
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x55d19(%rip), %rsi     # 0x1c1eed
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq 0x10(%rbp), %rax
cmpq -0x20(%rbp), %rax
jle 0x16c1fb
movq -0x20(%rbp), %rax
movq %rax, 0x10(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x20(%rbp), %rax
jge 0x16c4fa
movq -0x18(%rbp), %rax
movq -0x38(%rbp), %rcx
imulq -0x28(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jl 0x16c256
movq -0x38(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0x16c251
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
jmp 0x16c4e9
jmp 0x16c4fc
movl 0x18(%rbp), %eax
movl %eax, -0xac(%rbp)
decl %eax
subl $0x2, %eax
jb 0x16c275
jmp 0x16c268
movl -0xac(%rbp), %eax
subl $0x3, %eax
je 0x16c2af
jmp 0x16c2e2
movq $0x0, -0x40(%rbp)
cmpq $0x3, -0x40(%rbp)
jge 0x16c2ad
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq -0x40(%rbp), %rsi
callq 0x165060
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, -0x4f(%rbp,%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x16c27d
jmp 0x16c30a
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x1651d0
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
shrl $0x8, %eax
andl $0xf, %eax
movb %al, -0x4f(%rbp)
movl -0x4c(%rbp), %eax
shrl $0x4, %eax
andl $0xf, %eax
movb %al, -0x4e(%rbp)
movl -0x4c(%rbp), %eax
andl $0xf, %eax
movb %al, -0x4d(%rbp)
jmp 0x16c30a
movq $0x0, -0x40(%rbp)
cmpq $0x3, -0x40(%rbp)
jge 0x16c308
movq -0x40(%rbp), %rax
movb $0x0, -0x4f(%rbp,%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x16c2ea
jmp 0x16c30a
movl 0x20(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x16c32d
movb -0x4f(%rbp), %al
movb %al, -0xa5(%rbp)
movb -0x4d(%rbp), %al
movb %al, -0x4f(%rbp)
movb -0xa5(%rbp), %al
movb %al, -0x4d(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0xb0(%rbp)
subl $0x2, %eax
je 0x16c34a
jmp 0x16c33d
movl -0xb0(%rbp), %eax
subl $0x3, %eax
je 0x16c387
jmp 0x16c3bc
movq $0x0, -0x40(%rbp)
cmpq $0x3, -0x40(%rbp)
jge 0x16c385
movq -0x40(%rbp), %rax
movzbl -0x4f(%rbp,%rax), %eax
andl $0x3f, %eax
movzbl %al, %edi
callq 0x161710
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, -0x52(%rbp,%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x16c352
jmp 0x16c3eb
movq $0x0, -0x40(%rbp)
cmpq $0x3, -0x40(%rbp)
jge 0x16c3ba
movq -0x40(%rbp), %rax
movzbl -0x4f(%rbp,%rax), %eax
imull $0x11, %eax, %eax
movb %al, %cl
movq -0x40(%rbp), %rax
movb %cl, -0x52(%rbp,%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x16c38f
jmp 0x16c3eb
movq $0x0, -0x40(%rbp)
cmpq $0x3, -0x40(%rbp)
jge 0x16c3e9
movq -0x40(%rbp), %rax
movb -0x4f(%rbp,%rax), %cl
movq -0x40(%rbp), %rax
movb %cl, -0x52(%rbp,%rax)
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x16c3c4
jmp 0x16c3eb
movzbl -0x52(%rbp), %eax
shll $0x10, %eax
movzbl -0x51(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x50(%rbp), %ecx
orl %ecx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, -0xa4(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0xb4(%rbp)
subl $0x2, %eax
je 0x16c42c
jmp 0x16c41c
movl -0xb4(%rbp), %eax
subl $0x3, %eax
je 0x16c478
jmp 0x16c4b9
leaq -0xa0(%rbp), %rdi
movzbl -0x4f(%rbp), %ecx
movzbl -0x4e(%rbp), %r8d
movzbl -0x4d(%rbp), %r9d
movl $0x40, %esi
leaq 0x68d32(%rip), %rdx     # 0x1d517f
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0xa4(%rbp), %edx
leaq -0xa0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
jmp 0x16c4cc
leaq -0xa0(%rbp), %rdi
movl -0x4c(%rbp), %ecx
movl $0x40, %esi
leaq 0x5f6bb(%rip), %rdx     # 0x1cbb49
movb $0x0, %al
callq 0x15a980
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0xa4(%rbp), %edx
leaq -0xa0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %r8
callq 0x15bdb0
jmp 0x16c4cc
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl -0xa4(%rbp), %edx
callq 0x15bf00
movq -0x38(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0x16c4e7
movl -0xa4(%rbp), %edx
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
jmp 0x16c4e9
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x16c203
jmp 0x16c4fc
movl 0x20(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x16c515
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0xc0, %rsp
popq %rbp
retq
nop