// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movl %r8d, -0x44(%rbp)
movl %ecx, %eax
movl -0x44(%rbp), %ecx
movl %eax, -0x40(%rbp)
movl %edx, %eax
movl -0x40(%rbp), %edx
movl %eax, -0x3c(%rbp)
movq %rsi, %rax
movl -0x3c(%rbp), %esi
movq %rax, -0x38(%rbp)
movl %edi, %r8d
movq -0x38(%rbp), %rdi
movb %r9b, %al
movl %r8d, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movb %sil, -0x11(%rbp)
movb %dl, -0x12(%rbp)
movb %cl, -0x13(%rbp)
movb %al, -0x14(%rbp)
movslq -0x8(%rbp), %rcx
leaq 0x37df90(%rip), %rax    # 0x3d5640
cmpq $0x0, (%rax,%rcx,8)
jne 0x576fa
movswl 0x3f7eca(%rip), %eax    # 0x44f588
shll $0x8, %eax
movslq %eax, %rdi
callq 0x35ef0
movslq -0x8(%rbp), %rdx
leaq 0x37df6c(%rip), %rcx    # 0x3d5640
movq %rax, (%rcx,%rdx,8)
cmpq $0x0, %rax
jne 0x576f8
leaq 0x5bc9b(%rip), %rax     # 0xb3380
movq %rax, 0x19de94(%rip)    # 0x1f5580
movl $0x1, -0x4(%rbp)
jmp 0x57984
jmp 0x576fa
movzbl -0x14(%rbp), %eax
cmpl $0x0, %eax
jne 0x5770f
movl $0x0, -0x4(%rbp)
jmp 0x57984
movsbl -0x11(%rbp), %eax
movsbl -0x12(%rbp), %ecx
orl %ecx, %eax
movsbl -0x13(%rbp), %ecx
orl %ecx, %eax
orl $0x3f, %eax
cmpl $0x3f, %eax
je 0x57733
movl $0x0, -0x4(%rbp)
jmp 0x57984
movsbl -0x11(%rbp), %eax
movsbl -0x12(%rbp), %ecx
orl %ecx, %eax
movsbl -0x13(%rbp), %ecx
orl %ecx, %eax
cmpl $0x0, %eax
jne 0x57820
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x577ec
movq 0x37ded9(%rip), %rax    # 0x3d5640
movq -0x10(%rbp), %rcx
movslq -0x18(%rbp), %rdx
movzbl (%rcx,%rdx), %ecx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movslq -0x8(%rbp), %rcx
leaq 0x37debb(%rip), %rax    # 0x3d5640
movq (%rax,%rcx,8), %rax
movslq -0x18(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movswl 0x3f7de3(%rip), %ecx    # 0x44f588
cmpl %ecx, %eax
jge 0x577dc
movq -0x28(%rbp), %rax
movb (%rax), %cl
movq -0x30(%rbp), %rax
movb %cl, (%rax)
movq -0x28(%rbp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $0x100, %rax            # imm = 0x100
movq %rax, -0x30(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x5779b
jmp 0x577de
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x57753
movslq -0x8(%rbp), %rcx
leaq 0x419609(%rip), %rax    # 0x470e00
movb $0x0, (%rax,%rcx,4)
movslq -0x8(%rbp), %rcx
leaq 0x4195fa(%rip), %rax    # 0x470e00
movb $0x0, 0x1(%rax,%rcx,4)
movslq -0x8(%rbp), %rcx
leaq 0x4195ea(%rip), %rax    # 0x470e00
movb $0x0, 0x2(%rax,%rcx,4)
jmp 0x5797d
movslq -0x8(%rbp), %rcx
leaq 0x37de15(%rip), %rax    # 0x3d5640
movq (%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
movswl 0x3f7d44(%rip), %ecx    # 0x44f588
cmpl %ecx, %eax
jge 0x57948
movl -0x18(%rbp), %edi
movswl 0x3f7d32(%rip), %esi    # 0x44f588
callq 0x4c7c0
movl %eax, -0x20(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x100, -0x1c(%rbp)     # imm = 0x100
jge 0x57938
movq -0x10(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
imull $0x3, %eax, %eax
movslq %eax, %rcx
leaq 0x261885(%rip), %rax    # 0x2b9110
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x58(%rbp)
movsbl -0x11(%rbp), %edi
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
subl %eax, %edi
movl -0x20(%rbp), %esi
callq 0x39010
movl %eax, %ecx
movl -0x58(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x4c(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x1(%rax), %eax
movl %eax, -0x54(%rbp)
movsbl -0x12(%rbp), %edi
movq -0x28(%rbp), %rax
movzbl 0x1(%rax), %eax
subl %eax, %edi
movl -0x20(%rbp), %esi
callq 0x39010
movl %eax, %ecx
movl -0x54(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %eax
movl %eax, -0x50(%rbp)
movsbl -0x13(%rbp), %edi
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %eax
subl %eax, %edi
movl -0x20(%rbp), %esi
callq 0x39010
movl -0x50(%rbp), %edx
movl -0x4c(%rbp), %edi
movl -0x48(%rbp), %esi
addl %eax, %edx
callq 0x37750
movb %al, %cl
movq -0x30(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0x30(%rbp)
movb %cl, (%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x57865
jmp 0x5793a
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5783a
movb -0x11(%rbp), %dl
movslq -0x8(%rbp), %rcx
leaq 0x4194aa(%rip), %rax    # 0x470e00
movb %dl, (%rax,%rcx,4)
movb -0x12(%rbp), %dl
movslq -0x8(%rbp), %rcx
leaq 0x419499(%rip), %rax    # 0x470e00
movb %dl, 0x1(%rax,%rcx,4)
movb -0x13(%rbp), %dl
movslq -0x8(%rbp), %rcx
leaq 0x419487(%rip), %rax    # 0x470e00
movb %dl, 0x2(%rax,%rcx,4)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)