// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/simulation.c
// Function: Print_mode_PDB
int Print_mode_PDB(atom *atoms, int natoms,
		   struct axe *axe, int naxes,
		   struct bond *bonds, struct residue *seq,
		   float *Tors, float omega,
		   int N_STEP, struct Para_simul Para_simul,
		   char *nameout, int ia, int is)
{
  // Output file
  char pdbout[200];
  sprintf(pdbout, "%s_mode%d.pdb", nameout, is);
  FILE *file_out=fopen(pdbout, "w");
  printf("Writing normal modes in PDB format in %s\n", pdbout);

  // Coordinates
  Set_bonds_measure(bonds, natoms, atoms);
  int n3=3*natoms, i;
  float coord1[n3], coord2[n3], coord_ini[n3];
  float *coord_old=coord1, *coord_new=coord2;
  float mass[natoms]; Set_masses(mass,atoms,natoms);
  int num_atom=Put_coord(coord_ini, bonds, natoms);
  for(i=0; i<3*num_atom; i++)coord_old[i]=coord_ini[i];
  float Ene=Energy_clashes(coord_ini, num_atom);
  float Ene_thr=100*(Ene+0.1);

  // Determine maximum possible factor for mode ia
  float AMAX=20*Para_simul.AMPLITUDE;
  //float AMAX=16;
  float factor=AMAX/(omega*N_STEP); // Max amplitude = AMAX

  int nstep=N_STEP;
  float MAX_ANGLE=0.15, max_v=0;
  for(i=0; i<naxes; i++)if(fabs(Tors[i])>max_v)max_v=fabs(Tors[i]);
  if(factor*max_v > MAX_ANGLE){
    printf("WARNING, max. d_theta allowed for mode %d = %.3f > %.3f\n",
	   ia, factor*max_v, MAX_ANGLE);
    factor=(MAX_ANGLE/max_v);
    nstep=AMAX/(omega*factor);
  }
  double d_phi[naxes];
  for(i=0; i<naxes; i++)d_phi[i]=Tors[i]*factor;

  Print_PDB(file_out, atoms, natoms, coord1, seq, 0, 0.00);
  Set_bonds_measure(bonds, natoms, atoms);

  int step=0, forward=1, pdb=0, maxstep=0;
  float Ene_max=0, rmsd_max=0;
  while(1){
    if(forward){step++;}else{step--; if(step==0)break;}
    Build_up(bonds, natoms, d_phi, naxes);
    Put_coord(coord_new, bonds, num_atom);
    Ene=Energy_clashes(coord_new, num_atom);
    if(Ene>Ene_max)Ene_max=Ene;
    if((step==nstep)||(forward && (Ene>Ene_thr))){
      // change direction
      forward=0; for(i=0; i<naxes; i++)d_phi[i]=-d_phi[i];
      maxstep=step;
    }

    float rmsd=rmsd_mclachlan_f(coord_old, coord_new, mass, num_atom);
    if((rmsd > Para_simul.PDB_STEP)||((forward==0)&&(pdb==0))){
      pdb++;
      rmsd=rmsd_mclachlan_f(coord_ini, coord_new, mass, num_atom);
      Print_PDB(file_out, atoms, natoms, coord_new, seq, pdb, rmsd);
      float *tmp=coord_old; coord_old=coord_new; coord_new=tmp;
      if(rmsd>rmsd_max)rmsd_max=rmsd;
    }
  }
  fclose(file_out);
  printf("Mode %d, 1/omega= %.3g max.rmsd=%.2f Clash=%.3g snapshots=%d\n",
	 ia, 1./omega, rmsd_max, Ene_max, pdb);
  printf("Max.amplitude= %.3g step=%.3g max.angle= %.3f nstep=%d\n",
	 factor*maxstep, factor, factor*max_v, maxstep);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movss %xmm0, -0x3c(%rbp)
movq %r9, -0xa8(%rbp)
movq %r8, %r13
movq %r8, -0x88(%rbp)
movl %ecx, -0x44(%rbp)
movl %esi, %r12d
movq %rdi, -0x80(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0x64(%rbp)
movq 0x10(%rbp), %r14
movq 0x60(%rbp), %rdx
movl 0x70(%rbp), %ecx
leaq 0x25e90(%rip), %rsi     # 0x54d20
xorl %r15d, %r15d
leaq -0x190(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2260
leaq 0x222a5(%rip), %rsi     # 0x51150
movq %rbx, %rdi
callq 0x2210
movq %rax, -0x78(%rbp)
leaq 0x2422d(%rip), %rdi     # 0x530eb
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
movq %r13, %rdi
movl %r12d, %esi
movq -0x80(%rbp), %r13
movq %r13, %rdx
callq 0x139f6
movq %rsp, -0xb8(%rbp)
leal (%r12,%r12,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x50(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%rbp)
movq %rcx, %rsp
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
movl %r12d, %eax
movq %rsp, %rdi
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
movq %rdi, -0xb0(%rbp)
movq %r13, %rsi
movl %r12d, %edx
callq 0x2f30c
movq %rbx, %rdi
movq -0x88(%rbp), %rsi
movq %r12, -0x90(%rbp)
movl %r12d, %edx
callq 0x2f358
movq %rax, -0x70(%rbp)
leal (%rax,%rax,2), %eax
testl %eax, %eax
cmovlel %r15d, %eax
movq -0x50(%rbp), %rcx
cmpq %r15, %rax
je 0x2ef7e
movss (%rbx,%r15,4), %xmm0
movss %xmm0, (%rcx,%r15,4)
incq %r15
jmp 0x2ef68
movq %rbx, %rdi
movq -0x70(%rbp), %rsi
callq 0x2f3a6
cvtss2sd %xmm0, %xmm4
addsd 0x218ea(%rip), %xmm4    # 0x50880
mulsd 0x21e92(%rip), %xmm4    # 0x50e30
leaq 0x20(%rbp), %rax
movss 0x20(%rax), %xmm5
cvtsi2ssl 0x18(%rbp), %xmm0
mulss -0x3c(%rbp), %xmm0
xorl %eax, %eax
movl -0x44(%rbp), %ecx
testl %ecx, %ecx
movl $0x0, %r15d
cmovgl %ecx, %r15d
xorps %xmm3, %xmm3
movaps 0x22264(%rip), %xmm1    # 0x51230
cmpq %rax, %r15
je 0x2efe6
movss (%r14,%rax,4), %xmm2
andps %xmm1, %xmm2
maxss %xmm3, %xmm2
incq %rax
movaps %xmm2, %xmm3
jmp 0x2efcc
mulss 0x21e06(%rip), %xmm5    # 0x50df4
movaps %xmm5, %xmm1
divss %xmm0, %xmm1
movaps %xmm1, %xmm0
mulss %xmm3, %xmm0
ucomiss 0x24c35(%rip), %xmm0    # 0x53c38
movss %xmm3, -0x5c(%rbp)
jbe 0x2f05b
cvtss2sd %xmm0, %xmm0
leaq 0x25d19(%rip), %rdi     # 0x54d2e
movsd 0x25c83(%rip), %xmm1    # 0x54ca0
movl 0x68(%rbp), %esi
movb $0x2, %al
movsd %xmm4, -0x38(%rbp)
movss %xmm5, -0x2c(%rbp)
callq 0x20f0
movsd -0x38(%rbp), %xmm4
movss 0x24bfa(%rip), %xmm1    # 0x53c38
divss -0x5c(%rbp), %xmm1
movaps %xmm1, %xmm0
mulss -0x3c(%rbp), %xmm0
movss -0x2c(%rbp), %xmm2
divss %xmm0, %xmm2
cvttss2si %xmm2, %eax
movl %eax, -0x64(%rbp)
cvtsd2ss %xmm4, %xmm0
movss %xmm0, -0x9c(%rbp)
movl -0x44(%rbp), %eax
movq %rsp, %r13
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
xorl %eax, %eax
cmpq %rax, %r15
je 0x2f0a0
movss (%r14,%rax,4), %xmm0
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%r13,%rax,8)
incq %rax
jmp 0x2f081
movss %xmm1, -0x94(%rbp)
movq %rbx, -0xc0(%rbp)
xorl %ebx, %ebx
xorps %xmm0, %xmm0
movss %xmm0, -0x2c(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %r12
movq %r12, %rsi
movq -0x90(%rbp), %r14
movl %r14d, %edx
movq -0x50(%rbp), %rcx
movq -0xa8(%rbp), %r8
xorl %r9d, %r9d
callq 0x2f4bd
movq -0x88(%rbp), %rdi
movl %r14d, %esi
movq %r12, %rdx
callq 0x139f6
pushq $0x1
popq %rax
movl %eax, -0x38(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x60(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x40(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movss -0x2c(%rbp), %xmm1
cmpl $0x0, -0x38(%rbp)
movss %xmm1, -0x2c(%rbp)
je 0x2f12b
incl %ebx
jmp 0x2f133
decl %ebx
je 0x2f286
movq -0x88(%rbp), %r14
movq %r14, %rdi
movq -0x90(%rbp), %rsi
movq %r13, %rdx
movl -0x44(%rbp), %ecx
callq 0x13d70
movq -0x58(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq -0x70(%rbp), %r14
movl %r14d, %edx
callq 0x2f358
movq %r12, %rdi
movl %r14d, %esi
callq 0x2f3a6
movaps 0x1bf49(%rip), %xmm1    # 0x4b0c0
cmpl -0x64(%rbp), %ebx
je 0x2f18b
cmpl $0x0, -0x38(%rbp)
je 0x2f1b6
ucomiss -0x9c(%rbp), %xmm0
jbe 0x2f1c2
xorl %eax, %eax
cmpq %rax, %r15
je 0x2f1a7
movsd (%r13,%rax,8), %xmm2
xorps %xmm1, %xmm2
movlps %xmm2, (%r13,%rax,8)
incq %rax
jmp 0x2f18d
movl $0x0, -0x38(%rbp)
movb $0x1, %r12b
movl %ebx, -0x60(%rbp)
jmp 0x2f1cb
movl $0x0, -0x38(%rbp)
movb $0x1, %r12b
jmp 0x2f1cb
xorl %r12d, %r12d
pushq $0x1
popq %rax
movl %eax, -0x38(%rbp)
maxss -0x2c(%rbp), %xmm0
movss %xmm0, -0x2c(%rbp)
cmpl $0x0, -0x30(%rbp)
sete %r14b
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0x70(%rbp), %rcx
callq 0x47a64
movss -0x2c(%rbp), %xmm1
cvtsd2ss %xmm0, %xmm0
leaq 0x20(%rbp), %rax
ucomiss 0x1c(%rax), %xmm0
ja 0x2f211
andb %r14b, %r12b
je 0x2f11c
movl -0x30(%rbp), %r14d
incl %r14d
movq -0xc0(%rbp), %rdi
movq -0x58(%rbp), %r12
movq %r12, %rsi
movq -0xb0(%rbp), %rdx
movq -0x70(%rbp), %rcx
callq 0x47a64
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x98(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq %r12, %rcx
movq -0xa8(%rbp), %r8
movl %r14d, -0x30(%rbp)
movl %r14d, %r9d
callq 0x2f4bd
movss -0x98(%rbp), %xmm0
maxss -0x40(%rbp), %xmm0
movq -0x50(%rbp), %rax
movq %r12, -0x50(%rbp)
movss %xmm0, -0x40(%rbp)
jmp 0x2f113
movq -0x78(%rbp), %rdi
callq 0x20c0
cvtss2sd -0x3c(%rbp), %xmm1
movsd 0x1bdac(%rip), %xmm0    # 0x4b048
divsd %xmm1, %xmm0
cvtss2sd -0x40(%rbp), %xmm1
cvtss2sd -0x2c(%rbp), %xmm2
leaq 0x25ab6(%rip), %rdi     # 0x54d67
movl 0x68(%rbp), %esi
movl -0x30(%rbp), %edx
movb $0x3, %al
callq 0x20f0
movl -0x60(%rbp), %esi
cvtsi2ss %esi, %xmm0
movss -0x94(%rbp), %xmm3
mulss %xmm3, %xmm0
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm3, %xmm1
movss -0x5c(%rbp), %xmm2
mulss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x25ab8(%rip), %rdi     # 0x54da5
movb $0x3, %al
callq 0x20f0
movq -0xb8(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq