// File: /workspace/github_c_makefile_star2_O0/ugobas[P]tnm/nma.c
// Function: Convert_torsion2cart
void Convert_torsion2cart(float *Cart_mode, atom *atoms, float *u_a,
			  struct axe *axes, int N_axes,
			  struct Reference Ref, int ia)
{
  // Computing derivative:
  // dr_ia ~ (sum_k u_a,k*rot_ik) X r_i + (sum_k u_a,k*shift_ik)
  int  i, j, k, jj;

  double dr_daxe_rot_global[3], dr_daxe_shift_global[3];
  for(j=0; j<3; j++){
    dr_daxe_rot_global[j]=0;
    dr_daxe_shift_global[j]=0;
  }
  float *u_ak=u_a;
  struct axe *axe=axes;
  for(k=0; k<N_axes; k++){
    for(j=0; j<3; j++){
      dr_daxe_shift_global[j]+=(*u_ak)*axe->global_shift[j];
      dr_daxe_rot_global[j]  +=(*u_ak)*axe->global_rot[j];
    }
    u_ak++; axe++;
  }

  double dr_daxe_rot[Ref.N_Cart], dr_daxe_shift[Ref.N_Cart];
  for(j=0; j<Ref.N_Cart; j++){
    dr_daxe_rot[j]=0;
    dr_daxe_shift[j]=0;
  }
  u_ak=u_a; axe=axes;
  for(k=0; k<N_axes; k++){
    jj=3*axe->first_kin;
    for(i=axe->first_kin; i<=axe->last_kin; i++){
      for(j=0; j<3; j++){
	dr_daxe_rot[jj]  +=(*u_ak)*axe->rot[j];
	dr_daxe_shift[jj]+=(*u_ak)*axe->shift[j];
	jj++;
      }
    }
    u_ak++; axe++;
  }

  double r[3], dd[3], rot[3]; jj=0;
  for(i=0; i<Ref.N_ref; i++){
    atom *atom=atoms+Ref.atom_num[i];
    for(j=0; j<3; j++){
      r[j]= atom->r[j];
      rot[j]=dr_daxe_rot[jj+j]+dr_daxe_rot_global[j];
    }
    Vector_product_d(dd, rot, r);
    for(j=0; j<3; j++){
      Cart_mode[jj]=dd[j]+dr_daxe_shift[jj]+dr_daxe_shift_global[j];
      jj++;
    }
  }
  Normalize_vector_weighted(Cart_mode, Ref.mass_coord, Ref.N_Cart);

  // UGO 10/08/22 r_i=sqrt(m_i)*x_i; It may induce mistakes
  //float norm=sqrt(Ref.mass_sum/Ref.N_ref);
  //for(j=0; j<Ref.N_Cart; j++)Cart_mode[j]*=norm; //Ref.mass_sqrt[j];

