// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/nma.c
// Function: Convert_cart2torsion
int Convert_cart2torsion(struct Tors *D, 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

  double sum=0; int i, j, a;
  if(J->Jtilde_ar==NULL)Compute_Jtilde(J);

  // Compute Masswtd_Tors_dev = Jtilde M Delta_r
  for(a=0; a<J->N_axes; a++){
    float *Jt=J->Jtilde_ar[a], *r=D->Cart, *m=Ref.mass_coord;
    sum=0; for(j=0; j<J->N_Cart; j++)sum+=m[j]*r[j]*Jt[j];
    D->MW_Tors[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]*D->MW_Tors[i];
      D->Tors[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--){
      D->Tors[i]=D->MW_Tors[i];
      for(j=i+1; j<J->N_axes; j++)D->Tors[i]-=D->Tors[j]*J->T_sqrt[j][i];
      D->Tors[i]/=J->T_sqrt[i][i];
    }
  }
  return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
cmpq $0x0, 0x18(%rsi)
jne 0x26693
movq %rbx, %rdi
callq 0x230a8
movslq (%rbx), %rax
testq %rax, %rax
jle 0x26700
leaq 0x30(%rsp), %rdi
movq 0x18(%rbx), %rcx
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rsi
movq 0x18(%rdi), %rdi
movl 0x4(%rbx), %r8d
xorl %r9d, %r9d
testl %r8d, %r8d
jle 0x266ef
movq (%rcx,%r9,8), %r10
xorpd %xmm0, %xmm0
xorl %r11d, %r11d
movss (%rdi,%r11,4), %xmm1
mulss (%rdx,%r11,4), %xmm1
mulss (%r10,%r11,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r11
cmpq %r11, %r8
jne 0x266c7
cvtsd2ss %xmm0, %xmm0
jmp 0x266f2
xorps %xmm0, %xmm0
movss %xmm0, (%rsi,%r9,4)
incq %r9
cmpq %rax, %r9
jne 0x266b7
movq 0x30(%rbx), %rcx
testq %rcx, %rcx
je 0x26757
testl %eax, %eax
jle 0x26803
movq 0x10(%r14), %rdx
movq 0x18(%r14), %rsi
xorl %edi, %edi
xorps %xmm0, %xmm0
xorl %r8d, %r8d
movq (%rcx,%r8,8), %r9
movss (%r9,%rdi,4), %xmm1
mulss (%rdx,%r8,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r8
cmpq %r8, %rax
jne 0x26721
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rsi,%rdi,4)
incq %rdi
cmpq %rax, %rdi
jne 0x2671b
jmp 0x26803
testl %eax, %eax
jle 0x26803
movq 0x10(%r14), %rcx
movq 0x18(%r14), %rdx
movq 0x28(%rbx), %rsi
leaq (,%rax,8), %rdi
leaq (%rdx,%rax,4), %r8
movq %rax, %r9
leaq -0x1(%r9), %r10
movss -0x4(%rcx,%r9,4), %xmm0
movss %xmm0, -0x4(%rdx,%r9,4)
cmpq %rax, %r9
jge 0x267d1
movss (%rdx,%r10,4), %xmm0
movq 0x28(%rbx), %r11
addq %rdi, %r11
xorl %r14d, %r14d
xorps %xmm1, %xmm1
cvtss2sd (%r8,%r14,4), %xmm1
movq (%r11,%r14,8), %r15
mulsd (%r15,%r10,8), %xmm1
cvtss2sd %xmm0, %xmm0
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rdx,%r10,4)
incq %r14
leal (%r14,%r9), %ebp
cmpl %ebp, %eax
jg 0x267a1
movq (%rsi,%r10,8), %r11
xorps %xmm0, %xmm0
cvtss2sd (%rdx,%r10,4), %xmm0
divsd (%r11,%r10,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rdx,%r10,4)
addq $-0x8, %rdi
addq $-0x4, %r8
cmpq $0x1, %r9
movq %r10, %r9
jg 0x2677a
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq