// File: /workspace/github_c_makefile_star2_O1/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, %r12
subq %rcx, %r12
jge 0x4371
leaq (,%rsi,4), %rcx
leaq -0x1(%rsi), %r12
imulq %rsi, %r12
addq %r12, %r12
jmp 0x439a
leaq (%rcx,%rcx,2), %rax
shlq $0x2, %rcx
cmpq %rsi, %rax
jge 0x43a4
subq %rsi, %rcx
movq %rcx, %r12
notq %r12
imulq %rcx, %r12
leaq (,%rcx,4), %rcx
addq %r12, %r12
addq 0x8(%rdi), %r12
movsd 0x4cce(%rip), %xmm2     # 0x9070
jmp 0x43c0
testb $0x1, %r12b
je 0x43b0
xorpd %xmm2, %xmm2
jmp 0x43b8
movsd 0x4cb8(%rip), %xmm2     # 0x9070
imulq %rcx, %r12
addq 0x18(%rdi), %r12
leaq (%rcx,%r12), %rsi
ucomisd 0x4cac(%rip), %xmm1     # 0x9078
movq %rsi, 0x8(%rsp)
jbe 0x4477
testq %rcx, %rcx
jle 0x45b2
movl $0x1, %r15d
movq (%rbx), %rax
movq 0x8(%rbx), %rbp
cmpq %rbp, %rax
jne 0x4454
leaq (,%rbp,2), %r13
testq %rbp, %rbp
cmoveq %r15, %r13
cmpq %rax, %r13
jae 0x4409
movq %r13, (%rbx)
cmpq $0x1, %r13
adcq $0x0, %r13
cmpq %rbp, %r13
je 0x4454
movq 0x10(%rbx), %rdi
leaq (,%r13,8), %rsi
callq 0x2230
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x45c1
movq %r13, %rdx
subq %rbp, %rdx
jbe 0x444b
shlq $0x3, %rdx
leaq (%rax,%rbp,8), %rdi
xorl %esi, %esi
callq 0x2120
movq %r13, 0x8(%rbx)
movq 0x8(%rsp), %rsi
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r12, (%rcx,%rax,8)
incq %r12
cmpq %rsi, %r12
jl 0x43e6
jmp 0x45b2
cvtsi2sd %rcx, %xmm3
mulsd 0x4bec(%rip), %xmm3     # 0x9070
mulsd 0x4bf4(%rip), %xmm3     # 0x9080
movsd %xmm3, 0x28(%rsp)
movsd %xmm0, 0x30(%rsp)
subsd %xmm1, %xmm0
mulsd %xmm3, %xmm0
subsd %xmm2, %xmm0
movq %rcx, 0x10(%rsp)
movsd %xmm1, 0x18(%rsp)
movsd %xmm2, 0x20(%rsp)
callq 0x2260
cvttsd2si %xmm0, %r14
movsd 0x30(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
callq 0x2260
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
cvttsd2si %xmm0, %r13
subq %r14, %r13
jle 0x45b2
addq %r14, %r12
xorl %r8d, %r8d
cmpq $-0x1, %r14
movl $0x0, %eax
cmovlq %rdi, %rax
addq %rax, %r12
incq %r12
movl $0x1, %r9d
cmpq %rsi, %r12
movq %rdi, %r15
cmovlq %r8, %r15
movq (%rbx), %rax
movq 0x8(%rbx), %r14
cmpq %r14, %rax
jne 0x4591
leaq (%r14,%r14), %rbp
testq %r14, %r14
cmoveq %r9, %rbp
cmpq %rax, %rbp
jae 0x453c
movq %rbp, (%rbx)
cmpq $0x1, %rbp
adcq $0x0, %rbp
cmpq %r14, %rbp
je 0x4591
movq 0x10(%rbx), %rdi
leaq (,%rbp,8), %rsi
callq 0x2230
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x45c6
movq %rbp, %rdx
subq %r14, %rdx
jbe 0x457a
shlq $0x3, %rdx
leaq (%rax,%r14,8), %rdi
xorl %esi, %esi
callq 0x2120
movq %rbp, 0x8(%rbx)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
movl $0x1, %r9d
subq %r15, %r12
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movq %r12, (%rcx,%rax,8)
incq %r12
decq %r13
jne 0x4513
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2333
callq 0x230e