// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %rsi, %rbx
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rsp)
movq %rdi, 0x18(%rsp)
movq (%rdi), %r13
movsd %xmm2, 0x10(%rsp)
movaps %xmm2, %xmm0
callq 0x2140
movsd %xmm0, 0x68(%rsp)
xorl %eax, %eax
movq %rbx, 0x60(%rsp)
movq %rax, (%rbx)
leaq 0x90(%rsp), %r15
movq %rax, (%r15)
leaq 0x88(%rsp), %r12
movq %rax, (%r12)
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq %r15, %rdi
movq %r12, %rsi
callq 0x47d8
movsd (%r15), %xmm0
movsd %xmm0, (%rsp)
callq 0x2250
movsd %xmm0, 0x8(%rsp)
movsd (%r12), %xmm0
movsd %xmm0, 0x30(%rsp)
callq 0x2140
mulsd 0x8(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movsd 0x30(%rsp), %xmm0
callq 0x2250
mulsd 0x8(%rsp), %xmm0
movapd %xmm0, 0x70(%rsp)
movsd (%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x8(%rsp)
xorpd %xmm1, %xmm1
xorps %xmm0, %xmm0
movsd %xmm0, (%rsp)
movapd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movapd 0x70(%rsp), %xmm1
jne 0x3eae
jp 0x3eae
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x3eae
jnp 0x3ec2
movapd %xmm1, %xmm0
movapd 0x20(%rsp), %xmm1
callq 0x2210
movsd %xmm0, (%rsp)
movsd (%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x50(%rsp)
movsd 0x8(%rsp), %xmm0
callq 0x2270
movapd %xmm0, %xmm2
movsd 0x10(%rsp), %xmm1
addsd %xmm1, %xmm0
subsd %xmm1, %xmm2
movsd %xmm2, 0x30(%rsp)
ucomisd 0x5133(%rip), %xmm0     # 0x9030
movsd 0x511b(%rip), %xmm1     # 0x9020
movsd %xmm1, 0x10(%rsp)
movsd 0x510d(%rip), %xmm1     # 0x9020
jae 0x3f1e
callq 0x2250
movapd %xmm0, %xmm1
movq 0x18(%rsp), %rdi
movapd %xmm1, %xmm0
callq 0x4226
cmpq $0x3, %rax
movl $0x2, %ebx
cmovgeq %rax, %rbx
leaq -0x1(%rbx), %r15
movsd 0x50fb(%rip), %xmm0     # 0x9040
movsd 0x30(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x3f5c
movapd %xmm1, %xmm0
callq 0x2250
jmp 0x3f64
movsd 0x50d4(%rip), %xmm0     # 0x9038
movq 0x18(%rsp), %rdi
callq 0x4226
movq %rax, %r12
leaq -0x1(,%r13,4), %rax
incq %r12
cmpq %r12, %rax
cmovlq %rax, %r12
cmpq %r12, %r15
jg 0x4154
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
movsd 0x507c(%rip), %xmm1     # 0x9018
mulsd %xmm0, %xmm1
mulsd %xmm1, %xmm0
movsd 0x507c(%rip), %xmm3     # 0x9028
movapd 0x70(%rsp), %xmm6
movapd %xmm6, %xmm2
mulsd %xmm6, %xmm2
movapd 0x20(%rsp), %xmm5
movapd %xmm5, %xmm4
mulsd %xmm5, %xmm4
addsd %xmm2, %xmm4
movapd %xmm4, 0x30(%rsp)
movsd 0x10(%rsp), %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
divsd %xmm1, %xmm3
movsd %xmm3, 0x48(%rsp)
leaq (,%r13,4), %rbp
leaq (%r13,%r13,2), %rax
movq %rax, 0x58(%rsp)
leaq (%r13,%r13), %r14
xorpd %xmm0, %xmm0
cmpeqpd %xmm0, %xmm6
cmpeqpd %xmm0, %xmm5
andpd %xmm6, %xmm5
movq %rbx, %rax
movd %xmm5, %ebx
negq %r12
subq %rax, %r14
incq %r14
subq %rax, %rbp
incq %rbp
cmpq %r13, %r15
jge 0x4053
movq %r15, %rax
imulq %r15, %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x10(%rsp), %xmm0
movsd 0x4fd3(%rip), %xmm1     # 0x9020
subsd %xmm0, %xmm1
jmp 0x4084
cmpq 0x58(%rsp), %r15
jle 0x4076
xorps %xmm1, %xmm1
cvtsi2sd %rbp, %xmm1
mulsd %xmm1, %xmm1
mulsd 0x10(%rsp), %xmm1
addsd 0x4fc4(%rip), %xmm1     # 0x9038
jmp 0x4084
xorps %xmm1, %xmm1
cvtsi2sd %r14, %xmm1
mulsd 0x48(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x8(%rsp), %xmm0
movsd 0x68(%rsp), %xmm2
subsd %xmm0, %xmm2
testb $0x1, %bl
je 0x40a7
ucomisd 0x4fab(%rip), %xmm2     # 0x9050
jmp 0x410a
movapd %xmm1, %xmm0
xorpd 0x4ffd(%rip), %xmm0     # 0x90b0
mulsd %xmm1, %xmm0
addsd 0x4f61(%rip), %xmm0     # 0x9020
mulsd 0x30(%rsp), %xmm0
ucomisd 0x4f83(%rip), %xmm0     # 0x9050
jb 0x40d5
sqrtsd %xmm0, %xmm0
jmp 0x40e6
movapd %xmm2, 0x20(%rsp)
callq 0x22b0
movapd 0x20(%rsp), %xmm2
divsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andpd 0x4fca(%rip), %xmm0     # 0x90c0
movsd 0x4f22(%rip), %xmm1     # 0x9020
ucomisd %xmm0, %xmm1
jae 0x4116
ucomisd 0x50(%rsp), %xmm2
movsd 0x4f36(%rip), %xmm1     # 0x9048
jbe 0x4123
jmp 0x413a
movapd %xmm2, %xmm0
callq 0x2160
movapd %xmm0, %xmm1
movq 0x18(%rsp), %rdi
movq %r15, %rsi
movsd (%rsp), %xmm0
movq 0x60(%rsp), %rdx
callq 0x4340
leaq (%r12,%r15), %rax
incq %rax
incq %r15
decq %r14
decq %rbp
cmpq $0x1, %rax
jne 0x402b
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq