// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movq %rsi, %r15
movslq %edi, %rbx
leaq 0x35cc61(%rip), %r9     # 0x397ae0
cmpq $0x0, (%r9,%rbx,8)
jne 0x3aeb1
movswq 0x3d899a(%rip), %rdi    # 0x413828
shlq $0x8, %rdi
movl %edx, %r14d
callq 0x69a0
leaq 0x35cc3f(%rip), %r9     # 0x397ae0
movl %r14d, %edx
movq %rax, (%r9,%rbx,8)
testq %rax, %rax
je 0x3afe5
xorl %eax, %eax
testb %r13b, %r13b
je 0x3b05a
movl %r12d, %ecx
orb %dl, %cl
orb %bpl, %cl
movsbl %cl, %ecx
cmpl $0x3f, %ecx
ja 0x3b05a
testl %ecx, %ecx
je 0x3affa
movl %edx, 0xc(%rsp)
movl %r12d, 0x10(%rsp)
movl %ebp, 0x14(%rsp)
movq %r15, 0x28(%rsp)
movq %rbx, 0x18(%rsp)
movzwl 0x3d8932(%rip), %ecx    # 0x413828
testw %cx, %cx
jle 0x3afc0
movq 0x18(%rsp), %rax
movq (%r9,%rax,8), %rdi
xorl %eax, %eax
leaq 0x24069f(%rip), %rbp    # 0x27b5b0
movsbq 0xc(%rsp), %r14
movsbq 0x10(%rsp), %r13
movsbq 0x14(%rsp), %r15
movq %rax, 0x20(%rsp)
shlq $0x10, %rax
movswq %cx, %rcx
xorl %edx, %edx
idivq %rcx
movslq %eax, %r12
xorl %ebx, %ebx
movq %rdi, 0x30(%rsp)
movq 0x28(%rsp), %rax
movzbl (%rax,%rbx), %eax
leaq (%rax,%rax,2), %rax
movzbl (%rbp,%rax), %ecx
movq %r14, %rdi
subq %rcx, %rdi
imulq %r12, %rdi
shrq $0x10, %rdi
addl %ecx, %edi
movzbl 0x1(%rbp,%rax), %ecx
movq %r13, %rsi
subq %rcx, %rsi
imulq %r12, %rsi
shrq $0x10, %rsi
addl %ecx, %esi
movzbl 0x2(%rbp,%rax), %eax
movq %r15, %rdx
subq %rax, %rdx
imulq %r12, %rdx
shrq $0x10, %rdx
addl %eax, %edx
callq 0x24a31
movq 0x30(%rsp), %rdi
movb %al, (%rdi,%rbx)
incq %rbx
cmpq $0x100, %rbx            # imm = 0x100
jne 0x3af3f
movq 0x20(%rsp), %rax
incq %rax
movswq 0x3d8874(%rip), %rcx    # 0x413828
addq %rbx, %rdi
cmpq %rcx, %rax
jl 0x3af23
leaq 0x3fa5e9(%rip), %rax    # 0x4355b0
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 0x3b058
leaq 0x3b381(%rip), %rax     # 0x7636d
movq %rax, 0x17ca2d(%rip)    # 0x1b7a20
movl $0x1, %eax
jmp 0x3b05a
xorl %eax, %eax
cmpw $0x0, 0x3d8824(%rip)    # 0x413828
jle 0x3b03b
movq (%r9,%rbx,8), %rcx
addq %rax, %rcx
movzbl (%r15,%rax), %edx
addq 0x35cac7(%rip), %rdx    # 0x397ae0
xorl %esi, %esi
xorl %edi, %edi
movb (%rdx,%rsi), %r8b
movb %r8b, (%rcx,%rsi)
incl %edi
movswl 0x3d87f9(%rip), %r8d    # 0x413828
addq $0x100, %rsi            # imm = 0x100
cmpl %r8d, %edi
jl 0x3b01d
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x3affc
leaq 0x3fa563(%rip), %rax    # 0x4355b0
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