// File: /workspace/github_c_makefile_star2_O3/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 %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
cmpq $0x0, 0x18(%rsi)
jne 0x26224
movq %r14, %rdi
callq 0x22a0d
movslq (%r14), %rax
testq %rax, %rax
jle 0x2637e
leaq 0x20(%rsp), %rdi
movq 0x18(%r14), %rcx
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rsi
movq 0x18(%rdi), %rdi
movl 0x4(%r14), %r8d
xorl %r9d, %r9d
testl %r8d, %r8d
jle 0x26284
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 0x2625c
cvtsd2ss %xmm0, %xmm0
jmp 0x26287
xorps %xmm0, %xmm0
movss %xmm0, (%rsi,%r9,4)
incq %r9
cmpq %rax, %r9
jne 0x2624c
movq 0x30(%r14), %rcx
testq %rcx, %rcx
je 0x262ec
testl %eax, %eax
jle 0x2637e
movq 0x10(%rbx), %rdx
movq 0x18(%rbx), %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 0x262b6
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rsi,%rdi,4)
incq %rdi
cmpq %rax, %rdi
jne 0x262b0
jmp 0x2637e
testl %eax, %eax
jle 0x2637e
movq 0x10(%rbx), %rcx
movq 0x18(%rbx), %rdx
movq 0x28(%r14), %rsi
leaq (%rsi,%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 0x26355
xorl %r11d, %r11d
xorps %xmm1, %xmm1
cvtss2sd (%r8,%r11,4), %xmm1
movq (%rdi,%r11,8), %rbx
mulsd (%rbx,%r10,8), %xmm1
cvtss2sd %xmm0, %xmm0
subsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rdx,%r10,4)
incq %r11
leal (%r11,%r9), %ebx
cmpl %ebx, %eax
jg 0x26325
movq (%rsi,%r10,8), %r11
cvtss2sd %xmm0, %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 0x2630b
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq