// File: /workspace/github_c_makefile_star2_O3/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: makepalookup
int makepalookup(int palnum, unsigned char *remapbuf, signed char r, signed char g, signed char b, unsigned char dastat)
{
	int i, j, palscale;
	unsigned char *ptr, *ptr2;

	if (palookup[palnum] == NULL)
	{
			//Allocate palookup buffer
		if ((palookup[palnum] = kmalloc(numpalookups<<8)) == NULL) {
			engineerrstr = "Failed to allocate palette lookup memory";
			return 1;
		}
	}

	if (dastat == 0) return 0;
	if ((r|g|b|63) != 63) return 0;

	if ((r|g|b) == 0)
	{
		for(i=0;i<256;i++)
		{
			ptr = (unsigned char *)((intptr_t)palookup[0]+remapbuf[i]);
			ptr2 = (unsigned char *)((intptr_t)palookup[palnum]+i);
			for(j=0;j<numpalookups;j++)
				{ *ptr2 = *ptr; ptr += 256; ptr2 += 256; }
		}
#if USE_POLYMOST && USE_OPENGL
		palookupfog[palnum].r = 0;
		palookupfog[palnum].g = 0;
		palookupfog[palnum].b = 0;
#endif
	}
	else
	{
		ptr2 = palookup[palnum];
		for(i=0;i<numpalookups;i++)
		{
			palscale = divscale16(i,numpalookups);
			for(j=0;j<256;j++)
			{
				ptr = &palette[remapbuf[j]*3];
				*ptr2++ = getclosestcol((int)ptr[0]+mulscale16(r-ptr[0],palscale),
							(int)ptr[1]+mulscale16(g-ptr[1],palscale),
							(int)ptr[2]+mulscale16(b-ptr[2],palscale));
			}
		}
#if USE_POLYMOST && USE_OPENGL
		palookupfog[palnum].r = r;
		palookupfog[palnum].g = g;
		palookupfog[palnum].b = b;
#endif
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, %ebp
movl %r8d, %r12d
movl %ecx, %r13d
movq %rsi, %r14
movslq %edi, %rbx
leaq 0x35c994(%rip), %r9     # 0x398ae0
movq (%r9,%rbx,8), %rdi
testq %rdi, %rdi
jne 0x3c183
movswq 0x3d86cb(%rip), %rdi    # 0x414828
shlq $0x8, %rdi
movl %edx, %r15d
callq 0x69a0
leaq 0x35c970(%rip), %r9     # 0x398ae0
movl %r15d, %edx
movq %rax, %rdi
movq %rax, (%r9,%rbx,8)
testq %rax, %rax
je 0x3c2b1
xorl %eax, %eax
testb %bpl, %bpl
je 0x3c320
movl %r13d, %ecx
orb %dl, %cl
orb %r12b, %cl
movsbl %cl, %esi
cmpl $0x3f, %esi
ja 0x3c320
movzwl 0x3d867f(%rip), %ecx    # 0x414828
testl %esi, %esi
je 0x3c2c6
movl %edx, 0xc(%rsp)
movl %r13d, 0x10(%rsp)
movl %r12d, 0x14(%rsp)
movq %r14, 0x28(%rsp)
movq %rbx, 0x18(%rsp)
testw %cx, %cx
jle 0x3c28c
xorl %eax, %eax
leaq 0x2403d5(%rip), %r12    # 0x27c5b0
movsbq 0xc(%rsp), %r15
movsbq 0x10(%rsp), %rbp
movsbq 0x14(%rsp), %r14
movq %rax, 0x20(%rsp)
shlq $0x10, %rax
movswq %cx, %rcx
xorl %edx, %edx
idivq %rcx
movslq %eax, %r13
xorl %ebx, %ebx
movq %rdi, 0x30(%rsp)
movq 0x28(%rsp), %rax
movzbl (%rax,%rbx), %eax
leaq (%rax,%rax,2), %rax
movzbl (%r12,%rax), %ecx
movq %r15, %rdi
subq %rcx, %rdi
imulq %r13, %rdi
shrq $0x10, %rdi
addl %ecx, %edi
movzbl 0x1(%r12,%rax), %ecx
movq %rbp, %rsi
subq %rcx, %rsi
imulq %r13, %rsi
shrq $0x10, %rsi
addl %ecx, %esi
movzbl 0x2(%r12,%rax), %eax
movq %r14, %rdx
subq %rax, %rdx
imulq %r13, %rdx
shrq $0x10, %rdx
addl %eax, %edx
callq 0x24f9e
movq 0x30(%rsp), %rdi
movb %al, (%rdi,%rbx)
incq %rbx
cmpq $0x100, %rbx            # imm = 0x100
jne 0x3c209
movq 0x20(%rsp), %rax
incq %rax
movswq 0x3d85a8(%rip), %rcx    # 0x414828
addq %rbx, %rdi
cmpq %rcx, %rax
jl 0x3c1ed
leaq 0x3fa31d(%rip), %rax    # 0x4365b0
movq 0x18(%rsp), %rcx
movl 0xc(%rsp), %edx
movb %dl, (%rax,%rcx,4)
movl 0x10(%rsp), %edx
movb %dl, 0x1(%rax,%rcx,4)
movl 0x14(%rsp), %edx
movb %dl, 0x2(%rax,%rcx,4)
jmp 0x3c31e
leaq 0x3b235(%rip), %rax     # 0x774ed
movq %rax, 0x17c761(%rip)    # 0x1b8a20
movl $0x1, %eax
jmp 0x3c320
xorl %eax, %eax
testw %cx, %cx
jle 0x3c301
movq (%r9,%rbx,8), %rdx
addq %rax, %rdx
movzbl (%r14,%rax), %esi
addq 0x35c800(%rip), %rsi    # 0x398ae0
xorl %edi, %edi
xorl %r8d, %r8d
movb (%rsi,%rdi), %cl
movb %cl, (%rdx,%rdi)
incl %r8d
movswl 0x3d8533(%rip), %ecx    # 0x414828
addq $0x100, %rdi            # imm = 0x100
cmpl %ecx, %r8d
jl 0x3c2e5
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x3c2c8
leaq 0x3fa29d(%rip), %rax    # 0x4365b0
movw $0x0, (%rax,%rbx,4)
movb $0x0, 0x2(%rax,%rbx,4)
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq