// File: /workspace/github_c_makefile_star2_O3/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, -0x78(%rbp)
movl %r9d, -0x2c(%rbp)
movq %r8, -0x48(%rbp)
movq %rcx, -0x70(%rbp)
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x2190b(%rip), %rdi     # 0x5bdb6
callq 0x2090
leaq 0x21989(%rip), %rsi     # 0x5be40
leaq -0x160(%rbp), %rax
movl $0xa, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1cd30(%rip), %rsi     # 0x57200
movq %rax, %rdi
callq 0x2220
leaq 0x215b7(%rip), %rdi     # 0x5ba96
movl $0x1d, %esi
movl $0x1, %edx
movq %rax, -0x38(%rbp)
movq %rax, %rcx
callq 0x2290
movq %r14, %rdi
movl %r13d, %esi
movq %rbx, -0x60(%rbp)
movq %rbx, %rdx
callq 0x14cf3
movl %r13d, %eax
testl %r13d, %r13d
movl %r13d, -0x30(%rbp)
movq %rax, -0x58(%rbp)
jle 0x3a64b
shlq $0x6, %rax
leaq (%rax,%rax,2), %r15
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq $0x0, 0x28(%r14,%r12)
je 0x3a639
cmpl $0x0, 0x34(%r14,%r12)
jns 0x3a639
cmpl $0x0, 0x38(%r14,%r12)
jns 0x3a639
movq (%r14,%r12), %rdi
addq $0x18, %rdi
callq 0xff04
cmpl $0x4, %eax
ja 0x3a639
movl %ebx, -0x40(%rbp)
movq (%r14,%r12), %rbx
addq $0x18, %rbx
movsd 0x58(%r14,%r12), %xmm1
movl %eax, %r13d
leaq 0x2f8d2(%rip), %rax     # 0x69e50
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
movsd 0x60(%r14,%r12), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, -0x68(%rbp)
subsd %xmm0, %xmm1
movsd %xmm1, -0x50(%rbp)
leaq 0x2f8c9(%rip), %rax     # 0x69e70
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
callq 0x2130
movsd -0x68(%rbp), %xmm1
subsd %xmm0, %xmm1
movq -0x38(%rbp), %rdi
leaq 0x21534(%rip), %rsi     # 0x5bafd
movq %rbx, %rdx
leaq 0x2f87d(%rip), %rbx     # 0x69e50
movsd -0x50(%rbp), %xmm0
movb $0x2, %al
callq 0x21a0
movq 0x28(%r14,%r12), %rax
cmpq $0x0, 0x28(%rax)
xorps %xmm1, %xmm1
cvtss2sd (%rbx,%r13,4), %xmm1
je 0x3a61e
leaq 0x2f875(%rip), %rax     # 0x69e70
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
movsd %xmm1, -0x50(%rbp)
callq 0x2130
mulsd -0x50(%rbp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%rbx,%r13,4), %xmm1
jmp 0x3a621
movaps %xmm1, %xmm0
movl -0x30(%rbp), %r13d
movl -0x40(%rbp), %ebx
movsd %xmm0, 0x60(%r14,%r12)
movsd %xmm1, 0x58(%r14,%r12)
addl $0x2, %ebx
addq $0xc0, %r12
cmpq %r12, %r15
jne 0x3a528
jmp 0x3a64d
xorl %ebx, %ebx
movq -0x38(%rbp), %rdi
callq 0x20c0
leaq 0x21457(%rip), %rdi     # 0x5bab4
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x21468(%rip), %rdi     # 0x5bad5
leaq -0x160(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %rsp, -0x40(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, %r12d
movq %rsp, %rbx
leaq 0xf(,%r12,8), %r15
andq $-0x10, %r15
subq %r15, %rbx
movq %rbx, %rsp
testl %eax, %eax
jle 0x3a6b0
leaq (,%r12,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2110
movq %r12, -0x38(%rbp)
movq %r14, %rdi
movl %r13d, %esi
movq %rbx, %rdx
movl -0x2c(%rbp), %ecx
callq 0x15083
movq %rsp, %r12
imulq $0x70, -0x58(%rbp), %rax
subq %rax, %r12
movq %r12, %rsp
testl %r13d, %r13d
movq -0x60(%rbp), %r9
jle 0x3a730
xorl %edx, %edx
xorl %r8d, %r8d
leaq (%r9,%rdx), %rsi
leaq (%r12,%rdx), %rdi
movl $0xe, %ecx
rep  movsq (%rsi), %es:(%rdi)
movl %r8d, 0x54(%r12,%rdx)
incl %r8d
addq $0x70, %rdx
cmpl %r8d, %r13d
jne 0x3a6e1
testl %r13d, %r13d
jle 0x3a730
leaq 0x8(%r14), %rcx
xorl %edx, %edx
movq 0x10(%rcx), %rsi
movq %rsi, 0x10(%r12,%rdx)
movupd (%rcx), %xmm0
movupd %xmm0, (%r12,%rdx)
addq $0xc0, %rcx
addq $0x70, %rdx
cmpq %rdx, %rax
jne 0x3a70d
movq %rsp, %rax
subq %r15, %rax
movq %rax, -0x58(%rbp)
movq %rax, %rsp
leaq 0x16b93(%rip), %rdx     # 0x512d7
leaq -0xc8(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq %r9, %rcx
xorl %r8d, %r8d
movl %r13d, %r9d
callq 0x257f8
movl (%rbx), %eax
movl 0x4(%rbx), %edx
movq 0x10(%rbx), %rcx
movq 0x28(%rbx), %rsi
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), %esi
callq 0x2321a
movl %r13d, %r8d
movq %rsp, %r13
movq -0x38(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq %rsp, %r15
subq %rax, %r15
movq %r15, %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, %rdi
movq %r15, %rsi
movq -0x48(%rbp), %rdx
movl -0x2c(%rbp), %ebx
movl %ebx, %ecx
callq 0x387ac
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 -0x48(%rbp), %rsi
movl %ebx, %edx
movq -0x60(%rbp), %rcx
movl -0x30(%rbp), %r8d
xorl %r9d, %r9d
callq 0x20900
movq -0x38(%rbp), %rsi
addq $0x30, %rsp
testl %ebx, %ebx
jle 0x3a859
movq -0x48(%rbp), %rax
addq $0x1c, %rax
xorl %ecx, %ecx
movl (%r13,%rcx,4), %edx
movl %edx, -0x4(%rax)
movl (%r15,%rcx,4), %edx
movl %edx, (%rax)
incq %rcx
addq $0x158, %rax            # imm = 0x158
cmpl %ecx, %esi
jne 0x3a83e
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), %rbx
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq -0x60(%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 -0x2c(%rbp), %eax
movl %eax, 0x60(%rsp)
movq -0x48(%rbp), %rax
movq %rax, 0x58(%rsp)
movq -0x70(%rbp), %rax
movq %rax, 0x50(%rsp)
leaq 0x208f4(%rip), %rsi     # 0x5b207
leaq -0x110(%rbp), %r15
movq -0x58(%rbp), %rdi
movq %r14, %rdx
movq %r15, %rcx
movsd -0x78(%rbp), %xmm0
movq %r12, %r8
movl -0x30(%rbp), %r12d
movl %r12d, %r9d
callq 0x35693
addq $0xf0, %rsp
movq %rbx, %rdi
callq 0x25c01
subq $0x50, %rsp
movl $0x9, %ecx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x23266
addq $0x50, %rsp
movq %r14, %rdi
movl %r12d, %esi
callq 0x3a980
movq -0x40(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq