// File: /workspace/github_c_makefile_star2_O2/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, %r13d
movl %r8d, %ebp
movl %ecx, %r15d
movl %edx, 0xc(%rsp)
movq %rsi, 0x20(%rsp)
movslq %edi, %rbx
leaq 0x359431(%rip), %r14    # 0x391ae0
movq (%r14,%rbx,8), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x386e0
movswq 0x3d5163(%rip), %rdi    # 0x40d828
shlq $0x8, %rdi
callq 0x69a0
movq %rax, (%r14,%rbx,8)
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x387f9
xorl %eax, %eax
testb %r13b, %r13b
je 0x38869
movl %r15d, %ecx
orb 0xc(%rsp), %cl
orb %bpl, %cl
movsbl %cl, %ecx
cmpl $0x3f, %ecx
ja 0x38869
testl %ecx, %ecx
je 0x3880c
movq %rbx, 0x28(%rsp)
xorl %edx, %edx
leaq 0x23ce99(%rip), %r14    # 0x2755b0
movl %r15d, %ecx
movsbq 0xc(%rsp), %r15
movl %ebp, %eax
movl %ecx, 0x18(%rsp)
movsbq %cl, %rbp
movl %eax, 0x1c(%rsp)
movsbq %al, %r13
movswq 0x3d50ee(%rip), %rcx    # 0x40d828
cmpq %rcx, %rdx
jge 0x387d4
movq %rdx, 0x30(%rsp)
movq %rdx, %rax
shlq $0x10, %rax
xorl %edx, %edx
idivq %rcx
movslq %eax, %r12
xorl %ebx, %ebx
cmpq $0x100, %rbx            # imm = 0x100
je 0x387c2
movq 0x20(%rsp), %rax
movzbl (%rax,%rbx), %eax
leaq (%rax,%rax,2), %rax
movzbl (%r14,%rax), %ecx
movq %r15, %rdi
subq %rcx, %rdi
imulq %r12, %rdi
shrq $0x10, %rdi
addl %ecx, %edi
movzbl 0x1(%r14,%rax), %ecx
movq %rbp, %rsi
subq %rcx, %rsi
imulq %r12, %rsi
shrq $0x10, %rsi
addl %ecx, %esi
movzbl 0x2(%r14,%rax), %eax
movq %r13, %rdx
subq %rax, %rdx
imulq %r12, %rdx
shrq $0x10, %rdx
addl %eax, %edx
callq 0x221a2
movq 0x10(%rsp), %rcx
movb %al, (%rcx,%rbx)
incq %rbx
jmp 0x38759
movq 0x30(%rsp), %rdx
incq %rdx
addq %rbx, 0x10(%rsp)
jmp 0x38732
leaq 0x3f6dd5(%rip), %rax    # 0x42f5b0
movq 0x28(%rsp), %rcx
movl 0xc(%rsp), %edx
movb %dl, (%rax,%rcx,4)
movl 0x18(%rsp), %edx
movb %dl, 0x1(%rax,%rcx,4)
movl 0x1c(%rsp), %edx
movb %dl, 0x2(%rax,%rcx,4)
jmp 0x38867
leaq 0x33bad(%rip), %rax     # 0x6c3ad
movq %rax, 0x179219(%rip)    # 0x1b1a20
pushq $0x1
popq %rax
jmp 0x38869
xorl %eax, %eax
cmpq $0x100, %rax            # imm = 0x100
je 0x38856
movq 0x20(%rsp), %rcx
movzbl (%rcx,%rax), %ecx
addq 0x3592ba(%rip), %rcx    # 0x391ae0
movq (%r14,%rbx,8), %rdx
addq %rax, %rdx
xorl %esi, %esi
xorl %edi, %edi
movswl 0x3d4fef(%rip), %r8d    # 0x40d828
cmpl %r8d, %edi
jge 0x38851
movb (%rcx,%rsi), %r8b
movb %r8b, (%rdx,%rsi)
incl %edi
addq $0x100, %rsi            # imm = 0x100
jmp 0x38831
incq %rax
jmp 0x3880e
leaq 0x3f6d53(%rip), %rax    # 0x42f5b0
andw $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