// File: /workspace/github_c_makefile_star2_O3/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
testb $0x4, %dl
movl %edx, 0x4(%rsp)
jne 0x348d5
xorl %ecx, %ecx
testl %edi, %edi
cmovgl %edi, %ecx
cmpl $0xf, %ecx
movl $0xf, %eax
cmovll %ecx, %eax
movl %eax, 0x184131(%rip)    # 0x1b8a04
jmp 0x348db
movl 0x184129(%rip), %eax    # 0x1b8a04
cvtsi2ss %eax, %xmm0
divss 0x42829(%rip), %xmm0    # 0x77110
addss 0x42825(%rip), %xmm0    # 0x77114
movss %xmm0, (%rsp)
movss 0x42818(%rip), %xmm0    # 0x77114
leaq 0x1497e5(%rip), %r15    # 0x17e0e8
leaq 0x42e4e(%rip), %r12     # 0x77758
leaq 0x426ff(%rip), %r13     # 0x77010
leaq 0x42e6b(%rip), %rbx     # 0x77783
xorl %ebp, %ebp
movl 0x6fe6c(%rip), %eax     # 0xa478c
cmpl $0x1, %eax
je 0x34957
testl %eax, %eax
je 0x3498d
movl $0x2, 0x6fe59(%rip)     # 0xa478c
movss (%rsp), %xmm1
callq 0x6f2bd
testl %eax, %eax
je 0x3499a
movl %eax, %ebp
xorl %eax, %eax
cmpl $0x0, (%r15)
sete %al
movl %eax, 0x6fe3a(%rip)     # 0xa478c
movq %rbx, %rdi
jmp 0x3496f
cmpl $0x0, (%r15)
je 0x34988
xorl %eax, %eax
testl %ebp, %ebp
sete %al
addl %eax, %eax
movl %eax, 0x6fe20(%rip)     # 0xa478c
movq %r12, %rdi
movslq (%r13,%rax,4), %rsi
addq %r13, %rsi
xorl %eax, %eax
callq 0x6e05b
movss 0x4278e(%rip), %xmm0    # 0x77114
jmp 0x3491a
movss (%rsp), %xmm0
movss 0x41a9b(%rip), %xmm1    # 0x76430
callq 0x6f2bd
movss (%rsp), %xmm0
movss %xmm0, 0x6fded(%rip)    # 0xa4794
movl 0x6fddf(%rip), %edx     # 0xa478c
movl %edx, 0x6fddd(%rip)     # 0xa4790
xorl %eax, %eax
movl $0x0, %ecx
testl %edx, %edx
jne 0x349c4
movl 0x184040(%rip), %ecx    # 0x1b8a04
movslq %ecx, %rdi
leaq 0x2(%r14), %rcx
leaq 0x401fde(%rip), %rdx    # 0x4369b0
shlq $0x8, %rdi
leaq 0x4027d3(%rip), %rsi    # 0x4371b0
addq %rdi, %rsi
leaq 0x3a6de9(%rip), %rdi    # 0x3db7d0
leaq 0x4023c2(%rip), %r8     # 0x436db0
xorl %r9d, %r9d
testq %r14, %r14
je 0x34a1f
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 0x34a2c
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
cmpq $0x100, %rax            # imm = 0x100
jne 0x349f1
movl 0x4(%rsp), %ebx
testb $0x1, %bl
jne 0x34a96
leaq 0x3a6d46(%rip), %rdx    # 0x3db7d0
xorl %edi, %edi
movl $0x100, %esi            # imm = 0x100
callq 0x6f296
leaq 0x5c5eb3(%rip), %rax    # 0x5fa950
cmpl $0x3, (%rax)
jne 0x34acd
leaq 0x402307(%rip), %rdi    # 0x436db0
movl $0x400, %esi            # imm = 0x400
callq 0x70277
movl %eax, %ebp
testb $0x2, %bl
jne 0x34ac7
cmpl 0x4036f0(%rip), %ebp    # 0x4381b0
je 0x34ac7
callq 0x4fe35
movl %ebp, 0x4036e3(%rip)    # 0x4381b0
xorl %eax, %eax
movb %al, 0x183f6c(%rip)     # 0x1b8a41
movw $0x0, 0x183f61(%rip)    # 0x1b8a3f
movb %al, 0x183f5f(%rip)     # 0x1b8a43
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq