// File: /workspace/github_cpp_makefile_star2_O3/misohena[P]drawstars/drawstars.cpp
// Function: void drawStarsPerspective<Range<GaiaStar>>(Image<ColorRGB<double>>&, Range<GaiaStar>, Mat4<float>, float, float, StarAppearanceOptions const&)
void drawStarsPerspective(ImageType &canvas, Range stars, Mat4F mat, float minMag, float maxMag, const StarAppearanceOptions &starAppearanceOptions)
{
    for(const auto &star : stars){
        const auto mag = star.mag();
        if(!(mag >= minMag && mag <= maxMag)){
            continue;
        }

        Vec4F pos = mat * dirRADec(star.ra_rad(), star.dec_rad());
        if(pos.w != 0){
            const float invW = 1 / pos.w;
            const float x = pos.x * invW;
            const float y = pos.y * invW;
            const float z = pos.z * invW;
            if(x >= -1 && x <= 1 && y >= -1 && y <= 1 && z >= -1 && z <= 1){
                const float screenX = canvas.width()/2*(1+x);
                const float screenY = canvas.height()/2*(1-y);
                if(canvas.includes(screenX, screenY)){
                    const StarAppearance appearance = calculateStarAppearance(star, starAppearanceOptions);
                    const ColorType color = appearance.color;
                    const float radius = appearance.radius;
                    //*canvas.at(screenX, screenY) += color;
                    fillCircle(
                        canvas, screenX, screenY, radius,
                        PixelPlotter(appearance));
                }
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movss %xmm1, 0x28(%rsp)
movss %xmm0, 0x2c(%rsp)
cmpq %rdx, %rsi
je 0xbd30
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0xc0(%rsp), %r13
leaq 0x50(%rsp), %rbp
movss 0x8(%r15), %xmm0
ucomiss 0x2c(%rsp), %xmm0
jb 0xbd23
movss 0x28(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jb 0xbd23
xorps %xmm0, %xmm0
cvtss2sd (%r15), %xmm0
movsd 0x15c4(%rip), %xmm1     # 0xd138
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
xorps %xmm0, %xmm0
cvtss2sd 0x4(%r15), %xmm0
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x30(%rsp)
callq 0x4210
movss %xmm0, 0x40(%rsp)
movss 0x24(%rsp), %xmm0
callq 0x4270
mulss 0x40(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
movss 0x30(%rsp), %xmm0
callq 0x4270
movaps %xmm0, 0x70(%rsp)
movss 0x30(%rsp), %xmm0
callq 0x4210
movss %xmm0, 0x30(%rsp)
movss 0x24(%rsp), %xmm0
callq 0x4210
mulss 0x30(%rsp), %xmm0
movaps 0x15ad(%rip), %xmm1     # 0xd1a0
movss %xmm0, %xmm1            # xmm1 = xmm0[0],xmm1[1,2,3]
movaps 0x40(%rsp), %xmm0
unpcklps 0x70(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x50(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x9db3
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
ucomiss 0x1412(%rip), %xmm2     # 0xd034
jne 0xbc2a
jnp 0xbd23
movaps %xmm0, %xmm3
movss 0x13db(%rip), %xmm0     # 0xd010
divss %xmm2, %xmm0
movaps %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm2      # xmm2 = xmm2[0,0],xmm0[0,0]
mulps %xmm2, %xmm3
movaps %xmm3, %xmm2
andps 0x15b3(%rip), %xmm2     # 0xd200
cmpleps 0x15db(%rip), %xmm2     # 0xd230
unpcklps %xmm2, %xmm2            # xmm2 = xmm2[0,0,1,1]
movmskpd %xmm2, %eax
testb $0x1, %al
je 0xbd23
shrb %al
je 0xbd23
mulss %xmm0, %xmm1
andps 0x1589(%rip), %xmm1     # 0xd200
movss 0x1391(%rip), %xmm0     # 0xd010
ucomiss %xmm1, %xmm0
jb 0xbd23
movl 0x4(%r12), %ecx
movl %ecx, %eax
shrl %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
shufps $0x55, %xmm3, %xmm1     # xmm1 = xmm1[1,1],xmm3[1,1]
movss 0x1369(%rip), %xmm2     # 0xd010
subss %xmm1, %xmm2
mulss %xmm0, %xmm2
cvttss2si %xmm2, %eax
cmpl %eax, %ecx
jbe 0xbd23
movl (%r12), %edx
movl %edx, %ecx
shrl %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
addss 0x1342(%rip), %xmm3     # 0xd010
mulss %xmm0, %xmm3
cvttss2si %xmm3, %ecx
cmpl %ecx, %edx
jbe 0xbd23
orl %ecx, %eax
js 0xbd23
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movaps %xmm3, 0x30(%rsp)
movss %xmm2, 0x24(%rsp)
callq 0xb29f
movss 0x68(%rsp), %xmm2
movups 0x50(%rsp), %xmm0
movups 0x60(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r12, %rdi
movaps 0x30(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
callq 0xbd42
addq $0x18, %r15
cmpq %r14, %r15
jne 0xbb44
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq