// File: /workspace/github_c_makefile_star2_O0/jonof[P]jftekwar/libsmacker/smacker.c
// Function: smk_render_palette
static char smk_render_palette(struct smk_video_t * s, unsigned char * p, unsigned long size)
{
	/* Index into palette */
	unsigned short i = 0;
	/* Helper variables */
	unsigned short count, src;
	static unsigned char oldPalette[256][3];
	/* Smacker palette map: smk colors are 6-bit, this table expands them to 8. */
	const unsigned char palmap[64] = {
		0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C,
		0x20, 0x24, 0x28, 0x2C, 0x30, 0x34, 0x38, 0x3C,
		0x41, 0x45, 0x49, 0x4D, 0x51, 0x55, 0x59, 0x5D,
		0x61, 0x65, 0x69, 0x6D, 0x71, 0x75, 0x79, 0x7D,
		0x82, 0x86, 0x8A, 0x8E, 0x92, 0x96, 0x9A, 0x9E,
		0xA2, 0xA6, 0xAA, 0xAE, 0xB2, 0xB6, 0xBA, 0xBE,
		0xC3, 0xC7, 0xCB, 0xCF, 0xD3, 0xD7, 0xDB, 0xDF,
		0xE3, 0xE7, 0xEB, 0xEF, 0xF3, 0xF7, 0xFB, 0xFF
	};
	/* null check */
	assert(s);
	assert(p);
	/* Copy palette to old palette */
	memcpy(oldPalette, s->palette, 256 * 3);

	/* Loop until palette is complete, or we are out of bytes to process */
	while ((i < 256) && (size > 0)) {
		if ((*p) & 0x80) {
			/* 0x80: Skip block
				(preserve C+1 palette entries from previous palette) */
			count = ((*p) & 0x7F) + 1;
			p ++;
			size --;

			/* check for overflow condition */
			if (i + count > 256) {
				fprintf(stderr, "libsmacker::palette_render(s,p,size) - ERROR: overflow, 0x80 attempt to skip %d entries from %d\n", count, i);
				goto error;
			}

			/* finally: advance the index. */
			i += count;
		} else if ((*p) & 0x40) {
			/* 0x40: Color-shift block
				Copy (c + 1) color entries of the previous palette,
				starting from entry (s),
				to the next entries of the new palette. */
			if (size < 2) {
				fputs("libsmacker::palette_render(s,p,size) - ERROR: 0x40 ran out of bytes for copy\n", stderr);
				goto error;
			}

			/* pick "count" items to copy */
			count = ((*p) & 0x3F) + 1;
			p ++;
			size --;
			/* start offset of old palette */
			src = *p;
			p ++;
			size --;

			/* overflow: see if we write/read beyond 256colors, or overwrite own palette */
			if (i + count > 256 || src + count > 256 ||
				(src < i && src + count > i)) {
				fprintf(stderr, "libsmacker::palette_render(s,p,size) - ERROR: overflow, 0x40 attempt to copy %d entries from %d to %d\n", count, src, i);
				goto error;
			}

			/* OK!  Copy the color-palette entries. */
			memmove(&s->palette[i][0], &oldPalette[src][0], count * 3);
			i += count;
		} else {
			/* 0x00: Set Color block
				Direct-set the next 3 bytes for palette index */
			if (size < 3) {
				fprintf(stderr, "libsmacker::palette_render - ERROR: 0x3F ran out of bytes for copy, size=%lu\n", size);
				goto error;
			}

			for (count = 0; count < 3; count ++) {
				if (*p > 0x3F) {
					fprintf(stderr, "libsmacker::palette_render - ERROR: palette index exceeds 0x3F (entry [%u][%u])\n", i, count);
					goto error;
				}

				s->palette[i][count] = palmap[*p];
				p++;
				size --;
			}

			i ++;
		}
	}

	if (i < 256) {
		fprintf(stderr, "libsmacker::palette_render - ERROR: did not completely fill palette (idx=%u)\n", i);
		goto error;
	}

	return 0;
error:
	/* Error, return -1
		The new palette probably has errors but is preferrable to a black screen */
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movw $0x0, -0x22(%rbp)
leaq -0x70(%rbp), %rdi
leaq 0xa4ad8(%rip), %rsi     # 0xecff0
movl $0x40, %edx
callq 0x80e0
cmpq $0x0, -0x10(%rbp)
je 0x4852b
jmp 0x4854a
leaq 0xa721d(%rip), %rdi     # 0xef74f
leaq 0xa4d8a(%rip), %rsi     # 0xed2c3
movl $0x4a2, %edx            # imm = 0x4A2
leaq 0xa6972(%rip), %rcx     # 0xeeeb7
callq 0x8230
cmpq $0x0, -0x18(%rbp)
je 0x48553
jmp 0x48572
leaq 0x9154f(%rip), %rdi     # 0xd9aa9
leaq 0xa4d62(%rip), %rsi     # 0xed2c3
movl $0x4a3, %edx            # imm = 0x4A3
leaq 0xa694a(%rip), %rcx     # 0xeeeb7
callq 0x8230
movq -0x10(%rbp), %rsi
addq $0xa0, %rsi
leaq 0x32f59c(%rip), %rdi    # 0x377b20
movl $0x300, %edx            # imm = 0x300
callq 0x80e0
movzwl -0x22(%rbp), %ecx
xorl %eax, %eax
cmpl $0x100, %ecx            # imm = 0x100
movb %al, -0x71(%rbp)
jge 0x485aa
cmpq $0x0, -0x20(%rbp)
seta %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x485b6
jmp 0x4883d
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x48639
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x7f, %eax
addl $0x1, %eax
movw %ax, -0x24(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
movzwl -0x22(%rbp), %eax
movzwl -0x24(%rbp), %ecx
addl %ecx, %eax
cmpl $0x100, %eax            # imm = 0x100
jle 0x48626
movq 0xd09d0(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movzwl -0x24(%rbp), %edx
movzwl -0x22(%rbp), %ecx
leaq 0xa68eb(%rip), %rsi     # 0xeef05
movb $0x0, %al
callq 0x8280
jmp 0x4886c
movzwl -0x24(%rbp), %ecx
movzwl -0x22(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x22(%rbp)
jmp 0x48838
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x4876e
cmpq $0x2, -0x20(%rbp)
jae 0x4866e
movq 0xd097e(%rip), %rax     # 0x118fd8
movq (%rax), %rsi
leaq 0xa6902(%rip), %rdi     # 0xeef66
callq 0x8430
jmp 0x4886c
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
andl $0x3f, %eax
addl $0x1, %eax
movw %ax, -0x24(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movw %ax, -0x26(%rbp)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
movzwl -0x22(%rbp), %eax
movzwl -0x24(%rbp), %ecx
addl %ecx, %eax
cmpl $0x100, %eax            # imm = 0x100
jg 0x486fa
movzwl -0x26(%rbp), %eax
movzwl -0x24(%rbp), %ecx
addl %ecx, %eax
cmpl $0x100, %eax            # imm = 0x100
jg 0x486fa
movzwl -0x26(%rbp), %eax
movzwl -0x22(%rbp), %ecx
cmpl %ecx, %eax
jge 0x48724
movzwl -0x26(%rbp), %eax
movzwl -0x24(%rbp), %ecx
addl %ecx, %eax
movzwl -0x22(%rbp), %ecx
cmpl %ecx, %eax
jle 0x48724
movq 0xd08d7(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movzwl -0x24(%rbp), %edx
movzwl -0x26(%rbp), %ecx
movzwl -0x22(%rbp), %r8d
leaq 0xa689c(%rip), %rsi     # 0xeefb4
movb $0x0, %al
callq 0x8280
jmp 0x4886c
movq -0x10(%rbp), %rdi
addq $0xa0, %rdi
movzwl -0x22(%rbp), %eax
imulq $0x3, %rax, %rax
addq %rax, %rdi
movzwl -0x26(%rbp), %eax
leaq 0x32f3db(%rip), %rsi    # 0x377b20
imulq $0x3, %rax, %rax
addq %rax, %rsi
movzwl -0x24(%rbp), %eax
imull $0x3, %eax, %eax
movslq %eax, %rdx
callq 0x8210
movzwl -0x24(%rbp), %ecx
movzwl -0x22(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x22(%rbp)
jmp 0x48836
cmpq $0x3, -0x20(%rbp)
jae 0x48796
movq 0xd085c(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movq -0x20(%rbp), %rdx
leaq 0xa6891(%rip), %rsi     # 0xef01b
movb $0x0, %al
callq 0x8280
jmp 0x4886c
movw $0x0, -0x24(%rbp)
movzwl -0x24(%rbp), %eax
cmpl $0x3, %eax
jge 0x4882a
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x3f, %eax
jle 0x487da
movq 0xd081c(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movzwl -0x22(%rbp), %edx
movzwl -0x24(%rbp), %ecx
leaq 0xa689b(%rip), %rsi     # 0xef069
movb $0x0, %al
callq 0x8280
jmp 0x4886c
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movb -0x70(%rbp,%rax), %dl
movq -0x10(%rbp), %rax
addq $0xa0, %rax
movzwl -0x22(%rbp), %ecx
imulq $0x3, %rcx, %rcx
addq %rcx, %rax
movzwl -0x24(%rbp), %ecx
movb %dl, (%rax,%rcx)
movq -0x18(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
movq -0x20(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
movw -0x24(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x24(%rbp)
jmp 0x4879c
movw -0x22(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x22(%rbp)
jmp 0x48838
jmp 0x4858e
movzwl -0x22(%rbp), %eax
cmpl $0x100, %eax            # imm = 0x100
jge 0x48866
movq 0xd0789(%rip), %rax     # 0x118fd8
movq (%rax), %rdi
movzwl -0x22(%rbp), %edx
leaq 0xa685d(%rip), %rsi     # 0xef0ba
movb $0x0, %al
callq 0x8280
jmp 0x4886c
movb $0x0, -0x1(%rbp)
jmp 0x48870
movb $-0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax)