// File: /workspace/github_c_makefile_star2_O1/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 $0x148, %rsp            # imm = 0x148
movsd %xmm0, -0x80(%rbp)
movl %r9d, -0x2c(%rbp)
movq %r8, -0x60(%rbp)
movq %rcx, -0x78(%rbp)
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x21927(%rip), %rdi     # 0x5bdfe
callq 0x2090
leaq 0x2199d(%rip), %rsi     # 0x5be80
leaq -0x170(%rbp), %rax
movl $0xa, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1cd44(%rip), %rsi     # 0x57240
movq %rax, %rdi
callq 0x2210
leaq 0x215d3(%rip), %rdi     # 0x5bade
movl $0x1d, %esi
movl $0x1, %edx
movq %rax, -0x48(%rbp)
movq %rax, %rcx
callq 0x2280
movq %r14, %rdi
movl %r13d, %esi
movq %rbx, -0x68(%rbp)
movq %rbx, %rdx
callq 0x1512b
movl %r13d, %eax
testl %r13d, %r13d
movl %r13d, -0x3c(%rbp)
movq %rax, -0x58(%rbp)
jle 0x3a687
shlq $0x6, %rax
leaq (%rax,%rax,2), %r15
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq $0x0, 0x28(%r14,%r12)
je 0x3a675
cmpl $0x0, 0x34(%r14,%r12)
jns 0x3a675
cmpl $0x0, 0x38(%r14,%r12)
jns 0x3a675
movq (%r14,%r12), %rdi
addq $0x18, %rdi
callq 0x10374
cmpl $0x4, %eax
ja 0x3a675
movl %ebx, -0x4c(%rbp)
movq (%r14,%r12), %rbx
addq $0x18, %rbx
movsd 0x58(%r14,%r12), %xmm1
movl %eax, %r13d
leaq 0x2f8a6(%rip), %rax     # 0x69e50
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
movsd 0x60(%r14,%r12), %xmm2
divsd %xmm1, %xmm2
movsd %xmm2, -0x70(%rbp)
subsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
leaq 0x2f89d(%rip), %rax     # 0x69e70
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
callq 0x2130
movsd -0x70(%rbp), %xmm1
subsd %xmm0, %xmm1
movq -0x48(%rbp), %rdi
leaq 0x21550(%rip), %rsi     # 0x5bb45
movq %rbx, %rdx
movsd -0x38(%rbp), %xmm0
movb $0x2, %al
callq 0x2190
movq 0x28(%r14,%r12), %rax
cmpq $0x0, 0x28(%rax)
leaq 0x2f83b(%rip), %rax     # 0x69e50
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
je 0x3a64d
leaq 0x2f849(%rip), %rax     # 0x69e70
movsd %xmm0, -0x38(%rbp)
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
callq 0x2130
movsd -0x38(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movl -0x4c(%rbp), %ebx
movsd %xmm0, 0x60(%r14,%r12)
leaq 0x2f7f2(%rip), %rax     # 0x69e50
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,4), %xmm0
movsd %xmm0, 0x58(%r14,%r12)
addl $0x2, %ebx
movl -0x3c(%rbp), %r13d
addq $0xc0, %r12
cmpq %r12, %r15
jne 0x3a554
jmp 0x3a689
xorl %ebx, %ebx
movq -0x48(%rbp), %rdi
callq 0x20c0
leaq 0x21463(%rip), %rdi     # 0x5bafc
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x21474(%rip), %rdi     # 0x5bb1d
leaq -0x170(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq %rsp, -0x38(%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 0x3a6ec
leaq (,%r12,8), %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2110
movq %r12, -0x48(%rbp)
movq %r14, %rdi
movl %r13d, %esi
movq %rbx, %rdx
movl -0x2c(%rbp), %ecx
callq 0x154bb
movq %rsp, %r12
imulq $0x70, -0x58(%rbp), %rax
subq %rax, %r12
movq %r12, %rsp
testl %r13d, %r13d
movq -0x68(%rbp), %r8
jle 0x3a73c
xorl %eax, %eax
xorl %edx, %edx
leaq (%r8,%rax), %rsi
leaq (%r12,%rax), %rdi
movl $0xe, %ecx
rep  movsq (%rsi), %es:(%rdi)
movl %edx, 0x54(%r12,%rax)
incl %edx
addq $0x70, %rax
cmpl %edx, %r13d
jne 0x3a71c
testl %r13d, %r13d
jle 0x3a76c
leaq 0x8(%r14), %rax
imulq $0x70, -0x58(%rbp), %rcx
xorl %edx, %edx
movq 0x10(%rax), %rsi
movq %rsi, 0x10(%r12,%rdx)
movups (%rax), %xmm0
movups %xmm0, (%r12,%rdx)
addq $0xc0, %rax
addq $0x70, %rdx
cmpq %rdx, %rcx
jne 0x3a74c
movq %rsp, %rax
subq %r15, %rax
movq %rax, -0x58(%rbp)
movq %rax, %rsp
leaq 0x16ae3(%rip), %rdx     # 0x51263
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq %r8, %rcx
xorl %r8d, %r8d
movl %r13d, %r9d
callq 0x25c24
movl (%rbx), %eax
movl 0x4(%rbx), %edx
movq 0x10(%rbx), %rcx
movq 0x28(%rbx), %rsi
movl %eax, -0x88(%rbp)
movl %edx, -0x84(%rbp)
movq %rsi, -0xa0(%rbp)
movq %rsi, -0x98(%rbp)
movq %rcx, -0x90(%rbp)
leaq -0x118(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x2382e
movl %r13d, %r8d
movq %rsp, %r13
movq -0x48(%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 -0x60(%rbp), %rdx
movl -0x2c(%rbp), %ebx
movl %ebx, %ecx
callq 0x389ee
leaq -0xd0(%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 -0x118(%rbp), %rdi
movq -0x60(%rbp), %rsi
movl %ebx, %edx
movq -0x68(%rbp), %rcx
movl -0x3c(%rbp), %r8d
xorl %r9d, %r9d
callq 0x20fb0
movq -0x48(%rbp), %rsi
addq $0x30, %rsp
testl %ebx, %ebx
jle 0x3a89b
movq -0x60(%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 0x3a880
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 -0xa0(%rbp), %xmm0
movups -0x90(%rbp), %xmm1
movups %xmm1, 0x40(%rsp)
movups %xmm0, 0x30(%rsp)
leaq -0xd0(%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 -0x68(%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 -0x60(%rbp), %rax
movq %rax, 0x58(%rsp)
movq -0x78(%rbp), %rax
movq %rax, 0x50(%rsp)
leaq 0x208fa(%rip), %rsi     # 0x5b24f
leaq -0x118(%rbp), %r15
movq -0x58(%rbp), %rdi
movq %r14, %rdx
movq %r15, %rcx
movsd -0x80(%rbp), %xmm0
movq %r12, %r8
movl -0x3c(%rbp), %r12d
movl %r12d, %r9d
callq 0x358db
addq $0xf0, %rsp
movq %rbx, %rdi
callq 0x26056
subq $0x50, %rsp
movl $0x9, %ecx
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x2387a
addq $0x50, %rsp
movq %r14, %rdi
movl %r12d, %esi
callq 0x3a9c2
movq -0x38(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq