// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rcx, %rbx
movss %xmm1, 0x28(%rsp)
movss %xmm0, 0x2c(%rsp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %r12
leaq 0xa0(%rsp), %r13
leaq 0x30(%rsp), %rbp
cmpq %r14, %r15
je 0xb82d
movss 0x8(%r15), %xmm0
ucomiss 0x2c(%rsp), %xmm0
jb 0xb824
movss 0x28(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jb 0xb824
cvtss2sd (%r15), %xmm0
movsd 0xa4b(%rip), %xmm2      # 0xc138
mulsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
cvtss2sd 0x4(%r15), %xmm1
mulsd %xmm2, %xmm1
cvtsd2ss %xmm1, %xmm1
callq 0x5b71
movlhps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
movups %xmm0, 0x30(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0x87a0
movaps %xmm1, %xmm2
shufps $0x55, %xmm1, %xmm2     # xmm2 = xmm2[1,1],xmm1[1,1]
ucomiss 0x933(%rip), %xmm2      # 0xc05c
jne 0xb731
jnp 0xb824
movaps %xmm0, %xmm3
movss 0x8d8(%rip), %xmm0      # 0xc014
divss %xmm2, %xmm0
movaps %xmm0, %xmm2
shufps $0x0, %xmm0, %xmm2      # xmm2 = xmm2[0,0],xmm0[0,0]
mulps %xmm2, %xmm3
movaps %xmm3, %xmm2
andps 0xa9c(%rip), %xmm2      # 0xc1f0
cmpleps 0xac4(%rip), %xmm2      # 0xc220
unpcklps %xmm2, %xmm2            # xmm2 = xmm2[0,0,1,1]
movmskpd %xmm2, %eax
testb $0x1, %al
je 0xb824
shrb %al
je 0xb824
mulss %xmm0, %xmm1
andps 0xa72(%rip), %xmm1      # 0xc1f0
movss 0x88e(%rip), %xmm0      # 0xc014
ucomiss %xmm1, %xmm0
jb 0xb824
movl 0x4(%r12), %ecx
movl %ecx, %eax
shrl %eax
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
shufps $0x55, %xmm3, %xmm1     # xmm1 = xmm1[1,1],xmm3[1,1]
movss 0x869(%rip), %xmm2      # 0xc014
subss %xmm1, %xmm2
mulss %xmm0, %xmm2
cvttss2si %xmm2, %eax
cmpl %eax, %ecx
jbe 0xb824
movl (%r12), %edx
movl %edx, %ecx
shrl %ecx
cvtsi2ss %ecx, %xmm0
addss 0x845(%rip), %xmm3      # 0xc014
mulss %xmm0, %xmm3
cvttss2si %xmm3, %ecx
cmpl %ecx, %edx
jbe 0xb824
orl %ecx, %eax
js 0xb824
movq %rbp, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movaps %xmm3, 0x50(%rsp)
movss %xmm2, 0x24(%rsp)
callq 0xadfa
movss 0x48(%rsp), %xmm2
movups 0x30(%rsp), %xmm0
movups 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r12, %rdi
movaps 0x50(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
callq 0xb83c
addq $0x18, %r15
jmp 0xb6b7
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq