// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/engine.c
// Function: setbrightness
void setbrightness(int dabrightness, unsigned char *dapal, char noapply)
{
	int i, k, j;

	if (!(noapply&4))
		curbrightness = min(max((int)dabrightness,0),15);

	setgamma(1.f + ((float)curbrightness / 10.f));
	ousegammabrightness = usegammabrightness;

	if (usegammabrightness) j = 0; else j = curbrightness;
	for(k=i=0;i<256;i++)
	{
		if (dapal) {
			// save palette without any brightness adjustment
			curpalette[i].r = dapal[i*3+0] << 2;
			curpalette[i].g = dapal[i*3+1] << 2;
			curpalette[i].b = dapal[i*3+2] << 2;
			curpalette[i].f = 0;
		}

		// brightness adjust the palette
		curpalettefaded[i].b = (tempbuf[k++] = britable[j][ curpalette[i].b ]);
		curpalettefaded[i].g = (tempbuf[k++] = britable[j][ curpalette[i].g ]);
		curpalettefaded[i].r = (tempbuf[k++] = britable[j][ curpalette[i].r ]);
		curpalettefaded[i].f = tempbuf[k++] = 0;
	}

	if ((noapply&1) == 0) setpalette(0,256,(unsigned char*)tempbuf);

#if USE_POLYMOST && USE_OPENGL
	if (rendmode == 3) {
		static unsigned int lastpalettesum = 0;
		unsigned int newpalettesum = crc32once((unsigned char *)curpalettefaded, sizeof(curpalettefaded));

		// only reset the textures if the preserve flag (bit 1 of noapply) is clear and
		// the new palette is different to the last
		if (!(noapply&2) && (newpalettesum != lastpalettesum))
			polymost_texinvalidateall();

		lastpalettesum = newpalettesum;
	}
#endif

	palfadergb.r = palfadergb.g = palfadergb.b = 0;
	palfadedelta = 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movl %edx, 0x4(%rsp)
testb $0x4, %dl
jne 0x312a8
xorl %ecx, %ecx
testl %edi, %edi
cmovgl %edi, %ecx
cmpl $0xf, %ecx
pushq $0xf
popq %rax
cmovll %ecx, %eax
movl %eax, 0x18075e(%rip)    # 0x1b1a04
jmp 0x312ae
movl 0x180756(%rip), %eax    # 0x1b1a04
cvtsi2ss %eax, %xmm0
divss 0x3ad16(%rip), %xmm0    # 0x6bfd0
addss 0x3ad12(%rip), %xmm0    # 0x6bfd4
movss %xmm0, (%rsp)
movss 0x3ad05(%rip), %xmm0    # 0x6bfd4
leaq 0x145e12(%rip), %r15    # 0x1770e8
leaq 0x3b33b(%rip), %r12     # 0x6c618
leaq 0x3ac4c(%rip), %r13     # 0x6bf30
leaq 0x3b358(%rip), %rbx     # 0x6c643
xorl %ebp, %ebp
movl 0x6c499(%rip), %eax     # 0x9d78c
cmpl $0x1, %eax
je 0x3132a
testl %eax, %eax
je 0x31360
movl $0x2, 0x6c486(%rip)     # 0x9d78c
movss (%rsp), %xmm1
callq 0x63a04
testl %eax, %eax
je 0x3136d
movl %eax, %ebp
xorl %eax, %eax
cmpl $0x0, (%r15)
sete %al
movl %eax, 0x6c467(%rip)     # 0x9d78c
movq %rbx, %rdi
jmp 0x31342
cmpl $0x0, (%r15)
je 0x3135b
xorl %eax, %eax
testl %ebp, %ebp
sete %al
addl %eax, %eax
movl %eax, 0x6c44d(%rip)     # 0x9d78c
movq %r12, %rdi
movslq (%r13,%rax,4), %rsi
addq %r13, %rsi
xorl %eax, %eax
callq 0x62903
movss 0x3ac7b(%rip), %xmm0    # 0x6bfd4
jmp 0x312ed
movss (%rsp), %xmm0
movss 0x39fe8(%rip), %xmm1    # 0x6b350
callq 0x63a04
movss (%rsp), %xmm0
movss %xmm0, 0x6c41a(%rip)    # 0x9d794
movl 0x6c40c(%rip), %edx     # 0x9d78c
movl %edx, 0x6c40a(%rip)     # 0x9d790
xorl %eax, %eax
movl $0x0, %ecx
testl %edx, %edx
jne 0x31397
movl 0x18066d(%rip), %ecx    # 0x1b1a04
movslq %ecx, %rdi
leaq 0x2(%r14), %rcx
leaq 0x3fe60b(%rip), %rdx    # 0x42f9b0
shlq $0x8, %rdi
leaq 0x3fee00(%rip), %rsi    # 0x4301b0
addq %rdi, %rsi
leaq 0x3a3416(%rip), %rdi    # 0x3d47d0
leaq 0x3fe9ef(%rip), %r8     # 0x42fdb0
xorl %r9d, %r9d
cmpq $0x100, %rax            # imm = 0x100
je 0x31452
testq %r14, %r14
je 0x313fe
movb -0x2(%rcx), %r10b
shlb $0x2, %r10b
movb %r10b, (%rdx,%rax,4)
movb -0x1(%rcx), %r11b
shlb $0x2, %r11b
movb %r11b, 0x1(%rdx,%rax,4)
movb (%rcx), %bl
shlb $0x2, %bl
movb %bl, 0x2(%rdx,%rax,4)
movb $0x0, 0x3(%rdx,%rax,4)
jmp 0x3140b
movb 0x2(%rdx,%rax,4), %bl
movb (%rdx,%rax,4), %r10b
movb 0x1(%rdx,%rax,4), %r11b
movzbl %bl, %ebx
movb (%rbx,%rsi), %bl
movb %bl, (%rdi,%rax,4)
movb %bl, 0x2(%r8,%rax,4)
movzbl %r11b, %r11d
movb (%r11,%rsi), %r11b
movb %r11b, 0x1(%rdi,%rax,4)
movb %r11b, 0x1(%r8,%rax,4)
movzbl %r10b, %r10d
movb (%r10,%rsi), %r10b
movb %r10b, 0x2(%rdi,%rax,4)
movb %r10b, (%r8,%rax,4)
movb %r9b, 0x3(%rdi,%rax,4)
movb %r9b, 0x3(%r8,%rax,4)
incq %rax
addq $0x3, %rcx
jmp 0x313c4
movl 0x4(%rsp), %ebx
testb $0x1, %bl
jne 0x3146e
leaq 0x3a336e(%rip), %rdx    # 0x3d47d0
xorl %edi, %edi
movl $0x100, %esi            # imm = 0x100
callq 0x639dd
leaq 0x5c24db(%rip), %rax    # 0x5f3950
cmpl $0x3, (%rax)
jne 0x314a5
leaq 0x3fe92f(%rip), %rdi    # 0x42fdb0
movl $0x400, %esi            # imm = 0x400
callq 0x64918
movl %eax, %ebp
testb $0x2, %bl
jne 0x3149f
cmpl 0x3ffd18(%rip), %ebp    # 0x4311b0
je 0x3149f
callq 0x46615
movl %ebp, 0x3ffd0b(%rip)    # 0x4311b0
xorl %eax, %eax
movb %al, 0x180594(%rip)     # 0x1b1a41
andw $0x0, 0x18058a(%rip)    # 0x1b1a3f
movb %al, 0x180588(%rip)     # 0x1b1a43
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq