// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/src/fmtutil.c
// Function: decode_atari_image_paletted
static int decode_atari_image_paletted(deark *c, struct atari_img_decode_data *adata)
{
	i64 i, j;
	i64 plane;
	i64 rowspan;
	u8 b;
	u32 v;
	i64 planespan;
	i64 ncolors;

	planespan = 2*((adata->w+15)/16);
	rowspan = planespan*adata->bpp;
	if(adata->ncolors>0)
		ncolors = adata->ncolors;
	else
		ncolors = ((i64)1)<<adata->bpp;

	for(j=0; j<adata->h; j++) {
		for(i=0; i<adata->w; i++) {
			v = 0;

			for(plane=0; plane<adata->bpp; plane++) {
				if(adata->was_compressed==0) {
					// TODO: Simplify this.
					if(adata->bpp==1) {
						b = de_get_bits_symbol(adata->unc_pixels, 1, j*rowspan, i);
					}
					else if(adata->bpp==2) {
						b = de_get_bits_symbol(adata->unc_pixels, 1,
							j*rowspan + 2*plane + (i/16)*2, i);
					}
					else if(adata->bpp==4) {
						b = de_get_bits_symbol(adata->unc_pixels, 1,
							j*rowspan + 2*plane + (i/2-(i/2)%16)+8*((i%32)/16), i%16);
					}
					else if(adata->bpp==8) {
						b = de_get_bits_symbol(adata->unc_pixels, 1,
							j*rowspan + 2*plane + (i-i%16), i%16);
					}
					else {
						b = 0;
					}
				}
				else {
					b = de_get_bits_symbol(adata->unc_pixels, 1, j*rowspan + plane*planespan, i);
				}
				if(b) v |= 1<<plane;
			}

			if(adata->is_spectrum512) {
				v = spectrum512_FindIndex(i, v);
				if(j>0) {
					v += (unsigned int)(48*(j));
				}
			}
			if(v>=(unsigned int)ncolors) v=(unsigned int)(ncolors-1);

			de_bitmap_setpixel_rgb(adata->img, i, j, adata->pal[v]);
		}
	}
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
addq $0xf, %rax
movl $0x10, %ecx
cqto
idivq %rcx
shlq %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
imulq (%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jle 0x174ac9
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
jmp 0x174adc
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jge 0x174dd8
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jge 0x174dc5
movl $0x0, -0x38(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jge 0x174d4e
movq -0x10(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x174cf4
movq -0x10(%rbp), %rax
cmpq $0x1, (%rax)
jne 0x174b6f
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rdx
imulq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x1, %esi
callq 0x16b8d0
movb %al, -0x31(%rbp)
jmp 0x174cf2
movq -0x10(%rbp), %rax
cmpq $0x2, (%rax)
jne 0x174bc6
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rax
imulq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq -0x50(%rbp), %rdx
shlq %rax
addq %rax, %rdx
movq -0x18(%rbp), %rcx
movl $0x1, %esi
callq 0x16b8d0
movb %al, -0x31(%rbp)
jmp 0x174cf0
movq -0x10(%rbp), %rax
cmpq $0x4, (%rax)
jne 0x174c7b
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rax
imulq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x18(%rbp), %rax
movl $0x2, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x18(%rbp), %rax
movl $0x2, %esi
cqto
idivq %rsi
movl $0x10, %esi
cqto
idivq %rsi
movq -0x68(%rbp), %rax
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movl $0x20, %ecx
cqto
idivq %rcx
movq %rdx, %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x60(%rbp), %rax
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rdx, %rcx
movq -0x58(%rbp), %rdx
movl $0x1, %esi
callq 0x16b8d0
movb %al, -0x31(%rbp)
jmp 0x174cee
movq -0x10(%rbp), %rax
cmpq $0x8, (%rax)
jne 0x174ce8
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rax
imulq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rcx
movq -0x18(%rbp), %rax
movl $0x10, %esi
cqto
idivq %rsi
movq -0x78(%rbp), %rax
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x70(%rbp)
movq -0x18(%rbp), %rax
movl $0x10, %ecx
cqto
idivq %rcx
movq %rdx, %rcx
movq -0x70(%rbp), %rdx
movl $0x1, %esi
callq 0x16b8d0
movb %al, -0x31(%rbp)
jmp 0x174cec
movb $0x0, -0x31(%rbp)
jmp 0x174cee
jmp 0x174cf0
jmp 0x174cf2
jmp 0x174d22
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x20(%rbp), %rdx
imulq -0x30(%rbp), %rdx
movq -0x28(%rbp), %rax
imulq -0x40(%rbp), %rax
addq %rax, %rdx
movq -0x18(%rbp), %rcx
movl $0x1, %esi
callq 0x16b8d0
movb %al, -0x31(%rbp)
cmpb $0x0, -0x31(%rbp)
je 0x174d3b
movq -0x28(%rbp), %rax
movl %eax, %ecx
movl $0x1, %eax
shll %cl, %eax
orl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x174d3d
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x174b1f
movq -0x10(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x174d7b
movq -0x18(%rbp), %rdi
movl -0x38(%rbp), %esi
callq 0x1772d0
movl %eax, -0x38(%rbp)
cmpq $0x0, -0x20(%rbp)
jle 0x174d79
imulq $0x30, -0x20(%rbp), %rax
addl -0x38(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x174d7b
movl -0x38(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl %ecx, %eax
jb 0x174d91
movq -0x48(%rbp), %rax
subq $0x1, %rax
movl %eax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x38(%rbp), %ecx
movl (%rax,%rcx,4), %ecx
callq 0x16b310
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
jmp 0x174afe
jmp 0x174dc7
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x174ae4
movl $0x1, %eax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)