// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/nma.c
// Function: Convert_cart2torsion1
int Convert_cart2torsion1(float *Tors_dev,
			  float *Masswtd_Tors_dev,
			  float *Cart_dev,
			  struct Reference Ref,
			  struct Jacobian *J)
{
  // Formula: (LL^t)Delta_theta = JM Delta_r  with:
  // L=T_sqrt (lower diagonal matrix if obtained from Cholevsky decomposition)
  // T=JMJ^t = LL^t
  // J is the jacobian matrix dr_int_i/d_theta_a
  // M_i is the diagonal mass matrix.
  // ==> L^t Delta_theta = L^{-1} J M Delta_r = Jtilde M Delta_r
  // To solve the equation:
  // Compute x=Jtilde MDelta_r =Masswtd_Tors
  // solve x=L^t Delta_theta (by backward substitution if T_sqrt lower diagonal)
  // Return the torsional fraction,
  // sum_i m_i (J d_theta)^2/sum_i m_i Dr_i^2

  if(J->Jtilde_ar==NULL)Compute_Jtilde(J);

  double sum=0; int i, j, a;

  // Compute Masswtd_Tors_dev = Jtilde M Delta_r
  for(a=0; a<J->N_axes; a++){
    float *Jt=J->Jtilde_ar[a]; sum=0;
    float *r=Cart_dev, *m=Ref.mass_coord;
    for(j=0; j<J->N_Cart; j++)sum+=m[j]*r[j]*Jt[j];
    Masswtd_Tors_dev[a]=sum;
  }
  if(J->T_sqrt_inv){
    // T_sqrt is not lower diagonal!
    for(a=0; a<J->N_axes; a++){
      sum=0;
      for(i=0; i<J->N_axes; i++)sum+=J->T_sqrt_inv[i][a]*Masswtd_Tors_dev[i];
      Tors_dev[a]=sum;
    }
  }else{
    // Exploit that T_sqrt is lower diagonal
    // Backward_substitution
    // theta_i =(x_i - sum_j>i L_ji x_j)/L_ii
    for(i=J->N_axes-1; i>=0; i--){
      Tors_dev[i]=Masswtd_Tors_dev[i];
      for(j=i+1; j<J->N_axes; j++)Tors_dev[i]-=Tors_dev[j]*J->T_sqrt[j][i];
      Tors_dev[i]/=J->T_sqrt[i][i];
    }
  }
  return(0);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r14
cmpq $0x0, 0x18(%rcx)
jne 0x26516
movq %r15, %rdi
callq 0x230a8
movslq (%r15), %rax
testq %rax, %rax
jle 0x26578
leaq 0x30(%rsp), %rdx
movq 0x18(%r15), %rcx
movq 0x18(%rdx), %rdx
movl 0x4(%r15), %esi
xorl %edi, %edi
testl %esi, %esi
jle 0x26568
movq (%rcx,%rdi,8), %r8
xorpd %xmm0, %xmm0
xorl %r9d, %r9d
movss (%rdx,%r9,4), %xmm1
mulss (%r12,%r9,4), %xmm1
mulss (%r8,%r9,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r9
cmpq %r9, %rsi
jne 0x26540
cvtsd2ss %xmm0, %xmm0
jmp 0x2656b
xorps %xmm0, %xmm0
movss %xmm0, (%rbx,%rdi,4)
incq %rdi
cmpq %rax, %rdi
jne 0x26531
movq 0x30(%r15), %rcx
testq %rcx, %rcx
je 0x265c5
testl %eax, %eax
jle 0x26669
xorl %edx, %edx
xorps %xmm0, %xmm0
xorl %esi, %esi
movq (%rcx,%rsi,8), %rdi
movss (%rdi,%rdx,4), %xmm1
mulss (%rbx,%rsi,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %rax
jne 0x26590
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%rdx,4)
incq %rdx
cmpq %rax, %rdx
jne 0x2658b
jmp 0x26669
testl %eax, %eax
jle 0x26669
movq 0x28(%r15), %rcx
leaq (,%rax,8), %rdx
leaq (%r14,%rax,4), %rsi
movq %rax, %rdi
leaq -0x1(%rdi), %r8
movss -0x4(%rbx,%rdi,4), %xmm0
movss %xmm0, -0x4(%r14,%rdi,4)
cmpq %rax, %rdi
jge 0x26637
movss (%r14,%r8,4), %xmm0
movq 0x28(%r15), %r9
addq %rdx, %r9
xorl %r10d, %r10d
xorps %xmm1, %xmm1
cvtss2sd (%rsi,%r10,4), %xmm1
movq (%r9,%r10,8), %r11
mulsd (%r11,%r8,8), %xmm1
cvtss2sd %xmm0, %xmm0
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%r8,4)
incq %r10
leal (%r10,%rdi), %r11d
cmpl %r11d, %eax
jg 0x26606
movq (%rcx,%r8,8), %r9
xorps %xmm0, %xmm0
cvtss2sd (%r14,%r8,4), %xmm0
divsd (%r9,%r8,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r14,%r8,4)
addq $-0x8, %rdx
addq $-0x4, %rsi
cmpq $0x1, %rdi
movq %r8, %rdi
jg 0x265e0
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq