// File: /workspace/github_c_makefile_star2_O3/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 $0x188, %rsp            # imm = 0x188
movss %xmm0, -0x48(%rbp)
movq %r9, -0xa8(%rbp)
movq %r8, %r14
movl %ecx, -0x30(%rbp)
movl %esi, %r15d
movq %rdi, %r12
movl 0x18(%rbp), %eax
movl %eax, -0x54(%rbp)
movq 0x60(%rbp), %rdx
movl 0x70(%rbp), %ecx
leaq 0x28cd0(%rip), %rsi     # 0x5adc0
movl $0x0, -0x50(%rbp)
leaq -0x1b0(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2270
leaq 0x250f1(%rip), %rsi     # 0x57200
movq %rbx, %rdi
callq 0x2220
movq %rax, -0x80(%rbp)
leaq 0x27069(%rip), %rdi     # 0x5918b
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x14cf3
movq %rsp, -0xc0(%rbp)
leal (%r15,%r15,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x68(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x40(%rbp)
movq %rcx, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movl %r15d, %ebx
movq %rsp, %rdi
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
movq %rdi, -0xb8(%rbp)
movq %r12, -0xa0(%rbp)
movq %r12, %rsi
movl %r15d, %edx
callq 0x326a4
movl $0x0, %r12d
movq %r15, -0x70(%rbp)
testl %r15d, %r15d
jle 0x32231
leaq 0x8(%r14), %rax
xorl %r12d, %r12d
movq %r13, %rcx
leaq (%r12,%r12,2), %rdx
shlq $0x6, %rdx
cmpl $0x0, 0x20(%r14,%rdx)
js 0x321ff
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rdx,2), %xmm0
movss %xmm0, (%rcx,%rdx)
addq $0x4, %rdx
cmpq $0xc, %rdx
jne 0x321d0
incq %r12
addq $0xc0, %rax
addq %rdx, %rcx
cmpq %rbx, %r12
jne 0x321be
movq -0x70(%rbp), %rax
movl %eax, %r12d
testl %r12d, %r12d
jle 0x3222a
leal (%r12,%r12,2), %eax
cmpl $0x2, %eax
movl $0x1, %edx
cmovgel %eax, %edx
shlq $0x2, %rdx
movq -0x68(%rbp), %rdi
movq %r13, %rsi
callq 0x21b0
movb $0x1, %al
movl %eax, -0x50(%rbp)
jmp 0x32231
movl $0x0, -0x50(%rbp)
movq 0x10(%rbp), %r15
movq %r13, %rdi
movq %r12, -0x78(%rbp)
movl %r12d, %esi
callq 0x3273e
cvtss2sd %xmm0, %xmm4
leaq 0x20(%rbp), %rax
movss 0x20(%rax), %xmm5
mulss 0x24c4b(%rip), %xmm5    # 0x56ea4
xorps %xmm0, %xmm0
cvtsi2ssl 0x18(%rbp), %xmm0
mulss -0x48(%rbp), %xmm0
movaps %xmm5, %xmm2
divss %xmm0, %xmm2
movl -0x30(%rbp), %eax
movl %eax, %r12d
testl %eax, %eax
jle 0x3229d
xorps %xmm1, %xmm1
xorl %eax, %eax
movaps 0x2505d(%rip), %xmm0    # 0x572e0
movss (%r15,%rax,4), %xmm3
andps %xmm0, %xmm3
maxss %xmm1, %xmm3
incq %rax
movaps %xmm3, %xmm1
cmpq %rax, %r12
jne 0x32283
jmp 0x322a0
xorps %xmm3, %xmm3
movaps %xmm3, -0xe0(%rbp)
addsd 0x24681(%rip), %xmm4    # 0x56930
movl 0x68(%rbp), %esi
movaps %xmm2, %xmm0
mulss %xmm3, %xmm0
ucomiss 0x27a1c(%rip), %xmm0    # 0x59cdc
jbe 0x32313
cvtss2sd %xmm0, %xmm0
leaq 0x28b01(%rip), %rdi     # 0x5adce
movsd 0x28a6b(%rip), %xmm1    # 0x5ad40
movb $0x2, %al
movsd %xmm4, -0x60(%rbp)
movss %xmm5, -0x2c(%rbp)
callq 0x20f0
movsd -0x60(%rbp), %xmm4
movss 0x279e9(%rip), %xmm2    # 0x59cdc
divss -0xe0(%rbp), %xmm2
movaps %xmm2, %xmm0
mulss -0x48(%rbp), %xmm0
movss -0x2c(%rbp), %xmm1
divss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movl %eax, -0x54(%rbp)
movq %r13, -0xc8(%rbp)
mulsd 0x24bbe(%rip), %xmm4    # 0x56ee0
movq %rsp, %rbx
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
cmpl $0x0, -0x30(%rbp)
jle 0x3235a
xorl %eax, %eax
movss (%r15,%rax,4), %xmm0
mulss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rbx,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x3233f
movss %xmm2, -0x8c(%rbp)
xorps %xmm0, %xmm0
cvtsd2ss %xmm4, %xmm0
movss %xmm0, -0x94(%rbp)
movq $0x0, -0x88(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x2c(%rbp)
movq -0x80(%rbp), %rdi
movq -0xa0(%rbp), %r12
movq %r12, %rsi
movq -0x70(%rbp), %r15
movl %r15d, %edx
movq -0x68(%rbp), %rcx
movq -0xa8(%rbp), %r8
xorl %r9d, %r9d
callq 0x3285f
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x14cf3
movl -0x78(%rbp), %r12d
movl -0x30(%rbp), %eax
cmpl $0x2, %eax
movl $0x1, %ecx
movl $0x1, %r13d
cmovgel %eax, %r13d
movq %r14, -0xb0(%rbp)
leaq 0x8(%r14), %rax
movq %rax, -0xd0(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x34(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x60(%rbp)
movss -0x2c(%rbp), %xmm1
testl %ecx, %ecx
movss %xmm1, -0x2c(%rbp)
je 0x32424
movq -0x88(%rbp), %r15
incl %r15d
movq -0xb0(%rbp), %r14
jmp 0x3243b
movq -0x88(%rbp), %r15
decl %r15d
movq -0xb0(%rbp), %r14
je 0x3260a
movl %ecx, -0x40(%rbp)
movq %r14, %rdi
movq -0x70(%rbp), %rsi
movq %rbx, %rdx
movl -0x30(%rbp), %ecx
callq 0x15083
cmpb $0x0, -0x50(%rbp)
je 0x3249d
movq -0xd0(%rbp), %rax
xorl %ecx, %ecx
movq -0x60(%rbp), %rdx
leaq (%rcx,%rcx,2), %rsi
shlq $0x6, %rsi
cmpl $0x0, 0x20(%r14,%rsi)
js 0x3249d
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x32475
incq %rcx
addq $0xc0, %rax
addq %rsi, %rdx
cmpq %r12, %rcx
jne 0x32463
movq -0x60(%rbp), %rdi
movq -0x78(%rbp), %rsi
callq 0x3273e
cmpl -0x54(%rbp), %r15d
movq %r15, -0x88(%rbp)
jne 0x324db
cmpl $0x0, -0x30(%rbp)
movaps 0x1ebce(%rip), %xmm1    # 0x51090
movss -0x2c(%rbp), %xmm3
jg 0x3251a
movl $0x0, -0x40(%rbp)
movb $0x1, %r14b
movl -0x54(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x3254a
cmpl $0x0, -0x40(%rbp)
movaps 0x1ebaa(%rip), %xmm1    # 0x51090
movss -0x2c(%rbp), %xmm3
je 0x32540
cmpl $0x0, -0x30(%rbp)
setle %cl
xorl %esi, %esi
ucomiss -0x94(%rbp), %xmm0
seta %r14b
setbe %al
movl -0x34(%rbp), %edx
cmoval %r15d, %edx
orb %al, %cl
je 0x3251a
movl %edx, -0x34(%rbp)
movb %al, %sil
movl %esi, -0x40(%rbp)
jmp 0x3254a
xorl %eax, %eax
movsd (%rbx,%rax,8), %xmm2
xorps %xmm1, %xmm2
movlps %xmm2, (%rbx,%rax,8)
incq %rax
cmpq %rax, %r13
jne 0x3251c
movl $0x0, -0x40(%rbp)
movb $0x1, %r14b
movl %r15d, -0x34(%rbp)
jmp 0x3254a
movl $0x0, -0x40(%rbp)
movb $0x1, %r14b
maxss %xmm3, %xmm0
movss %xmm0, -0x2c(%rbp)
cmpl $0x0, -0x44(%rbp)
sete %r15b
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0xb8(%rbp), %rdx
movq -0x78(%rbp), %rcx
callq 0x4dfa0
movss -0x2c(%rbp), %xmm1
cvtsd2ss %xmm0, %xmm0
leaq 0x20(%rbp), %rax
ucomiss 0x1c(%rax), %xmm0
movl -0x40(%rbp), %ecx
ja 0x32592
andb %r15b, %r14b
je 0x32408
movl -0x44(%rbp), %r14d
incl %r14d
movq -0xc8(%rbp), %rdi
movq -0x60(%rbp), %r15
movq %r15, %rsi
movq -0xb8(%rbp), %rdx
movq -0x78(%rbp), %rcx
callq 0x4dfa0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x90(%rbp)
movq -0x80(%rbp), %rdi
movq -0xa0(%rbp), %rsi
movq -0x70(%rbp), %rdx
movq %r15, %rcx
movq -0xa8(%rbp), %r8
movl %r14d, -0x44(%rbp)
movl %r14d, %r9d
callq 0x3285f
movl -0x40(%rbp), %ecx
movss -0x90(%rbp), %xmm0
maxss -0x4c(%rbp), %xmm0
movq -0x68(%rbp), %rax
movq %r15, -0x68(%rbp)
movss %xmm0, -0x4c(%rbp)
jmp 0x323ff
movq -0x80(%rbp), %rdi
callq 0x20c0
xorps %xmm1, %xmm1
cvtss2sd -0x48(%rbp), %xmm1
movsd 0x1eb25(%rip), %xmm0    # 0x51148
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x4c(%rbp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x2c(%rbp), %xmm2
leaq 0x287c9(%rip), %rdi     # 0x5ae07
movl 0x68(%rbp), %esi
movl -0x44(%rbp), %edx
movb $0x3, %al
callq 0x20f0
movl -0x34(%rbp), %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movss -0x8c(%rbp), %xmm2
mulss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
movaps -0xe0(%rbp), %xmm3
mulss %xmm2, %xmm3
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
leaq 0x287c0(%rip), %rdi     # 0x5ae45
movb $0x3, %al
callq 0x20f0
movq -0xc0(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq