// File: /workspace/github_c_makefile_star2_O2/esheldon[P]xshear/src/healpix.c
// Function: hpix_disc_contains
void hpix_disc_contains(
        const HealPix* hpix,
        double ra, double dec, double radius, 
        lvector* listpix) {

    //double vector0[3];
    int64 nside=hpix->nside;
    double cosang = cos(radius);

    // this does not alter the storage
    vector_resize(listpix, 0);

    double x0, y0, z0;
    hpix_eq2xyz(ra, dec, &x0, &y0, &z0);

    double dth1 = 1. / (3.0*nside*nside);
    double dth2 = 2. / (3.0*nside);

    double phi0=0.0;
    if ((x0 != 0.) || (y0 != 0.)) {
        // in (-Pi, Pi]
        phi0 = atan2(y0, x0);
    }
    double cosphi0 = cos(phi0);
    double a = x0*x0 + y0*y0;

    //     --- coordinate z of highest and lowest points in the disc ---
    double rlat0  = asin(z0);    // latitude in RAD of the center
    double rlat1  = rlat0 + radius;
    double rlat2  = rlat0 - radius;
    double zmax;
    if (rlat1 >=  M_PI_2) {
        zmax =  1.0;
    } else {
        zmax = sin(rlat1);
    }
    int64 irmin = hpix_ring_num(hpix, zmax);
    irmin = i64max(1, irmin-1); // start from a higher point, to be safe

    double zmin;
    if (rlat2 <= -M_PI_2) {
        zmin = -1.;
    } else {
        zmin = sin(rlat2);
    }
    int64 irmax = hpix_ring_num(hpix, zmin);
    irmax = i64min(4*nside-1, irmax + 1); // go down to a lower point

    //double z, tmp=0;
    double tmp=0;
    for (int64 iz=irmin; iz<= irmax; iz++) {

        double z;
        if (iz <= nside-1) { // north polar cap
              z = 1.  - iz*iz*dth1;
        } else if (iz <= 3*nside) { // tropical band + equat.
            z = (2*nside-iz) * dth2;
        } else {
            tmp = 4*nside-iz;
            z = - 1. + tmp*tmp*dth1;
        }
        double b = cosang - z*z0;
        double c = 1. - z*z;
        //double x = (cosang-z*z0)/sqrt((1-z0)*(1+z0));

        double dphi;
        if ((x0==0.) && (y0==0.)) {
            dphi=M_PI;
            if (b > 0.) {
                goto SKIP2; // out of the disc, 2008-03-30
            }
            goto SKIP1;
        } 

        double cosdphi = b / sqrt(a*c);
        if (fabs(cosdphi) <= 1.) {
              dphi = acos(cosdphi); // in [0,Pi]
        } else {
            if (cosphi0 < cosdphi) {
                goto SKIP2; // out of the disc
            }
            dphi = M_PI; // all the pixels at this elevation are in the disc
        }
SKIP1:
        hpix_in_ring(hpix, iz, phi0, dphi, listpix);

SKIP2:
        // we have to put something here
        continue;

    }


}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %rbx
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, 0x20(%rsp)
movq (%rdi), %r13
movsd %xmm2, 0x8(%rsp)
movaps %xmm2, %xmm0
callq 0x2140
movsd %xmm0, 0x80(%rsp)
movq %rbx, 0x78(%rsp)
andq $0x0, (%rbx)
leaq 0xc0(%rsp), %r15
leaq 0xb8(%rsp), %r12
leaq 0x88(%rsp), %rdx
movsd 0x10(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movq %r15, %rdi
movq %r12, %rsi
callq 0x288a
cvtsi2sd %r13, %xmm0
movsd %xmm0, (%rsp)
movsd (%r15), %xmm2
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movapd %xmm2, 0x30(%rsp)
ucomisd %xmm1, %xmm2
movsd (%r12), %xmm0
movapd %xmm0, 0x90(%rsp)
jne 0x257c
jp 0x257c
ucomisd %xmm1, %xmm0
jne 0x257c
jnp 0x258d
movapd 0x30(%rsp), %xmm1
callq 0x2210
movsd %xmm0, 0x10(%rsp)
movsd 0x6a83(%rip), %xmm0     # 0x9018
mulsd (%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x70(%rsp)
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
callq 0x2270
movapd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm2
addsd %xmm2, %xmm1
subsd %xmm2, %xmm0
movsd %xmm0, 0x68(%rsp)
ucomisd 0x6a4b(%rip), %xmm1     # 0x9030
movsd 0x6a33(%rip), %xmm0     # 0x9020
movsd %xmm0, 0x8(%rsp)
movsd 0x6a25(%rip), %xmm0     # 0x9020
jae 0x2606
movapd %xmm1, %xmm0
callq 0x2250
movsd (%rsp), %xmm1
mulsd 0x48(%rsp), %xmm1
movsd %xmm1, (%rsp)
movsd 0x6a0a(%rip), %xmm1     # 0x9028
movsd %xmm1, 0x18(%rsp)
movapd 0x90(%rsp), %xmm1
mulsd %xmm1, %xmm1
movapd %xmm1, 0xa0(%rsp)
movapd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movapd %xmm1, 0x50(%rsp)
movq 0x20(%rsp), %rdi
callq 0x2925
cmpq $0x3, %rax
pushq $0x2
popq %rbx
cmovgeq %rax, %rbx
movsd 0x69d1(%rip), %xmm1     # 0x9038
movsd 0x69d1(%rip), %xmm0     # 0x9040
movsd 0x68(%rsp), %xmm2
ucomisd %xmm2, %xmm0
movapd %xmm1, %xmm0
jae 0x2688
movapd %xmm2, %xmm0
callq 0x2250
movsd 0x8(%rsp), %xmm1
divsd (%rsp), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x18(%rsp), %xmm1
divsd 0x48(%rsp), %xmm1
movsd %xmm1, 0x18(%rsp)
movapd 0x50(%rsp), %xmm1
addsd 0xa0(%rsp), %xmm1
movapd %xmm1, 0x50(%rsp)
leaq -0x1(%rbx), %r15
movq 0x20(%rsp), %rdi
callq 0x2925
movq %rax, %r12
leaq -0x1(,%r13,4), %rax
incq %r12
cmpq %r12, %rax
cmovlq %rax, %r12
leaq (,%r13,4), %rbp
leaq (,%r13,2), %rax
addq %r13, %rax
movq %rax, (%rsp)
leaq (%r13,%r13), %r14
xorpd %xmm0, %xmm0
movapd 0x90(%rsp), %xmm2
cmpeqpd %xmm0, %xmm2
movapd 0x30(%rsp), %xmm1
cmpeqpd %xmm0, %xmm1
andpd %xmm2, %xmm1
movq %rbx, %rax
movd %xmm1, %ebx
subq %rax, %rbp
incq %rbp
subq %rax, %r14
incq %r14
cmpq %r12, %r15
jg 0x2853
cmpq %r13, %r15
jge 0x2761
movq %r15, %rax
imulq %r15, %rax
cvtsi2sd %rax, %xmm0
mulsd 0x8(%rsp), %xmm0
movsd 0x68c5(%rip), %xmm1     # 0x9020
subsd %xmm0, %xmm1
jmp 0x278b
cmpq (%rsp), %r15
jle 0x2780
cvtsi2sd %rbp, %xmm1
mulsd %xmm1, %xmm1
mulsd 0x8(%rsp), %xmm1
addsd 0x68ba(%rip), %xmm1     # 0x9038
jmp 0x278b
cvtsi2sd %r14, %xmm1
mulsd 0x18(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x28(%rsp), %xmm0
movsd 0x80(%rsp), %xmm2
subsd %xmm0, %xmm2
testb $0x1, %bl
je 0x27b1
ucomisd 0x68a1(%rip), %xmm2     # 0x9050
jmp 0x2814
movapd %xmm1, %xmm0
xorpd 0x68f3(%rip), %xmm0     # 0x90b0
mulsd %xmm1, %xmm0
addsd 0x6857(%rip), %xmm0     # 0x9020
mulsd 0x50(%rsp), %xmm0
ucomisd 0x6879(%rip), %xmm0     # 0x9050
jb 0x27df
sqrtsd %xmm0, %xmm0
jmp 0x27f0
movapd %xmm2, 0x30(%rsp)
callq 0x22b0
movapd 0x30(%rsp), %xmm2
divsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andpd 0x68c0(%rip), %xmm0     # 0x90c0
movsd 0x6818(%rip), %xmm1     # 0x9020
ucomisd %xmm0, %xmm1
jae 0x2820
ucomisd 0x70(%rsp), %xmm2
movsd 0x682c(%rip), %xmm1     # 0x9048
jbe 0x282d
jmp 0x2845
movapd %xmm2, %xmm0
callq 0x2160
movapd %xmm0, %xmm1
movq 0x20(%rsp), %rdi
movq %r15, %rsi
movsd 0x10(%rsp), %xmm0
movq 0x78(%rsp), %rdx
callq 0x2a3c
incq %r15
decq %rbp
decq %r14
jmp 0x2733
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq