// File: /workspace/github_c_makefile_star2_O3/halcy[P]quasi_crystals_bot/Engine/Downsample.c
// Function: DownsampleBitmap
void DownsampleBitmap(Bitmap *dest,const Bitmap *src,int factor)
{
	float gammatable[256];
	for(int i=0;i<256;i++) gammatable[i]=powf((float)i/255.0,Gamma);

	for(int y=0;y<dest->height;y++)
	for(int x=0;x<dest->width;x++)
	{
		float r=0,g=0,b=0;
		for(int dy=0;dy<factor;dy++)
		for(int dx=0;dx<factor;dx++)
		{
			Pixel p=ReadPixelNoClip(src,x*factor+dx,y*factor+dy);
			r+=gammatable[ExtractRed(p)];
			g+=gammatable[ExtractGreen(p)];
			b+=gammatable[ExtractBlue(p)];
		}

		DrawPixelNoClip(dest,x,y,RGB(
			powf(r/(float)(factor*factor),1.0/Gamma)*255,
			powf(g/(float)(factor*factor),1.0/Gamma)*255,
			powf(b/(float)(factor*factor),1.0/Gamma)*255
		));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
leaq -0x478(%rsp), %rsp
movq %rsi, %r14
pmovsxbd 0xc0a2(%rip), %xmm0     # 0xe118
movq $-0x400, %rbx           # imm = 0xFC00
movl %edx, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movdqa %xmm0, 0x20(%rsp)
movdqa 0xc04c(%rip), %xmm1     # 0xe0e0
pmovzxdq 0x20(%rsp), %xmm0
por %xmm1, %xmm0
subpd %xmm1, %xmm0
movss 0xc065(%rip), %xmm1     # 0xe110
mulpd 0xc03d(%rip), %xmm0     # 0xe0f0
cvtpd2ps %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
callq 0x1190
movapd %xmm0, 0x40(%rsp)
movshdup 0x50(%rsp), %xmm0       # xmm0 = mem[1,1,3,3]
movss 0xc03a(%rip), %xmm1     # 0xe110
callq 0x1190
movaps 0x40(%rsp), %xmm1
insertps $0x10, %xmm0, %xmm1     # xmm1 = xmm1[0],xmm0[0],xmm1[2,3]
movdqa 0x20(%rsp), %xmm0
movlps %xmm1, 0x470(%rsp,%rbx)
addq $0x8, %rbx
paddd 0xc000(%rip), %xmm0     # 0xe100
jne 0x2086
movq 0x18(%rsp), %rax
movl 0x10(%rsp), %ebx
movzwl 0x2(%rax), %eax
testw %ax, %ax
jle 0x23a2
movl %ebx, %ecx
movss 0xbee6(%rip), %xmm1     # 0xe008
movl $0xff, %r15d
movl $0x0, 0x8(%rsp)
imull %ebx, %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movq 0x18(%rsp), %rcx
movzwl (%rcx), %ecx
divss %xmm0, %xmm1
movss 0x70(%rsp), %xmm0
movss %xmm0, 0x3c(%rsp)
movss %xmm1, 0xc(%rsp)
testw %cx, %cx
jle 0x238d
movl 0x8(%rsp), %eax
xorl %r13d, %r13d
movl $0x0, 0x14(%rsp)
imull %ebx, %eax
movq %rax, 0x68(%rsp)
testl %ebx, %ebx
movl %r13d, 0x38(%rsp)
jle 0x226b
xorps %xmm0, %xmm0
xorl %r12d, %r12d
xorps %xmm1, %xmm1
movss %xmm0, 0x20(%rsp)
movss %xmm0, 0x40(%rsp)
movq 0x68(%rsp), %rax
movl 0x14(%rsp), %ebp
leal (%r12,%rax), %r13d
movq %r14, %rdi
movl %ebp, %esi
movl %r13d, %edx
movss %xmm1, 0x50(%rsp)
callq 0x8544
movl %eax, %esi
andl $0xff, %esi
je 0x2218
movl %eax, %edx
movl %eax, %ecx
shrl $0x8, %edx
movl %ecx, %edi
shrl $0x18, %ecx
andl %r15d, %edx
shrl $0x10, %edi
movl %edx, %eax
andl %r15d, %edi
shll $0x8, %eax
subl %edx, %eax
xorl %edx, %edx
divw %si
movl %edi, %edx
shll $0x8, %edx
subl %edi, %edx
movzwl %ax, %eax
movss 0x70(%rsp,%rax,4), %xmm0
movl %edx, %eax
xorl %edx, %edx
divw %si
movl %ecx, %edx
shll $0x8, %edx
subl %ecx, %edx
movzwl %ax, %eax
movss 0x70(%rsp,%rax,4), %xmm1
movl %edx, %eax
xorl %edx, %edx
divw %si
movzwl %ax, %eax
jmp 0x2223
movss 0x3c(%rsp), %xmm0
xorl %eax, %eax
movaps %xmm0, %xmm1
movss 0x40(%rsp), %xmm2
incl %ebp
addss %xmm1, %xmm2
movss 0x50(%rsp), %xmm1
decl %ebx
addss %xmm0, %xmm1
movss %xmm2, 0x40(%rsp)
movss 0x20(%rsp), %xmm2
addss 0x70(%rsp,%rax,4), %xmm2
movss %xmm2, 0x20(%rsp)
jne 0x21a7
incl %r12d
movl 0x10(%rsp), %ebx
cmpl %ebx, %r12d
jne 0x219a
jmp 0x227d
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movss %xmm0, 0x40(%rsp)
movss %xmm0, 0x20(%rsp)
mulss 0xc(%rsp), %xmm1
movaps %xmm1, %xmm0
movss 0xbe86(%rip), %xmm1     # 0xe114
callq 0x1190
movss 0xbd71(%rip), %xmm1     # 0xe00c
movl $0xff, %ebp
mulss %xmm1, %xmm0
ucomiss 0xbd71(%rip), %xmm0     # 0xe01c
jb 0x22c4
ucomiss %xmm1, %xmm0
movl $0xffff, %ebp           # imm = 0xFFFF
ja 0x22c4
cvttss2si %xmm0, %ebp
shll $0x8, %ebp
orl $0xff, %ebp
movss 0x40(%rsp), %xmm0
movss 0xbe42(%rip), %xmm1     # 0xe114
mulss 0xc(%rsp), %xmm0
callq 0x1190
movss 0xbd27(%rip), %xmm1     # 0xe00c
xorl %ebx, %ebx
movl $0x0, %r12d
mulss %xmm1, %xmm0
ucomiss 0xbd24(%rip), %xmm0     # 0xe01c
jb 0x230e
ucomiss %xmm1, %xmm0
movl $0xff0000, %r12d        # imm = 0xFF0000
ja 0x230e
cvttss2si %xmm0, %r12d
shll $0x10, %r12d
movss 0x20(%rsp), %xmm0
movss 0xbdf8(%rip), %xmm1     # 0xe114
mulss 0xc(%rsp), %xmm0
callq 0x1190
movss 0xbcdd(%rip), %xmm1     # 0xe00c
mulss %xmm1, %xmm0
ucomiss 0xbce2(%rip), %xmm0     # 0xe01c
movl 0x38(%rsp), %r13d
jb 0x2352
ucomiss %xmm1, %xmm0
movl $0xff000000, %ebx       # imm = 0xFF000000
ja 0x2352
cvttss2si %xmm0, %ebx
shll $0x18, %ebx
orl %r12d, %ebp
movl %r13d, %esi
movl 0x8(%rsp), %edx
orl %ebx, %ebp
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movl %ebp, %ecx
callq 0x84a7
movswl (%rbx), %ecx
movl 0x10(%rsp), %ebx
incl %r13d
addl %ebx, 0x14(%rsp)
cmpl %ecx, %r13d
jl 0x2178
movq 0x18(%rsp), %rax
movzwl 0x2(%rax), %eax
movl 0x8(%rsp), %esi
movswl %ax, %edx
incl %esi
cmpl %edx, %esi
movl %esi, 0x8(%rsp)
jl 0x2158
leaq 0x478(%rsp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)