// File: /workspace/github_c_makefile_star2_O2/esheldon[P]xshear/src/healpix.c
// Function: hpix_eq2pix
int64 hpix_eq2pix(const HealPix* hpix, double ra, double dec) {
    int64 nside=hpix->nside;
    int64 ipix=0;
    double theta=0, phi=0;

    hpix_radec_degrees_to_thetaphi_radians(ra, dec, &theta, &phi);

    double z = cos(theta);
    double za = fabs(z);

    // in [0,4)
    double tt = fmod(phi, M_TWO_PI)/M_PI_2;

    if (za <= M_TWOTHIRD) {
        double temp1 = nside*(.5 + tt);
        double temp2 = nside*.75*z;

        int64 jp = (int64)(temp1-temp2); // index of  ascending edge line
        int64 jm = (int64)(temp1+temp2); // index of descending edge line
        int64 ir = nside + 1 + jp - jm;  // in {1,2n+1} (ring number counted from z=2/3)
        int64 kshift = 1 - (ir % 2);      // kshift=1 if ir even, 0 otherwise
        
        int64 nl4 = 4*nside;
        int64 ip = (int64)( ( jp+jm - nside + kshift + 1 ) / 2); // in {0,4n-1}

        ip = ip % nl4;

        ipix = hpix->ncap + nl4*(ir-1) + ip;

    } else { 
        // North & South polar caps
        double tp = tt - (int64)(tt);   // MODULO(tt,1.0_dp)

        double tmp = nside * sqrt( 3.0*(1.0 - za) );
        int64 jp = (int64)(tp*tmp);              // increasing edge line index
        int64 jm = (int64)((1.0 - tp) * tmp); // decreasing edge line index

        int64 ir = jp + jm + 1;        // ring number counted from the closest pole
        int64 ip = (int64)( tt * ir);     // in {0,4*ir-1}

        if (ip >= 4*ir) {
            ip = ip - 4*ir;
        }
        if (z>0.) {
            ipix = 2*ir*(ir-1) + ip;
        } else {
            ipix = hpix->npix - 2*ir*(ir+1) + ip;
        }

    }

    return ipix;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq (%rdi), %r14
leaq 0x40(%rsp), %r15
andq $0x0, (%r15)
leaq 0x38(%rsp), %r12
andq $0x0, (%r12)
movq %r15, %rdi
movq %r12, %rsi
callq 0x2eff
movsd (%r15), %xmm0
callq 0x2140
movaps 0x6370(%rip), %xmm1     # 0x90c0
movaps %xmm0, 0x10(%rsp)
andps %xmm0, %xmm1
movaps %xmm1, (%rsp)
movsd (%r12), %xmm0
movsd 0x631e(%rip), %xmm1     # 0x9088
callq 0x2130
movapd (%rsp), %xmm3
movapd %xmm0, %xmm2
divsd 0x62b0(%rip), %xmm2     # 0x9030
movsd 0x62d8(%rip), %xmm0     # 0x9060
ucomisd %xmm3, %xmm0
jae 0x2dd4
cvttsd2si %xmm2, %rax
cvtsi2sd %rax, %xmm1
movapd %xmm2, %xmm4
movsd 0x627c(%rip), %xmm5     # 0x9020
movapd %xmm5, %xmm0
subsd %xmm3, %xmm0
cvtsi2sd %r14, %xmm6
mulsd 0x625f(%rip), %xmm0     # 0x9018
subsd %xmm1, %xmm4
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jb 0x2e4b
sqrtsd %xmm0, %xmm0
jmp 0x2e7e
cvtsi2sd %r14, %xmm0
addsd 0x628f(%rip), %xmm2     # 0x9070
mulsd %xmm0, %xmm2
mulsd 0x62a3(%rip), %xmm0     # 0x9090
mulsd 0x10(%rsp), %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %rdi
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %rcx
leaq (%r14,%rdi), %rsi
subq %rcx, %rsi
leaq 0x1(%rsi), %rax
pushq $0x2
popq %r8
cqto
idivq %r8
subq %r14, %rcx
shlq $0x2, %r14
addq %rdi, %rcx
subq %rdx, %rcx
addq $0x2, %rcx
movq %rcx, %rax
cqto
idivq %r8
cqto
idivq %r14
imulq %rsi, %r14
addq %rdx, %r14
addq 0x18(%rbx), %r14
jmp 0x2ef0
movsd %xmm2, (%rsp)
movsd %xmm4, 0x30(%rsp)
movsd %xmm6, 0x28(%rsp)
callq 0x22b0
movsd 0x28(%rsp), %xmm6
movsd 0x61b1(%rip), %xmm5     # 0x9020
movsd 0x30(%rsp), %xmm4
xorpd %xmm3, %xmm3
movsd (%rsp), %xmm2
movapd 0x10(%rsp), %xmm1
mulsd %xmm6, %xmm0
subsd %xmm4, %xmm5
mulsd %xmm0, %xmm4
cvttsd2si %xmm4, %rcx
mulsd %xmm0, %xmm5
cvttsd2si %xmm5, %rdx
leaq (%rcx,%rdx), %rax
leaq 0x1(%rcx,%rdx), %rcx
cvtsi2sd %rcx, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %r14
leaq 0x4(,%rax,4), %rdx
xorl %esi, %esi
cmpq %r14, %rdx
cmovleq %rdx, %rsi
subq %rsi, %r14
ucomisd %xmm3, %xmm1
jbe 0x2ed8
imulq %rcx, %rax
addq %rax, %rax
jmp 0x2eed
movq 0x8(%rbx), %rdx
addq $0x2, %rax
imulq %rax, %rcx
addq %rcx, %rcx
subq %rcx, %rdx
movq %rdx, %rax
addq %rax, %r14
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq