// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %r8, %rbx
movq %rcx, %r12
movq %rdx, %r15
movsd %xmm3, (%rsp)
movsd %xmm2, 0x28(%rsp)
movl %esi, %r14d
movl %edi, %ebp
movsd %xmm0, 0x10(%rsp)
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x80a0
movsd %xmm0, 0x38(%rsp)
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
cmpl $0x1, %ebp
movsd %xmm1, 0x8(%rsp)
jne 0x245b3
movsd 0xcfa7a(%rip), %xmm0    # 0xf3fb8
mulsd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movapd %xmm1, %xmm0
jbe 0x2457c
leaq 0x18(%rsp), %rdx
leaq 0x24(%rsp), %rcx
movl $0x1, %edi
movl %r14d, %esi
movsd (%rsp), %xmm1
callq 0x24806
movsd 0x10(%rsp), %xmm0
testl %eax, %eax
je 0x247f9
movsd 0xccd14(%rip), %xmm1    # 0xf1298
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %esi
leaq 0x18(%rsp), %rdx
movl %r14d, %edi
movsd 0x28(%rsp), %xmm0
movsd (%rsp), %xmm1
callq 0x24aaa
movsd (%rsp), %xmm5
movsd 0xcccaf(%rip), %xmm1    # 0xf1260
jmp 0x245f8
leaq 0x18(%rsp), %r13
leaq 0x24(%rsp), %rax
movl %ebp, %edi
movl %r14d, %esi
movsd 0x10(%rsp), %xmm0
movsd (%rsp), %xmm1
movq %r13, %rdx
movq %rax, %rbp
movq %rax, %rcx
callq 0x24806
xorps %xmm1, %xmm1
cvtsi2sdl (%rbp), %xmm1
movsd (%r13), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%r13)
movsd (%rsp), %xmm5
movslq %r14d, %r8
movsd %xmm1, (%rbx,%r8,8)
movsd 0x8(%rsp), %xmm6
mulsd %xmm5, %xmm6
mulsd %xmm1, %xmm6
subsd 0x18(%rsp), %xmm6
leal 0x7(%r14), %ecx
testl %r14d, %r14d
cmovnsl %r14d, %ecx
andl $-0x8, %ecx
movl %r14d, %edi
subl %ecx, %edi
testl %edi, %edi
movq %r8, 0x48(%rsp)
jle 0x24677
movslq %ecx, %rdx
leal 0x1(,%r14,2), %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
mulsd %xmm5, %xmm0
mulsd %xmm1, %xmm0
subsd %xmm6, %xmm0
mulsd (%r12,%r8,8), %xmm0
movapd %xmm1, %xmm6
mulsd (%r15,%r8,8), %xmm6
movsd %xmm0, -0x8(%rbx,%r8,8)
decq %r8
addl $-0x2, %esi
movapd %xmm0, %xmm1
cmpq %rdx, %r8
jg 0x2463c
jmp 0x2467b
movapd %xmm1, %xmm0
testl %ecx, %ecx
movq %rbx, 0x40(%rsp)
movq %r14, 0x30(%rsp)
jle 0x247a6
movl %ecx, %ecx
movl %edi, %eax
notl %eax
addl %r14d, %eax
movslq %eax, %r13
leal (%r14,%r14), %esi
addl %edi, %edi
subl %edi, %esi
incl %esi
leaq (%r12,%rcx,8), %rdi
leaq (%r15,%rcx,8), %r8
leaq (%rbx,%rcx,8), %r9
addq $-0x8, %r9
leaq (%rbx,%r13,8), %rax
decq %r13
xorl %r12d, %r12d
movapd 0xccb7b(%rip), %xmm1    # 0xf1240
movsd 0xcfdbb(%rip), %xmm2    # 0xf4488
movsd 0xcfdbb(%rip), %xmm3    # 0xf4490
movq %rcx, %r15
movq %r15, %rdx
addq $-0x8, %r15
movq %rax, %r10
movq %r13, (%rsp)
movq %rdx, 0x50(%rsp)
movl %edx, %ebp
movl %esi, 0x8(%rsp)
movq %r12, %r11
movapd %xmm0, %xmm4
movq %r13, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
mulsd %xmm5, %xmm0
mulsd %xmm4, %xmm0
subsd %xmm6, %xmm0
mulsd (%rdi,%r11,8), %xmm0
movq %r10, %r14
movapd %xmm4, %xmm6
mulsd (%r8,%r11,8), %xmm6
movsd %xmm0, (%r9,%r11,8)
addl $-0x2, %esi
decl %ebp
leaq (%rcx,%r11), %rbx
decq %rbx
decq %r11
decq %r13
addq $-0x8, %r10
cmpq %r15, %rbx
jg 0x246f4
movapd %xmm0, %xmm4
andpd %xmm1, %xmm4
ucomisd %xmm2, %xmm4
jbe 0x24783
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm6
cmpl 0x30(%rsp), %ebp
movq (%rsp), %r13
jg 0x24787
movq 0x48(%rsp), %rsi
movsd (%r14), %xmm4
mulsd %xmm3, %xmm4
movsd %xmm4, (%r14)
incq %rdx
addq $0x8, %r14
cmpq %rsi, %rdx
jl 0x24767
jmp 0x24787
movq (%rsp), %r13
movl 0x8(%rsp), %esi
addq $-0x8, %r12
addl $-0x10, %esi
addq $-0x8, %r13
addq $-0x40, %rax
cmpq $0x8, 0x50(%rsp)
jg 0x246d8
movq 0x30(%rsp), %rdx
testl %edx, %edx
movq 0x40(%rsp), %rcx
movsd 0x38(%rsp), %xmm1
js 0x247e8
movsd 0x10(%rsp), %xmm2
mulsd 0x28(%rsp), %xmm2
divsd %xmm2, %xmm1
divsd %xmm0, %xmm1
incl %edx
xorl %eax, %eax
movsd (%rcx,%rax,8), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x247d2
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0xcca5f(%rip), %xmm1    # 0xf1260
jmp 0x245f3