// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/simulation.c
// Function: Tors_step
int Tors_step(struct Tors *Diff, double **T_Lambda, float **Jacobian_ar,
	      float *mass_coord, int naxes, int N_Cart)
{
  double d_phi[naxes]; int fail=0;
  // Delta_phi=(T+Lambda I)^(-1)(M J^t Delta R)
  double Y[naxes]; int i, a;
  for(a=0; a<naxes; a++){
    Y[a]=0;
    for(i=0; i<N_Cart; i++)
      Y[a]+=mass_coord[i]*Jacobian_ar[a][i]*Diff->Cart[i];
    //Y[a]/=sqrt(T_Lambda[a][a]);
  }
  //(T+Lambda I)= LL^t Lower tridiagonal matrix L[i][j] with j<=i
  double **L=Allocate_mat2_d(naxes, naxes), X[naxes];
  if(choldc(L, T_Lambda, naxes)==0){
    //Solve L L^t d_phi = Y as: LX=Y (forward), L^t d_phi=X (backward)
    Forward_substitution(X, L, Y, naxes);
    Backward_substitution(d_phi, L, X, naxes);
    // The mean error is of order 1e-11
  }else{
    printf("Cholc decomposition failed\n");
    fail=1; for(a=0; a<naxes; a++)Diff->Tors[a]=0;
  }
  Empty_matrix_d(L, naxes);
  if(fail)return(1);
  /*double Yphi=0, phi2=0;
  for(a=0; a<naxes; a++){Yphi+=Y[a]*d_phi[a]; phi2+=d_phi[a]*d_phi[a];}
  float scale=Yphi/phi2;
  printf("Fast torsional step, scale= %.2g Yphi=%.2g phi2=%.2g\n",
	 scale, Yphi, phi2);
	 for(a=0; a<naxes; a++)Diff->Tors[a]=scale*d_phi[a];*/
  printf("Fast torsional step\n");
  for(a=0; a<naxes; a++)Diff->Tors[a]=d_phi[a];

  return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, %r14d
movq %rsi, -0x38(%rbp)
movq %rdi, %r15
movl %r8d, %ebx
movq %rsp, %r12
leaq 0xf(,%rbx,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %r13
movq %rax, -0x30(%rbp)
subq %rax, %r13
movq %r13, %rsp
testl %r8d, %r8d
jle 0x369c0
movl %r9d, %eax
xorl %esi, %esi
movq $0x0, (%r13,%rsi,8)
testl %r9d, %r9d
jle 0x369b8
movq (%rdx,%rsi,8), %rdi
movq 0x8(%r15), %r8
xorpd %xmm0, %xmm0
xorl %r10d, %r10d
movss (%rcx,%r10,4), %xmm1
mulss (%rdi,%r10,4), %xmm1
mulss (%r8,%r10,4), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %r10
cmpq %r10, %rax
jne 0x3698f
movsd %xmm0, (%r13,%rsi,8)
incq %rsi
cmpq %rbx, %rsi
jne 0x36972
movl %r14d, %edi
movl %r14d, %esi
callq 0x49804
movl %r14d, %edx
movq %rsp, %r14
subq -0x30(%rbp), %r14
movq %r14, %rsp
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq -0x38(%rbp), %rsi
movl %edx, -0x38(%rbp)
callq 0x4987c
testl %eax, %eax
je 0x36a2c
movq -0x30(%rbp), %r12
leaq 0x25330(%rip), %rdi     # 0x5bd2a
callq 0x2090
movl -0x38(%rbp), %r14d
testl %r14d, %r14d
jle 0x36a1a
movq 0x18(%r15), %rdi
shlq $0x2, %rbx
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2110
movq %r12, %rdi
movl %r14d, %esi
callq 0x496ae
movl $0x1, %eax
jmp 0x36a93
movq %r14, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
movl -0x38(%rbp), %r13d
movl %r13d, %ecx
callq 0x49a2e
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq %r14, %rdx
movl %r13d, %ecx
callq 0x49a86
movq -0x30(%rbp), %rdi
movl %r13d, %esi
callq 0x496ae
leaq 0x252de(%rip), %rdi     # 0x5bd45
callq 0x2090
testl %r13d, %r13d
jle 0x36a91
movq 0x18(%r15), %rcx
xorl %eax, %eax
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsd2ss (%r12,%rdx,8), %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %rbx
jne 0x36a79
jmp 0x36a93
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq