// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/simulation.c
// Function: Standardize_bonds
void Standardize_bonds(struct bond *bonds, atom *atoms,
		       int natoms, char *nameout1,
		       struct axe *axes, int naxes,
		       struct residue *seq, int nres,
		       struct chain *chains, int Nchain, 
		       double Tors_fluct,
		       struct Para_simul Para_simul)
{
  printf("Standardizing bond lengths and bond angles\n");

  int PR_STD=1; char name[80]="tmp.dat"; FILE *file_out;
  if(PR_STD){
    file_out=fopen(name, "w");
    fprintf(file_out, "#atm delta_r delta_cos_theta\n");
  }

  // Change bond angles and bond lengths in bonds
  Set_bonds_measure(bonds, natoms, atoms);
  int nc=0, i;
  for(i=0; i<natoms; i++){
    struct bond *bond=bonds+i;
    if(bond->previous==NULL)continue;
    if((bond->len>=0)||(bond->angle>=0))continue;  // exclude pseudo-bonds
    int na=Atom_type(bond->atom->name);
    if((na<0)||(na>4))continue;
    if(PR_STD){
      fprintf(file_out, "%s %.3f %.3f\n",
	      bond->atom->name, bond->l-BB_length[na],
	      bond->r_cos_theta/bond->l-cos(BB_angle[na]));
    }
    if(bond->previous->previous){
      bond->r_cos_theta =  BB_length[na]*cos(BB_angle[na]);
    }else{
      bond->r_cos_theta =  BB_length[na];
    }
    bond->l=BB_length[na];
    nc+=2;
  }
  if(PR_STD){fclose(file_out);
  printf("%d internal coordinates changed\n", nc);
  printf("Coordinates written in %s\n", name);}

  // Compute coordinates and RMSD
  double d_phi[naxes]; for(i=0; i<naxes; i++)d_phi[i]=0;
  Build_up(bonds, natoms, d_phi, naxes);

  atom atoms2[natoms], *atom2=atoms2, *atom1=atoms;
  for(i=0; i<natoms; i++){
    *atom2=*atom1; atom2->ali=i; atom1++; atom2++;
  }
  Copy_atoms_bonds(atoms2, bonds, natoms);

  // Adjust torsion angles
  double diff_phi[naxes];
  struct Reference Ref1;
  Set_reference(&Ref1, 0, "EB", atoms, 0, natoms);
  struct ali_atoms ali_a; Copy_ali(&ali_a, Ref1);
  struct Jacobian J;
  Allocate_Jacobian(&J, naxes, Ref1.N_Cart);
  int first_kin[naxes], last_kin[naxes];
  Change_kin(first_kin, last_kin, axes, naxes, natoms, Ref1);
  Compute_kinetic(&J, axes, naxes, atoms, natoms, Ref1, 0);
  Change_kin_back(axes, naxes, first_kin, last_kin);
  Torsional_confchange_RRR(diff_phi, "standard", bonds, &J, Ref1, ali_a,
			   Tors_fluct, atoms2, natoms, nameout1, axes, naxes,
			   seq, nres, chains, Nchain,atoms,natoms,Para_simul,
			   NULL,0);
  Empty_Ref(&Ref1);
  Empty_Jacobian(J);