  if(0){
    // Test Eckart conditions. 15/03/19 I verified that they are fulfilled
    jj=0; int violation=0; float eps=0.001;
    double d[3], M_tot=0, Md_sum[3]; for(j=0; j<3; j++)Md_sum[j]=0;
    double MRd[3], MRd_sum[3]; for(j=0; j<3; j++)MRd_sum[j]=0;
    for(i=0; i<Ref.N_ref; i++){
      double *ri=atoms[Ref.atom_num[i]].r;
      float m= Ref.mass_atom[i];
      M_tot+=m;
      for(j=0; j<3; j++){
	r[j]=ri[j]; d[j]=Cart_mode[jj];
	Md_sum[j]+=m*Cart_mode[jj]; jj++;	
      }
      Vector_product_d(MRd, r, d);
      for(j=0; j<3; j++){MRd_sum[j]+=m*MRd[j];}
    }
    float eps_m=eps*M_tot;
    for(j=0; j<3; j++){
      if((fabs(Md_sum[j])>eps_m)||(fabs(MRd_sum[j])>eps_m)){
	violation++;
	printf("Eckart violation for mode %d! Md_sum= %.4f MRd_sum= %.4f\n",
	       ia, Md_sum[j]/M_tot, MRd_sum[j]/M_tot);
      }
    }
    if(violation){
      printf("Exiting the program\n"); exit(8);
    }else{
      printf("Mode %d ok\n", ia);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
leaq 0x10(%rbp), %rax
movq %rax, -0x100(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x355a0
movslq -0x30(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp,%rax,8)
movslq -0x30(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp,%rax,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x35575
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x3566d
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x35645
movq -0x78(%rbp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x80(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsd 0x90(%rax,%rcx,8), %xmm2
movslq -0x30(%rbp), %rax
movsd -0x70(%rbp,%rax,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp,%rax,8)
movq -0x78(%rbp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x80(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsd 0xa8(%rax,%rcx,8), %xmm2
movslq -0x30(%rbp), %rax
movsd -0x50(%rbp,%rax,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp,%rax,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x355ca
movq -0x78(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x78(%rbp)
movq -0x80(%rbp), %rax
addq $0x158, %rax            # imm = 0x158
movq %rax, -0x80(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x355b7
movq -0x100(%rbp), %rax
movl 0x4(%rax), %ecx
movq %rsp, %rdx
movq %rdx, -0x88(%rbp)
leaq 0xf(,%rcx,8), %rsi
andq $-0x10, %rsi
movq %rsp, %rdx
subq %rsi, %rdx
movq %rdx, -0x110(%rbp)
movq %rdx, %rsp
movq %rcx, -0x90(%rbp)
movl 0x4(%rax), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x108(%rbp)
movq %rcx, %rsp
movq %rax, -0x98(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x100(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x35711
movq -0x108(%rbp), %rax
movq -0x110(%rbp), %rcx
movslq -0x30(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movslq -0x30(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x356d1
movq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x35827
movq -0x80(%rbp), %rax
imull $0x3, 0x18(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x80(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jg 0x357ff
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x357ef
movq -0x108(%rbp), %rax
movq -0x110(%rbp), %rcx
movq -0x78(%rbp), %rdx
movss (%rdx), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x80(%rbp), %rdx
movslq -0x30(%rbp), %rsi
movsd 0x48(%rdx,%rsi,8), %xmm2
movslq -0x38(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movq -0x78(%rbp), %rcx
movss (%rcx), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x80(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movsd 0x60(%rcx,%rdx,8), %xmm2
movslq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x35760
jmp 0x357f1
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x35749
movq -0x78(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x78(%rbp)
movq -0x80(%rbp), %rax
addq $0x158, %rax            # imm = 0x158
movq %rax, -0x80(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x35728
movl $0x0, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x100(%rbp), %rcx
movl -0x2c(%rbp), %eax
cmpl (%rcx), %eax
jge 0x35950
movq -0x100(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rcx), %rcx
movslq -0x2c(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x358cd
movq -0x110(%rbp), %rax
movq -0xf8(%rbp), %rcx
movslq -0x30(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movslq -0x30(%rbp), %rcx
movsd %xmm0, -0xb0(%rbp,%rcx,8)
movl -0x38(%rbp), %ecx
addl -0x30(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movslq -0x30(%rbp), %rax
addsd -0x50(%rbp,%rax,8), %xmm0
movslq -0x30(%rbp), %rax
movsd %xmm0, -0xf0(%rbp,%rax,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x35873
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x14aa0
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x35940
movq -0x108(%rbp), %rax
movslq -0x30(%rbp), %rcx
movsd -0xd0(%rbp,%rcx,8), %xmm0
movslq -0x38(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movslq -0x30(%rbp), %rax
addsd -0x70(%rbp,%rax,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x8(%rbp), %rax
movslq -0x38(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x358ee
jmp 0x35942
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x35835
movq -0x100(%rbp), %rax
movq -0x8(%rbp), %rdi
movq 0x18(%rax), %rsi
movl 0x4(%rax), %edx
callq 0x15650
movq -0x88(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)