// File: /workspace/github_c_makefile_star2_O3/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, %r8
movq (%rdi), %rcx
movq %rsi, %r13
subq %rcx, %r13
jge 0x43e1
leaq (,%rsi,4), %rcx
leaq -0x1(%rsi), %r13
imulq %rsi, %r13
addq %r13, %r13
jmp 0x440a
leaq (%rcx,%rcx,2), %rax
shlq $0x2, %rcx
cmpq %rsi, %rax
jge 0x4414
subq %rsi, %rcx
movq %rcx, %r13
notq %r13
imulq %rcx, %r13
leaq (,%rcx,4), %rcx
addq %r13, %r13
addq 0x8(%rdi), %r13
movsd 0x4c5e(%rip), %xmm2     # 0x9070
jmp 0x4430
testb $0x1, %r13b
je 0x4420
xorpd %xmm2, %xmm2
jmp 0x4428
movsd 0x4c48(%rip), %xmm2     # 0x9070
imulq %rcx, %r13
addq 0x18(%rdi), %r13
leaq (%rcx,%r13), %rdx
ucomisd 0x4c3c(%rip), %xmm1     # 0x9078
movq %r8, (%rsp)
movq %rdx, 0x8(%rsp)
jbe 0x4502
testq %rcx, %rcx
jle 0x465c
movl $0x1, %r12d
movq (%r8), %r15
cmpq 0x8(%r8), %r15
jne 0x4495
leaq (%r15,%r15), %rcx
testq %r15, %r15
movq %rcx, %rbp
cmoveq %r12, %rbp
movq %r15, %rax
cmpq %r15, %rbp
jae 0x447f
movq %rcx, (%r8)
movq %rbp, %rax
cmpq $0x1, %rbp
adcq $0x0, %rbp
movq 0x10(%r8), %r14
cmpq %r15, %rbp
jne 0x449b
movq %rax, %r15
jmp 0x44e6
movq 0x10(%r8), %r14
jmp 0x44e6
leaq (,%rbp,8), %rsi
movq %r14, %rdi
callq 0x2230
movq (%rsp), %r8
movq %rax, 0x10(%r8)
testq %rax, %rax
je 0x466b
movq %rax, %r14
movq %rbp, %rdx
subq %r15, %rdx
jbe 0x44da
shlq $0x3, %rdx
leaq (%r14,%r15,8), %rdi
xorl %esi, %esi
callq 0x2120
movq (%rsp), %r8
movq %rbp, 0x8(%r8)
movq (%r8), %r15
movq 0x8(%rsp), %rdx
leaq 0x1(%r15), %rax
movq %rax, (%r8)
movq %r13, (%r14,%r15,8)
incq %r13
cmpq %rdx, %r13
jl 0x445a
jmp 0x465c
cvtsi2sd %rcx, %xmm3
mulsd 0x4b61(%rip), %xmm3     # 0x9070
mulsd 0x4b69(%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, %rbx
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), %rdx
movq (%rsp), %rsi
cvttsd2si %xmm0, %rbp
subq %rbx, %rbp
jle 0x465c
addq %rbx, %r13
xorl %r8d, %r8d
cmpq $-0x1, %rbx
movl $0x0, %eax
cmovlq %rdi, %rax
addq %rax, %r13
incq %r13
movl $0x1, %r9d
cmpq %rdx, %r13
movq %rdi, %r12
cmovlq %r8, %r12
movq (%rsi), %r15
cmpq 0x8(%rsi), %r15
jne 0x45e7
leaq (%r15,%r15), %rcx
testq %r15, %r15
movq %rcx, %rbx
cmoveq %r9, %rbx
movq %r15, %rax
cmpq %r15, %rbx
jae 0x45d1
movq %rcx, (%rsi)
movq %rbx, %rax
cmpq $0x1, %rbx
adcq $0x0, %rbx
movq 0x10(%rsi), %r14
cmpq %r15, %rbx
jne 0x45ed
movq %rax, %r15
jmp 0x4642
movq 0x10(%rsi), %r14
jmp 0x4642
leaq (,%rbx,8), %rsi
movq %r14, %rdi
callq 0x2230
movq (%rsp), %rsi
movq %rax, 0x10(%rsi)
testq %rax, %rax
je 0x4670
movq %rax, %r14
movq %rbx, %rdx
subq %r15, %rdx
jbe 0x4628
shlq $0x3, %rdx
leaq (%r14,%r15,8), %rdi
xorl %esi, %esi
callq 0x2120
movq (%rsp), %rsi
movq %rbx, 0x8(%rsi)
movq (%rsi), %r15
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
movl $0x1, %r9d
subq %r12, %r13
leaq 0x1(%r15), %rax
movq %rax, (%rsi)
movq %r13, (%r14,%r15,8)
incq %r13
decq %rbp
jne 0x45a2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2333
callq 0x230e