  // Check
  Test_standard(bonds, natoms);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movsd %xmm0, -0x68(%rbp)
movl %r9d, -0x2c(%rbp)
movq %r8, -0x58(%rbp)
movq %rcx, -0x60(%rbp)
movl %edx, %r15d
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x20607(%rip), %rdi     # 0x55d16
callq 0x2090
leaq 0x20685(%rip), %rsi     # 0x55da0
pushq $0xa
popq %rcx
leaq -0x160(%rbp), %rax
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1ba1e(%rip), %rsi     # 0x51150
movq %rax, %rdi
callq 0x2210
leaq 0x202b5(%rip), %rdi     # 0x559f6
pushq $0x1d
popq %rsi
pushq $0x1
popq %rdx
movq %rax, -0x48(%rbp)
movq %rax, %rcx
callq 0x2280
movq %r14, %rdi
movl %r15d, %esi
movq %rbx, -0x50(%rbp)
movq %rbx, %rdx
callq 0x139f6
xorl %r12d, %r12d
testl %r15d, %r15d
movl $0x0, %ebx
movl %r15d, -0x30(%rbp)
cmovgl %r15d, %ebx
imulq $0xc0, %rbx, %r15
xorl %r13d, %r13d
cmpq %r12, %r15
je 0x358a4
cmpq $0x0, 0x28(%r14,%r12)
je 0x35898
cmpl $0x0, 0x34(%r14,%r12)
jns 0x35898
cmpl $0x0, 0x38(%r14,%r12)
jns 0x35898
movq (%r14,%r12), %rdi
addq $0x18, %rdi
callq 0xf464
cmpl $0x4, %eax
ja 0x35898
movl %r13d, -0x40(%rbp)
movq (%r14,%r12), %rcx
addq $0x18, %rcx
movq %rcx, -0x38(%rbp)
movsd 0x58(%r14,%r12), %xmm1
movl %eax, %r13d
leaq 0x2f66a(%rip), %rax     # 0x64e50
cvtss2sd (%rax,%r13,4), %xmm0
movsd 0x60(%r14,%r12), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, -0x70(%rbp)
subsd %xmm0, %xmm1
movsd %xmm1, -0x78(%rbp)
leaq 0x2f664(%rip), %rax     # 0x64e70
cvtss2sd (%rax,%r13,4), %xmm0
callq 0x2130
movsd -0x70(%rbp), %xmm1
subsd %xmm0, %xmm1
movq -0x48(%rbp), %rdi
leaq 0x20232(%rip), %rsi     # 0x55a5d
movq -0x38(%rbp), %rdx
movsd -0x78(%rbp), %xmm0
movb $0x2, %al
callq 0x2190
movq 0x28(%r14,%r12), %rax
cmpq $0x0, 0x28(%rax)
leaq 0x2f604(%rip), %rcx     # 0x64e50
cvtss2sd (%rcx,%r13,4), %xmm1
je 0x3587f
leaq 0x2f615(%rip), %rax     # 0x64e70
cvtss2sd (%rax,%r13,4), %xmm0
movsd %xmm1, -0x38(%rbp)
callq 0x2130
mulsd -0x38(%rbp), %xmm0
leaq 0x2f5d9(%rip), %rax     # 0x64e50
cvtss2sd (%rax,%r13,4), %xmm1
jmp 0x35882
movaps %xmm1, %xmm0
movl -0x40(%rbp), %r13d
movsd %xmm0, 0x60(%r14,%r12)
movsd %xmm1, 0x58(%r14,%r12)
addl $0x2, %r13d
addq $0xc0, %r12
jmp 0x35782
movq -0x48(%rbp), %rdi
callq 0x20c0
leaq 0x20160(%rip), %rdi     # 0x55a14
xorl %r12d, %r12d
movl %r13d, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x2016d(%rip), %rdi     # 0x55a35
leaq -0x160(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %rsp, -0x48(%rbp)
movl -0x2c(%rbp), %ecx
movl %ecx, %eax
movq %rsp, %rdx
movq %rax, -0x38(%rbp)
leaq 0xf(,%rax,8), %r15
andq $-0x10, %r15
subq %r15, %rdx
movq %rdx, %rsp
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
cmpq %r12, %rax
je 0x35911
andq $0x0, (%rdx,%r12,8)
incq %r12
jmp 0x35902
movq %r14, %rdi
movl -0x30(%rbp), %r12d
movl %r12d, %esi
movl -0x2c(%rbp), %ecx
callq 0x13d70
movl %r12d, %eax
movq %rsp, %r12
imulq $0x70, %rax, %rax
subq %rax, %r12
movq %r12, %rsp
xorl %r8d, %r8d
pushq $0xe
popq %rax
xorl %r9d, %r9d
movq -0x50(%rbp), %r13
cmpl %r9d, %ebx
je 0x35963
leaq (%r12,%r8), %rdx
leaq (%r8,%r13), %rsi
movq %rax, %rcx
movq %rdx, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl %r9d, 0x54(%rdx)
incl %r9d
addq $0x70, %r8
jmp 0x35940
movq %r12, %rdi
movq %r14, %rsi
movl -0x30(%rbp), %ebx
movl %ebx, %edx
callq 0x32938
movq %rsp, %rax
subq %r15, %rax
movq %rax, -0x40(%rbp)
movq %rax, %rsp
leaq 0x158a8(%rip), %rdx     # 0x4b22f
leaq -0xc8(%rbp), %r15
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %rcx
xorl %r8d, %r8d
movl %ebx, %r9d
callq 0x231d9
movl (%r15), %eax
movl 0x4(%r15), %edx
movq 0x10(%r15), %rcx
movq 0x28(%r15), %rsi
movq %r15, %rbx
movl %eax, -0x80(%rbp)
movl %edx, -0x7c(%rbp)
movq %rsi, -0x98(%rbp)
movq %rsi, -0x90(%rbp)
movq %rcx, -0x88(%rbp)
leaq -0x110(%rbp), %rdi
movl -0x2c(%rbp), %r15d
movl %r15d, %esi
callq 0x210c7
movq %rsp, %rcx
movq -0x38(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, %rsp
movq %rcx, %rdi
movq %rcx, -0x38(%rbp)
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
subq $0x30, %rsp
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r13, %rsi
movq -0x58(%rbp), %rbx
movq %rbx, %rdx
movl %r15d, %ecx
movl -0x30(%rbp), %r15d
movl %r15d, %r8d
callq 0x34062
leaq -0xc8(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq -0x110(%rbp), %rdi
movq %rbx, %rsi
movl -0x2c(%rbp), %edx
movq -0x50(%rbp), %rcx
movl %r15d, %r8d
xorl %r9d, %r9d
callq 0x1eb7c
addq $0x30, %rsp
movq %rbx, %rdi
movl -0x2c(%rbp), %r15d
movl %r15d, %esi
movq -0x38(%rbp), %rdx
movq %r13, %rcx
callq 0x34037
subq $0xf0, %rsp
leaq 0x30(%rbp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups 0x2c(%rax), %xmm3
movups %xmm3, 0xc4(%rsp)
movups %xmm2, 0xb8(%rsp)
movups %xmm1, 0xa8(%rsp)
movups %xmm0, 0x98(%rsp)
movups -0x98(%rbp), %xmm0
movups -0x88(%rbp), %xmm1
movups %xmm1, 0x40(%rsp)
movups %xmm0, 0x30(%rsp)
leaq -0xc8(%rbp), %r13
movups (%r13), %xmm0
movups 0x10(%r13), %xmm1
movups 0x20(%r13), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x50(%rbp), %rax
movq %rax, 0x88(%rsp)
movl 0x28(%rbp), %eax
movl %eax, 0x80(%rsp)
movq 0x20(%rbp), %rax
movq %rax, 0x78(%rsp)
movq 0x10(%rbp), %rax
movq %rax, 0x68(%rsp)
movl %r15d, 0x60(%rsp)
movq %rbx, 0x58(%rsp)
movq -0x60(%rbp), %rax
movq %rax, 0x50(%rsp)
leaq 0x1f619(%rip), %rsi     # 0x55167
movq -0x40(%rbp), %rdi
movq %r14, %rdx
leaq -0x110(%rbp), %r15
movq %r15, %rcx
movsd -0x68(%rbp), %xmm0
movq %r12, %r8
movl -0x30(%rbp), %ebx
movl %ebx, %r9d
callq 0x31a2b
addq $0xf0, %rsp
movq %r13, %rdi
callq 0x2357a
subq $0x50, %rsp
pushq $0x9
popq %rcx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x21113
addq $0x50, %rsp
movq %r14, %rdi
movl %ebx, %esi
callq 0x35bb7
movq -0x48(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq