// File: /workspace/github_c_makefile_star2_O3/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
xorl %eax, %eax
leaq 0x40(%rsp), %r15
movq %rax, (%r15)
leaq 0x38(%rsp), %r12
movq %rax, (%r12)
movq %r15, %rdi
movq %r12, %rsi
callq 0x4882
movsd (%r15), %xmm0
callq 0x2140
movaps 0x4a0b(%rip), %xmm1     # 0x90c0
movaps %xmm0, 0x10(%rsp)
andps %xmm0, %xmm1
movaps %xmm1, (%rsp)
movsd (%r12), %xmm0
movsd 0x49b9(%rip), %xmm1     # 0x9088
callq 0x2130
movapd (%rsp), %xmm3
movapd %xmm0, %xmm2
divsd 0x494b(%rip), %xmm2     # 0x9030
movsd 0x4973(%rip), %xmm0     # 0x9060
ucomisd %xmm3, %xmm0
jae 0x473c
cvttsd2si %xmm2, %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
movapd %xmm2, %xmm4
movsd 0x4914(%rip), %xmm5     # 0x9020
movapd %xmm5, %xmm0
subsd %xmm3, %xmm0
cvtsi2sd %r14, %xmm6
mulsd 0x48f7(%rip), %xmm0     # 0x9018
subsd %xmm1, %xmm4
xorpd %xmm3, %xmm3
ucomisd %xmm3, %xmm0
jb 0x47cb
sqrtsd %xmm0, %xmm0
jmp 0x47fe
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
addsd 0x4924(%rip), %xmm2     # 0x9070
mulsd %xmm0, %xmm2
mulsd 0x4938(%rip), %xmm0     # 0x9090
mulsd 0x10(%rsp), %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %rcx
leaq (%r14,%rax), %rsi
subq %rcx, %rsi
leaq 0x1(%rsi), %rdx
movq %rdx, %rdi
shrq $0x3f, %rdi
addq %rsi, %rdi
incq %rdi
andq $-0x2, %rdi
subq %r14, %rcx
shlq $0x2, %r14
addq %rax, %rcx
subq %rdx, %rdi
leaq (%rcx,%rdi), %rax
addq %rdi, %rcx
addq $0x2, %rcx
shrq $0x3f, %rcx
addq %rcx, %rax
addq $0x2, %rax
sarq %rax
cqto
idivq %r14
imulq %rsi, %r14
addq %rdx, %r14
addq 0x18(%rbx), %r14
jmp 0x4873
movsd %xmm2, (%rsp)
movsd %xmm4, 0x30(%rsp)
movsd %xmm6, 0x28(%rsp)
callq 0x22b0
movsd 0x28(%rsp), %xmm6
movsd 0x4831(%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
xorps %xmm0, %xmm0
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 0x485b
imulq %rcx, %rax
addq %rax, %rax
jmp 0x4870
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