// File: /workspace/github_c_makefile_star2_O2/PoulinV[P]AxiCLASS/build/../tools/hyperspherical.c
// Function: hyperspherical_backwards_recurrence
int hyperspherical_backwards_recurrence(int K,
                                        int lmax,
                                        double beta,
                                        double x,
                                        double sinK,
                                        double cotK,
                                        double * __restrict__ sqrtK,
                                        double * __restrict__ one_over_sqrtK,
                                        double * __restrict__ PhiL){
  double phi0, phi1, phipr1, phi, phi_plus_1_times_sqrtK, phi_minus_1, scaling;
  int l, k, isign;
  int funcreturn = _FAILURE_;
  phi0 = sin(beta*x)/(beta*sinK);

  //printf("in backwards. x = %g\n",x);
  if (K==1){
    if (beta > 1.5*lmax) {
      funcreturn = get_CF1(K,lmax,beta,cotK, &phipr1, &isign);
    }
    if (funcreturn == _FAILURE_) {
      CF1_from_Gegenbauer(lmax,(int) (beta+0.2),sinK,cotK, &phipr1);
    }
    phi1 = 1.0;
  }
  else{
    get_CF1(K,lmax,beta,cotK, &phipr1, &isign);
    phi1 = isign;
    phipr1 *=phi1;
    //printf("isign = %d, phi1 = %g, phipr1 = %g\n",isign,phi1,phipr1);
  }

  PhiL[lmax] = phi1;
  phi = phi1;
  //  phi_plus_1 = 1/sqrtK[lmax+1]*(lmax*cotK*phi1-phipr1);
  phi_plus_1_times_sqrtK = lmax*cotK*phi1-phipr1;


  int l_ini, l_align;
  l_align = lmax-lmax%_HYPER_BLOCK_;

  // Bring l down to _HYPER_BLOCK_ aligned region:
  for (l=lmax; l>l_align; l--){
    //    phi_minus_1 = ( (2*l+1)*cotK*phi-phi_plus_1_times_sqrtK )/sqrtK[l];
    phi_minus_1 = ( (2*l+1)*cotK*phi-phi_plus_1_times_sqrtK )*one_over_sqrtK[l];
    phi_plus_1_times_sqrtK = phi*sqrtK[l];
    phi = phi_minus_1;
    PhiL[l-1] = phi;
  }
  for (l_ini=l_align; l_ini>0; l_ini -= _HYPER_BLOCK_){
    for (l=l_ini; l>(l_ini-_HYPER_BLOCK_); l--){
      //    phi_minus_1 = ( (2*l+1)*cotK*phi-phi_plus_1_times_sqrtK )/sqrtK[l];
      phi_minus_1 = ( (2*l+1)*cotK*phi-phi_plus_1_times_sqrtK )*one_over_sqrtK[l];
      phi_plus_1_times_sqrtK = phi*sqrtK[l];
      phi = phi_minus_1;
      PhiL[l-1] = phi;
    }
    if (fabs(phi)>_HYPER_OVERFLOW_){
      phi *= _ONE_OVER_HYPER_OVERFLOW_;
      phi_plus_1_times_sqrtK *= _ONE_OVER_HYPER_OVERFLOW_;
      //Rescale whole Phi vector until this point:
      for (k=l; k<=lmax; k++)
        PhiL[k] *=_ONE_OVER_HYPER_OVERFLOW_;
    }
  }

  /**
  for (l=lmax; l>=1; l--){
    //    phi_minus_1 = ( (2*l+1)*cotK*phi-phi_plus_1_times_sqrtK )/sqrtK[l];
    phi_minus_1 = ( (2*l+1)*cotK*phi-phi_plus_1_times_sqrtK )*one_over_sqrtK[l];
    phi_plus_1_times_sqrtK = phi*sqrtK[l];
    phi = phi_minus_1;
    PhiL[l-1] = phi;
    if (fabs(phi)>_HYPER_OVERFLOW_){
      //
      phi *= _ONE_OVER_HYPER_OVERFLOW_;
      phi_plus_1_times_sqrtK *= _ONE_OVER_HYPER_OVERFLOW_;
      //Rescale whole Phi vector until this point:
      for (k=l-1; k<=lmax; k++)
        PhiL[k] *=_ONE_OVER_HYPER_OVERFLOW_;
    }
  }
  */
  scaling = phi0/phi;
  for (k=0; k<=lmax; k++)
    PhiL[k] *= scaling;
  return _SUCCESS_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %rbx
movq %rcx, %r14
movq %rdx, %r15
movsd %xmm3, 0x8(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
movl %esi, %r12d
movl %edi, %ebp
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x80a0
movsd 0x28(%rsp), %xmm2
movsd %xmm0, 0x40(%rsp)
movapd %xmm2, %xmm0
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
cmpl $0x1, %ebp
jne 0x2208e
cvtsi2sd %r12d, %xmm1
movsd 0xc5f72(%rip), %xmm0    # 0xe7f88
movsd %xmm1, 0x20(%rsp)
mulsd %xmm1, %xmm0
ucomisd %xmm0, %xmm2
jbe 0x22053
pushq $0x1
popq %rdi
leaq 0x10(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
movl %r12d, %esi
movapd %xmm2, %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x2225e
movsd 0x28(%rsp), %xmm2
testl %eax, %eax
je 0x2224b
addsd 0xc323d(%rip), %xmm2    # 0xe5298
cvttsd2si %xmm2, %esi
leaq 0x10(%rsp), %rdx
movl %r12d, %edi
movsd 0x30(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
callq 0x224ca
movsd 0x8(%rsp), %xmm6
movsd 0xc31da(%rip), %xmm0    # 0xe5260
movsd 0x20(%rsp), %xmm7
jmp 0x220d5
leaq 0x10(%rsp), %r13
leaq 0x1c(%rsp), %rax
movl %ebp, %edi
movl %r12d, %esi
movapd %xmm2, %xmm0
movsd 0x8(%rsp), %xmm1
movq %r13, %rdx
movq %rax, %rbp
movq %rax, %rcx
callq 0x2225e
cvtsi2sdl (%rbp), %xmm0
movsd (%r13), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r13)
cvtsi2sd %r12d, %xmm7
movsd 0x8(%rsp), %xmm6
movsd 0x40(%rsp), %xmm5
divsd 0x38(%rsp), %xmm5
movslq %r12d, %rcx
movsd %xmm0, (%rbx,%rcx,8)
mulsd %xmm6, %xmm7
mulsd %xmm0, %xmm7
subsd 0x10(%rsp), %xmm7
pushq $0x8
popq %rsi
movl %r12d, %eax
cltd
idivl %esi
movl %r12d, %eax
subl %edx, %eax
cltq
leal 0x1(,%r12,2), %esi
movq %rcx, %rdi
cmpq %rax, %rdi
jle 0x22149
cvtsi2sd %esi, %xmm1
mulsd %xmm6, %xmm1
mulsd %xmm0, %xmm1
subsd %xmm7, %xmm1
mulsd (%r14,%rdi,8), %xmm1
mulsd (%r15,%rdi,8), %xmm0
movsd %xmm1, -0x8(%rbx,%rdi,8)
decq %rdi
addl $-0x2, %esi
movapd %xmm0, %xmm7
movapd %xmm1, %xmm0
jmp 0x22112
movl %edx, %eax
negl %eax
movslq %eax, %rsi
addq %rcx, %rsi
addl %r12d, %r12d
subl %edx, %r12d
subl %edx, %r12d
incl %r12d
leaq (,%rcx,8), %rax
movslq %edx, %rdx
shlq $0x3, %rdx
subq %rdx, %rax
addq %rbx, %rax
movapd 0xc30c4(%rip), %xmm1    # 0xe5240
movsd 0xc62d4(%rip), %xmm2    # 0xe8458
movsd 0xc62d4(%rip), %xmm3    # 0xe8460
testq %rsi, %rsi
jle 0x2221c
leaq -0x8(%rsi), %rdx
movq %rax, %rdi
movl %r12d, %r8d
cmpq %rdx, %rsi
jle 0x221dc
cvtsi2sd %r8d, %xmm4
mulsd %xmm6, %xmm4
mulsd %xmm0, %xmm4
subsd %xmm7, %xmm4
mulsd (%r14,%rsi,8), %xmm4
mulsd (%r15,%rsi,8), %xmm0
movsd %xmm4, -0x8(%rbx,%rsi,8)
decq %rsi
addl $-0x2, %r8d
addq $-0x8, %rdi
movapd %xmm0, %xmm7
movapd %xmm4, %xmm0
jmp 0x2219f
movapd %xmm0, %xmm4
andpd %xmm1, %xmm4
ucomisd %xmm2, %xmm4
jbe 0x2220c
mulsd %xmm3, %xmm0
cmpq %rcx, %rsi
jg 0x22208
movsd (%rdi), %xmm4
mulsd %xmm3, %xmm4
movsd %xmm4, (%rdi)
incq %rsi
addq $0x8, %rdi
jmp 0x221ee
mulsd %xmm3, %xmm7
addl $-0x10, %r12d
addq $-0x40, %rax
movq %rdx, %rsi
jmp 0x2218c
divsd %xmm0, %xmm5
xorl %eax, %eax
cmpq %rcx, %rax
jg 0x2223a
movsd (%rbx,%rax,8), %xmm0
mulsd %xmm5, %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
jmp 0x22222
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0xc300d(%rip), %xmm0    # 0xe5260
movsd 0x8(%rsp), %xmm6
jmp 0x22086