// File: /workspace/github_c_makefile_star2_O2/esheldon[P]xshear/src/healpix.c
// Function: hpix_in_ring
void hpix_in_ring(
        const HealPix* hpix, 
        int64 iz, 
        double phi0, 
        double dphi, 
        lvector* plist) {

    int64 nr, ir, ipix1;
    double shift=0.5;
    int64 nside = hpix->nside;

    if (iz<nside) {
        // north pole
        ir = iz;
        nr = ir*4;
        ipix1 = 2*ir*(ir-1);        //    lowest pixel number in the ring
    } else if (iz>(3*nside)) {
        // south pole
        ir = 4*nside - iz;
        nr = ir*4;
        ipix1 = hpix->npix - 2*ir*(ir+1); // lowest pixel number in the ring
    } else {
        // equatorial region
        ir = iz - nside + 1;           //    within {1, 2*nside + 1}
        nr = nside*4;
        if ((ir&1)==0) shift = 0;
        ipix1 = hpix->ncap + (ir-1)*nr; // lowest pixel number in the ring
    }

    int64 ipix2 = ipix1 + nr - 1;  //    highest pixel number in the ring
 

    if (dphi > (M_PI-1e-7)) {
        for (int64 i=ipix1; i<=ipix2; ++i) {
            vector_push(plist, i);
        }
    } else {

        // M_1_PI is 1/pi
        int64 ip_lo = (int64)( floor(nr*.5*M_1_PI*(phi0-dphi) - shift) )+1;
        int64 ip_hi = (int64)( floor(nr*.5*M_1_PI*(phi0+dphi) - shift) );
        int64 pixnum = ip_lo+ipix1;
        if (pixnum<ipix1) {
            pixnum += nr;
        }
        for (int64 i=ip_lo; i<=ip_hi; ++i, ++pixnum) {
            if (pixnum>ipix2) {
                pixnum -= nr;
            }
            vector_push(plist, pixnum);
        }
    }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, %rbx
movq (%rdi), %rcx
movq %rsi, %r13
subq %rcx, %r13
jge 0x2a6d
leaq (,%rsi,4), %rcx
leaq -0x1(%rsi), %r13
imulq %rsi, %r13
addq %r13, %r13
jmp 0x2a96
leaq (%rcx,%rcx,2), %rax
shlq $0x2, %rcx
cmpq %rsi, %rax
jge 0x2aa0
subq %rsi, %rcx
movq %rcx, %r13
notq %r13
imulq %rcx, %r13
leaq (,%rcx,4), %rcx
addq %r13, %r13
addq 0x8(%rdi), %r13
movsd 0x65d2(%rip), %xmm2     # 0x9070
jmp 0x2abc
testb $0x1, %r13b
je 0x2aac
xorpd %xmm2, %xmm2
jmp 0x2ab4
movsd 0x65bc(%rip), %xmm2     # 0x9070
imulq %rcx, %r13
addq 0x18(%rdi), %r13
leaq (%rcx,%r13), %rdx
ucomisd 0x65b0(%rip), %xmm1     # 0x9078
movq %rdx, 0x18(%rsp)
jbe 0x2b77
pushq $0x1
popq %r12
cmpq %rdx, %r13
jge 0x2cdd
movq (%rbx), %r15
cmpq 0x8(%rbx), %r15
jne 0x2b1b
leaq (%r15,%r15), %rcx
testq %r15, %r15
movq %rcx, %rbp
cmoveq %r12, %rbp
movq %r15, %rax
cmpq %r15, %rbp
jae 0x2b05
movq %rcx, (%rbx)
movq %rbp, %rax
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq 0x10(%rbx), %r14
cmpq %r15, %rbp
jne 0x2b21
movq %rax, %r15
jmp 0x2b64
movq 0x10(%rbx), %r14
jmp 0x2b64
leaq (,%rbp,8), %rsi
movq %r14, %rdi
callq 0x2230
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x2cec
movq %rax, %r14
movq %rbp, %rdx
subq %r15, %rdx
jbe 0x2b58
shlq $0x3, %rdx
leaq (%r14,%r15,8), %rdi
xorl %esi, %esi
callq 0x2120
movq %rbp, 0x8(%rbx)
movq (%rbx), %r15
movq 0x18(%rsp), %rdx
leaq 0x1(%r15), %rax
movq %rax, (%rbx)
movq %r13, (%r14,%r15,8)
incq %r13
jmp 0x2ad7
cvtsi2sd %rcx, %xmm3
mulsd 0x64ec(%rip), %xmm3     # 0x9070
mulsd 0x64f4(%rip), %xmm3     # 0x9080
movsd %xmm3, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
subsd %xmm1, %xmm0
mulsd %xmm3, %xmm0
subsd %xmm2, %xmm0
movq %rcx, 0x20(%rsp)
movsd %xmm1, 0x28(%rsp)
movsd %xmm2, 0x30(%rsp)
callq 0x2260
cvttsd2si %xmm0, %r14
leaq 0x1(%r14), %r15
movsd 0x10(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
callq 0x2260
movq %r15, %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
cvttsd2si %xmm0, %r8
leaq (%r14,%r13), %rax
incq %rax
xorl %r9d, %r9d
cmpq $-0x1, %r14
movl $0x0, %r13d
cmovlq %rsi, %r13
addq %rax, %r13
pushq $0x1
popq %r10
movq %r8, 0x8(%rsp)
cmpq %r8, %rdi
jg 0x2cdd
cmpq %rdx, %r13
movq %rsi, %r12
cmovlq %r9, %r12
movq (%rbx), %r15
cmpq 0x8(%rbx), %r15
jne 0x2c64
leaq (%r15,%r15), %rcx
testq %r15, %r15
movq %rcx, %rbp
cmoveq %r10, %rbp
movq %r15, %rax
cmpq %r15, %rbp
jae 0x2c4e
movq %rcx, (%rbx)
movq %rbp, %rax
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq 0x10(%rbx), %r14
cmpq %r15, %rbp
jne 0x2c6a
movq %rax, %r15
jmp 0x2cc4
movq 0x10(%rbx), %r14
jmp 0x2cc4
movq %rdi, 0x10(%rsp)
leaq (,%rbp,8), %rsi
movq %r14, %rdi
callq 0x2230
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x2cec
movq %rax, %r14
movq %rbp, %rdx
subq %r15, %rdx
jbe 0x2ca2
shlq $0x3, %rdx
leaq (%r14,%r15,8), %rdi
xorl %esi, %esi
callq 0x2120
movq %rbp, 0x8(%rbx)
movq (%rbx), %r15
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %r8
xorl %r9d, %r9d
pushq $0x1
popq %r10
subq %r12, %r13
leaq 0x1(%r15), %rax
movq %rax, (%rbx)
movq %r13, (%r14,%r15,8)
incq %rdi
incq %r13
jmp 0x2c16
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb305(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x63f4(%rip), %rdi      # 0x90f1
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
pushq $0x1
popq %rdi
callq 0x2280