// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movsd %xmm1, -0x18(%rbp)
movsd %xmm2, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movsd -0x20(%rbp), %xmm0
callq 0x2120
movsd %xmm0, -0x38(%rbp)
movq -0x28(%rbp), %rcx
xorl %eax, %eax
cmpq 0x8(%rcx), %rax
jbe 0x26b6
jmp 0x25ce
movq $0x0, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq -0x28(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x25fa
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x1, -0x40(%rbp)
jae 0x2609
movq $0x1, -0x40(%rbp)
movq $0x8, -0x50(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
je 0x26b2
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x40(%rbp), %rsi
imulq -0x50(%rbp), %rsi
callq 0x2200
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x266d
movq 0xf9a6(%rip), %rax      # 0x11ff8
movq (%rax), %rdi
leaq 0xbaa1(%rip), %rsi      # 0xe0fd
movb $0x0, %al
callq 0x2170
movl $0x1, %edi
callq 0x2250
movq -0x40(%rbp), %rax
cmpq -0x48(%rbp), %rax
jbe 0x26a6
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
imulq -0x50(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x48(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rdi
movq -0x58(%rbp), %rdx
xorl %esi, %esi
callq 0x2100
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x26b4
jmp 0x26b6
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movsd -0x10(%rbp), %xmm0
movsd -0x18(%rbp), %xmm1
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
leaq -0x70(%rbp), %rdx
callq 0x2be0
cvtsi2sdq -0x30(%rbp), %xmm0
movsd 0xb936(%rip), %xmm1     # 0xe020
mulsd %xmm0, %xmm1
cvtsi2sdq -0x30(%rbp), %xmm0
mulsd %xmm0, %xmm1
movsd 0xb928(%rip), %xmm0     # 0xe028
divsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
cvtsi2sdq -0x30(%rbp), %xmm0
movsd 0xb909(%rip), %xmm1     # 0xe020
mulsd %xmm0, %xmm1
movsd 0xb8f5(%rip), %xmm0     # 0xe018
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x60(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2759
jp 0x2759
movsd -0x68(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2759
jp 0x2759
jmp 0x2770
movsd -0x68(%rbp), %xmm0
movsd -0x60(%rbp), %xmm1
callq 0x21e0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
callq 0x2120
movsd %xmm0, -0x90(%rbp)
movsd -0x60(%rbp), %xmm0
movsd -0x60(%rbp), %xmm2
movsd -0x68(%rbp), %xmm1
mulsd -0x68(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x70(%rbp), %xmm0
callq 0x2240
movsd %xmm0, -0xa0(%rbp)
movsd -0xa0(%rbp), %xmm0
addsd -0x20(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa0(%rbp), %xmm0
subsd -0x20(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xa8(%rbp), %xmm0
movsd 0xb83b(%rip), %xmm1     # 0xe030
ucomisd %xmm1, %xmm0
jb 0x280d
movsd 0xb825(%rip), %xmm0     # 0xe028
movsd %xmm0, -0xb8(%rbp)
jmp 0x2822
movsd -0xa8(%rbp), %xmm0
callq 0x2220
movsd %xmm0, -0xb8(%rbp)
movq -0x8(%rbp), %rdi
movsd -0xb8(%rbp), %xmm0
callq 0x2ca0
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rsi
subq $0x1, %rsi
movl $0x1, %edi
callq 0x2e00
movq %rax, -0xc0(%rbp)
movsd 0xb7da(%rip), %xmm0     # 0xe038
ucomisd -0xb0(%rbp), %xmm0
jb 0x287a
movsd 0xb7d0(%rip), %xmm0     # 0xe040
movsd %xmm0, -0xc8(%rbp)
jmp 0x288f
movsd -0xb0(%rbp), %xmm0
callq 0x2220
movsd %xmm0, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
movsd -0xc8(%rbp), %xmm0
callq 0x2ca0
movq %rax, -0xd0(%rbp)
movq -0x30(%rbp), %rdi
shlq $0x2, %rdi
subq $0x1, %rdi
movq -0xd0(%rbp), %rsi
addq $0x1, %rsi
callq 0x2e30
movq %rax, -0xd0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jg 0x2b71
movq -0xe0(%rbp), %rax
movq -0x30(%rbp), %rcx
subq $0x1, %rcx
cmpq %rcx, %rax
jg 0x2955
movq -0xe0(%rbp), %rax
imulq -0xe0(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd -0x78(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0xb6e1(%rip), %xmm1     # 0xe028
addsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
jmp 0x29d3
movq -0xe0(%rbp), %rax
imulq $0x3, -0x30(%rbp), %rcx
cmpq %rcx, %rax
jg 0x2988
movq -0x30(%rbp), %rax
shlq %rax
subq -0xe0(%rbp), %rax
cvtsi2sd %rax, %xmm0
mulsd -0x80(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
jmp 0x29d1
movq -0x30(%rbp), %rax
shlq $0x2, %rax
subq -0xe0(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0xd8(%rbp), %xmm0
mulsd -0xd8(%rbp), %xmm0
movsd -0x78(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xb67b(%rip), %xmm1     # 0xe040
addsd %xmm1, %xmm0
movsd %xmm0, -0xe8(%rbp)
jmp 0x29d3
movsd -0x38(%rbp), %xmm1
movsd -0xe8(%rbp), %xmm0
movsd -0x70(%rbp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xf0(%rbp)
movsd -0xe8(%rbp), %xmm0
movsd -0xe8(%rbp), %xmm1
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm1, %xmm0
movsd 0xb5e9(%rip), %xmm1     # 0xe028
addsd %xmm1, %xmm0
movsd %xmm0, -0xf8(%rbp)
movsd -0x60(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2a96
jp 0x2a96
movsd -0x68(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x2a96
jp 0x2a96
movsd 0xb5d5(%rip), %xmm0     # 0xe048
movsd %xmm0, -0x100(%rbp)
movsd -0xf0(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2a91
jmp 0x2b58
jmp 0x2b34
movsd -0xf0(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0x98(%rbp), %xmm0
mulsd -0xf8(%rbp), %xmm0
callq 0x2270
movaps %xmm0, %xmm1
movsd -0x110(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0x108(%rbp)
movsd -0x108(%rbp), %xmm1
movaps 0xb5cf(%rip), %xmm0     # 0xe0b0
pand %xmm0, %xmm1
movsd 0xb53b(%rip), %xmm0     # 0xe028
ucomisd %xmm1, %xmm0
jb 0x2b0a
movsd -0x108(%rbp), %xmm0
callq 0x2130
movsd %xmm0, -0x100(%rbp)
jmp 0x2b32
movsd -0x90(%rbp), %xmm1
movsd -0x108(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2b22
jmp 0x2b58
movsd 0xb51e(%rip), %xmm0     # 0xe048
movsd %xmm0, -0x100(%rbp)
jmp 0x2b34
movq -0x8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
movsd -0x88(%rbp), %xmm0
movsd -0x100(%rbp), %xmm1
movq -0x28(%rbp), %rdx
callq 0x2e60
jmp 0x2b5a
movq -0xe0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xe0(%rbp)
jmp 0x28e3
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopw (%rax,%rax)