// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movq %rsi, -0x38(%rbp)
movq %rdi, %r15
movl %r8d, %r14d
movq %rsp, %rax
leaq 0xf(,%r14,8), %rsi
andq $-0x10, %rsi
subq %rsi, %rax
movq %rax, -0x40(%rbp)
movq %rax, %rsp
movq %rsp, %r13
movq %rsi, -0x30(%rbp)
subq %rsi, %r13
movq %r13, %rsp
testl %r8d, %r8d
jle 0x36c0b
movl %r9d, %eax
xorl %esi, %esi
movq $0x0, (%r13,%rsi,8)
testl %r9d, %r9d
jle 0x36c03
movq (%rdx,%rsi,8), %rdi
movq 0x8(%r15), %r8
movsd (%r13,%rsi,8), %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 0x36bda
movsd %xmm0, (%r13,%rsi,8)
incq %rsi
cmpq %r14, %rsi
jne 0x36bba
movl %ebx, %edi
movl %ebx, %esi
callq 0x49798
movq %rsp, %r12
subq -0x30(%rbp), %r12
movq %r12, %rsp
movq %rax, -0x30(%rbp)
movq %rax, %rdi
movq -0x38(%rbp), %rsi
movl %ebx, %edx
callq 0x49810
movl %eax, -0x38(%rbp)
testl %eax, %eax
je 0x36c60
leaq 0x25134(%rip), %rdi     # 0x5bd72
callq 0x2090
testl %ebx, %ebx
movq -0x30(%rbp), %r13
jle 0x36c86
movq 0x18(%r15), %rdi
leaq (,%r14,4), %rdx
xorl %esi, %esi
callq 0x2110
jmp 0x36c86
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq %r13, %rdx
movl %ebx, %ecx
callq 0x499a8
movq -0x40(%rbp), %rdi
movq -0x30(%rbp), %r13
movq %r13, %rsi
movq %r12, %rdx
movl %ebx, %ecx
callq 0x49a0a
movq %r13, %rdi
movl %ebx, %esi
callq 0x49642
movl $0x1, %eax
cmpl $0x0, -0x38(%rbp)
jne 0x36cd0
leaq 0x250eb(%rip), %rdi     # 0x5bd8d
callq 0x2090
testl %ebx, %ebx
jle 0x36cce
movq 0x18(%r15), %rcx
xorl %eax, %eax
xorl %edx, %edx
movq -0x40(%rbp), %rsi
xorps %xmm0, %xmm0
cvtsd2ss (%rsi,%rdx,8), %xmm0
movss %xmm0, (%rcx,%rdx,4)
incq %rdx
cmpq %rdx, %r14
jne 0x36cb7
jmp 0x36cd0
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq