// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/wpg.c
// Function: get_final_palette
static void get_final_palette(deark *c, lctx *d, de_color *finalpal, i64 bpp)
{
	i64 k;
	u8 cr, cg, cb;
	int has_3plusbitpal = 0;
	int has_5plusbitpal = 0;
	int has_nonblack_color = 0;
	int fixpal2_flag = 0;
	int fixpal4_flag = 0;

	if(bpp==2 && !d->has_pal) {
		// I'm not sure what I'm supposed to do here. The first 4 colors of
		// the default palette do not really constitute a usable 4-color
		// palette.
		// The images of this type that I've seen look correct if I use a
		// particular CGA palette. So...
		de_warn(c, "4-color image with no palette. Using a CGA palette.");
		de_copy_std_palette(DE_PALID_CGA, 2, 0, finalpal, 4, 0);
		return;
	}

	for(k=0; k<256; k++) {
		finalpal[k] = d->pal[k];

		if(d->opt_fixpal && bpp==4 && k<16) {
			cr = DE_COLOR_R(d->pal[k]);
			cg = DE_COLOR_G(d->pal[k]);
			cb = DE_COLOR_B(d->pal[k]);

			if((cr&0x0f)!=0 || (cg&0x0f)!=0 || (cb&0x0f)!=0) {
				has_5plusbitpal = 1;
			}
			if((cr&0x3f)!=0 || (cg&0x3f)!=0 || (cb&0x3f)!=0) {
				has_3plusbitpal = 1;
			}

			if(cr || cg || cb) {
				has_nonblack_color = 1;
			}
		}
	}

	if(d->opt_fixpal && bpp==4 && !has_3plusbitpal && has_nonblack_color) {
		de_dbg(c, "Palette seems to have 2 bits of precision. Rescaling palette.");
		fixpal2_flag = 1;
	}
	else if(d->opt_fixpal && bpp==4 && !has_5plusbitpal && has_nonblack_color) {
		de_dbg(c, "Palette seems to have 4 bits of precision. Rescaling palette.");
		fixpal4_flag = 1;
	}

	if(fixpal2_flag) {
		for(k=0; k<16; k++) {
			cr = DE_COLOR_R(finalpal[k]);
			cg = DE_COLOR_G(finalpal[k]);
			cb = DE_COLOR_B(finalpal[k]);
			cr = 85*(cr>>6);
			cg = 85*(cg>>6);
			cb = 85*(cb>>6);
			finalpal[k] = DE_MAKE_RGB(cr, cg, cb);
		}
	}
	else if(fixpal4_flag) {
		for(k=0; k<16; k++) {
			cr = DE_COLOR_R(finalpal[k]);
			cg = DE_COLOR_G(finalpal[k]);
			cb = DE_COLOR_B(finalpal[k]);
			cr = 17*(cr>>4);
			cg = 17*(cg>>4);
			cb = 17*(cb>>4);
			finalpal[k] = DE_MAKE_RGB(cr, cg, cb);
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
cmpq $0x2, -0x20(%rbp)
jne 0x13b693
movq -0x10(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0x13b693
movq -0x8(%rbp), %rdi
leaq 0x91a9e(%rip), %rsi     # 0x1cd105
movb $0x0, %al
callq 0x15bc10
movq -0x18(%rbp), %rcx
movl $0xa, %edi
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %edx
movl $0x4, %r8d
xorl %r9d, %r9d
callq 0x161320
jmp 0x13b9a2
movq $0x0, -0x28(%rbp)
cmpq $0x100, -0x28(%rbp)     # imm = 0x100
jge 0x13b7b3
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x14(%rax,%rcx,4), %edx
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x13b7a0
cmpq $0x4, -0x20(%rbp)
jne 0x13b7a0
cmpq $0x10, -0x28(%rbp)
jge 0x13b7a0
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x14(%rax,%rcx,4), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x29(%rbp)
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x14(%rax,%rcx,4), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x2a(%rbp)
movq -0x10(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x14(%rax,%rcx,4), %eax
andl $0xff, %eax
movb %al, -0x2b(%rbp)
movzbl -0x29(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x13b74a
movzbl -0x2a(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x13b74a
movzbl -0x2b(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x13b751
movl $0x1, -0x34(%rbp)
movzbl -0x29(%rbp), %eax
andl $0x3f, %eax
cmpl $0x0, %eax
jne 0x13b775
movzbl -0x2a(%rbp), %eax
andl $0x3f, %eax
cmpl $0x0, %eax
jne 0x13b775
movzbl -0x2b(%rbp), %eax
andl $0x3f, %eax
cmpl $0x0, %eax
je 0x13b77c
movl $0x1, -0x30(%rbp)
movzbl -0x29(%rbp), %eax
cmpl $0x0, %eax
jne 0x13b797
movzbl -0x2a(%rbp), %eax
cmpl $0x0, %eax
jne 0x13b797
movzbl -0x2b(%rbp), %eax
cmpl $0x0, %eax
je 0x13b79e
movl $0x1, -0x38(%rbp)
jmp 0x13b7a0
jmp 0x13b7a2
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x13b69b
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x13b7eb
cmpq $0x4, -0x20(%rbp)
jne 0x13b7eb
cmpl $0x0, -0x30(%rbp)
jne 0x13b7eb
cmpl $0x0, -0x38(%rbp)
je 0x13b7eb
movq -0x8(%rbp), %rdi
leaq 0x9195e(%rip), %rsi     # 0x1cd139
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x3c(%rbp)
jmp 0x13b823
movq -0x10(%rbp), %rax
cmpl $0x0, 0xc(%rax)
je 0x13b821
cmpq $0x4, -0x20(%rbp)
jne 0x13b821
cmpl $0x0, -0x34(%rbp)
jne 0x13b821
cmpl $0x0, -0x38(%rbp)
je 0x13b821
movq -0x8(%rbp), %rdi
leaq 0x78fb0(%rip), %rsi     # 0x1b47c3
movb $0x0, %al
callq 0x15b0d0
movl $0x1, -0x40(%rbp)
jmp 0x13b823
cmpl $0x0, -0x3c(%rbp)
je 0x13b8e3
movq $0x0, -0x28(%rbp)
cmpq $0x10, -0x28(%rbp)
jge 0x13b8de
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x29(%rbp)
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x2a(%rbp)
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xff, %eax
movb %al, -0x2b(%rbp)
movzbl -0x29(%rbp), %eax
sarl $0x6, %eax
imull $0x55, %eax, %eax
movb %al, -0x29(%rbp)
movzbl -0x2a(%rbp), %eax
sarl $0x6, %eax
imull $0x55, %eax, %eax
movb %al, -0x2a(%rbp)
movzbl -0x2b(%rbp), %eax
sarl $0x6, %eax
imull $0x55, %eax, %eax
movb %al, -0x2b(%rbp)
movzbl -0x29(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0x2a(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x2b(%rbp), %eax
orl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x13b835
jmp 0x13b9a2
cmpl $0x0, -0x40(%rbp)
je 0x13b9a0
movq $0x0, -0x28(%rbp)
cmpq $0x10, -0x28(%rbp)
jge 0x13b99e
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, -0x29(%rbp)
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, -0x2a(%rbp)
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
andl $0xff, %eax
movb %al, -0x2b(%rbp)
movzbl -0x29(%rbp), %eax
sarl $0x4, %eax
imull $0x11, %eax, %eax
movb %al, -0x29(%rbp)
movzbl -0x2a(%rbp), %eax
sarl $0x4, %eax
imull $0x11, %eax, %eax
movb %al, -0x2a(%rbp)
movzbl -0x2b(%rbp), %eax
sarl $0x4, %eax
imull $0x11, %eax, %eax
movb %al, -0x2b(%rbp)
movzbl -0x29(%rbp), %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movzbl -0x2a(%rbp), %eax
shll $0x8, %eax
orl %eax, %edx
movzbl -0x2b(%rbp), %eax
orl %eax, %edx
movq -0x18(%rbp), %rax
movq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x13b8f5
jmp 0x13b9a0
jmp 0x13b9a2
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)