// File: /workspace/github_c_makefile_star2_O1/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, -0xa0(%rbp)
movq %r8, %r14
movl %ecx, -0x30(%rbp)
movl %esi, %r15d
movq %rdi, %r12
movq 0x60(%rbp), %rdx
movl 0x70(%rbp), %ecx
leaq 0x288ba(%rip), %rsi     # 0x5ae08
leaq -0x1b0(%rbp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2260
leaq 0x24cda(%rip), %rsi     # 0x57240
movq %rbx, %rdi
callq 0x2210
movq %rax, -0x80(%rbp)
leaq 0x26c65(%rip), %rdi     # 0x591de
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
movq %r14, %rdi
movl %r15d, %esi
movq %r12, %rdx
callq 0x1512b
movq %rsp, -0xb8(%rbp)
leal (%r15,%r15,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x58(%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, -0xb0(%rbp)
movq %r12, -0x98(%rbp)
movq %r12, %rsi
movl %r15d, %edx
callq 0x32ad7
movq %r15, -0x78(%rbp)
testl %r15d, %r15d
movl $0x0, %r15d
jle 0x32656
leaq 0x8(%r14), %rax
xorl %r15d, %r15d
movq %r13, %rcx
leaq (%r15,%r15,2), %rdx
shlq $0x6, %rdx
cmpl $0x0, 0x20(%r14,%rdx)
js 0x32656
xorl %edx, %edx
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rdx,2), %xmm0
movss %xmm0, (%rcx,%rdx)
addq $0x4, %rdx
cmpq $0xc, %rdx
jne 0x32627
incq %r15
addq $0xc0, %rax
addq %rdx, %rcx
cmpq %rbx, %r15
jne 0x32615
movq -0x78(%rbp), %rax
movl %eax, %r15d
movl 0x18(%rbp), %eax
movl %eax, -0x6c(%rbp)
testl %r15d, %r15d
jle 0x32680
leal (%r15,%r15,2), %eax
cmpl $0x2, %eax
movl $0x1, %edx
cmovgel %eax, %edx
shlq $0x2, %rdx
movq -0x60(%rbp), %rdi
movq %r13, %rsi
callq 0x21a0
movq 0x10(%rbp), %rbx
movq %r13, %rdi
movq %r15, -0x68(%rbp)
movl %r15d, %esi
callq 0x32b71
cvtss2sd %xmm0, %xmm4
leaq 0x20(%rbp), %rax
movss 0x20(%rax), %xmm5
mulss 0x2483c(%rip), %xmm5    # 0x56ee4
xorps %xmm0, %xmm0
cvtsi2ssl 0x18(%rbp), %xmm0
mulss -0x48(%rbp), %xmm0
movaps %xmm5, %xmm2
divss %xmm0, %xmm2
movl -0x30(%rbp), %eax
movl %eax, %r15d
testl %eax, %eax
jle 0x326eb
xorps %xmm1, %xmm1
xorl %eax, %eax
movaps 0x24c4e(%rip), %xmm0    # 0x57320
movss (%rbx,%rax,4), %xmm3
andps %xmm0, %xmm3
maxss %xmm1, %xmm3
incq %rax
movaps %xmm3, %xmm1
cmpq %rax, %r15
jne 0x326d2
jmp 0x326ee
xorps %xmm3, %xmm3
movaps %xmm3, -0xe0(%rbp)
movq %r13, -0xc0(%rbp)
addsd 0x2423c(%rip), %xmm4    # 0x56940
movl 0x68(%rbp), %esi
movaps %xmm2, %xmm0
mulss %xmm3, %xmm0
ucomiss 0x2761f(%rip), %xmm0    # 0x59d34
jbe 0x32768
cvtss2sd %xmm0, %xmm0
leaq 0x286f4(%rip), %rdi     # 0x5ae16
movsd 0x28676(%rip), %xmm1    # 0x5ada0
movb $0x2, %al
movsd %xmm4, -0x40(%rbp)
movss %xmm5, -0x2c(%rbp)
callq 0x20f0
movsd -0x40(%rbp), %xmm4
movss 0x275ec(%rip), %xmm2    # 0x59d34
divss -0xe0(%rbp), %xmm2
movaps %xmm2, %xmm0
mulss -0x48(%rbp), %xmm0
movss -0x2c(%rbp), %xmm1
divss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movl %eax, -0x6c(%rbp)
mulsd 0x247b0(%rip), %xmm4    # 0x56f20
movq %rsp, %r13
leaq 0xf(,%r15,8), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
cmpl $0x0, -0x30(%rbp)
jle 0x327a9
xorl %eax, %eax
movss (%rbx,%rax,4), %xmm0
mulss %xmm2, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%r13,%rax,8)
incq %rax
cmpq %rax, %r15
jne 0x3278d
movss %xmm2, -0x84(%rbp)
xorps %xmm0, %xmm0
cvtsd2ss %xmm4, %xmm0
movss %xmm0, -0x8c(%rbp)
movq $0x0, -0x40(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
movq -0x80(%rbp), %rdi
movq -0x98(%rbp), %r15
movq %r15, %rsi
movq -0x78(%rbp), %rbx
movl %ebx, %edx
movq -0x60(%rbp), %rcx
movq -0xa0(%rbp), %r8
xorl %r9d, %r9d
callq 0x32c92
movq %r14, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x1512b
movl -0x68(%rbp), %r12d
movl -0x30(%rbp), %eax
cmpl $0x2, %eax
movl $0x1, -0x2c(%rbp)
movl $0x1, %ebx
cmovgel %eax, %ebx
movq %r14, -0xa8(%rbp)
leaq 0x8(%r14), %rax
movq %rax, -0xc8(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x38(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movss -0x34(%rbp), %xmm1
cmpl $0x0, -0x2c(%rbp)
movss %xmm1, -0x34(%rbp)
je 0x32874
movq -0x40(%rbp), %rax
incl %eax
movq %rax, -0x40(%rbp)
movq -0xa8(%rbp), %r14
movq -0x68(%rbp), %r15
jmp 0x3288f
movq -0x40(%rbp), %rax
decl %eax
movq %rax, -0x40(%rbp)
movq -0xa8(%rbp), %r14
movq -0x68(%rbp), %r15
je 0x32a3d
movq %r14, %rdi
movq -0x78(%rbp), %rsi
movq %r13, %rdx
movl -0x30(%rbp), %ecx
callq 0x154bb
testl %r15d, %r15d
jle 0x328ed
movq -0xc8(%rbp), %rax
xorl %ecx, %ecx
movq -0x58(%rbp), %rdx
leaq (%rcx,%rcx,2), %rsi
shlq $0x6, %rsi
cmpl $0x0, 0x20(%r14,%rsi)
js 0x328ed
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x328c5
incq %rcx
addq $0xc0, %rax
addq %rsi, %rdx
cmpq %r12, %rcx
jne 0x328b3
movq -0x58(%rbp), %rdi
movl %r15d, %esi
callq 0x32b71
movq -0x40(%rbp), %rax
cmpl -0x6c(%rbp), %eax
jne 0x32911
cmpl $0x0, -0x30(%rbp)
movaps 0x1e783(%rip), %xmm1    # 0x51090
jg 0x3294b
jmp 0x32965
cmpl $0x0, -0x2c(%rbp)
movaps 0x1e774(%rip), %xmm1    # 0x51090
je 0x32978
cmpl $0x0, -0x30(%rbp)
setle %cl
xorl %esi, %esi
ucomiss -0x8c(%rbp), %xmm0
seta %r15b
setbe %al
movl -0x38(%rbp), %edx
cmoval -0x40(%rbp), %edx
orb %al, %cl
je 0x3294b
movl %edx, -0x38(%rbp)
movb %al, %sil
movl %esi, -0x2c(%rbp)
jmp 0x32982
xorl %eax, %eax
movsd (%r13,%rax,8), %xmm2
xorps %xmm1, %xmm2
movlps %xmm2, (%r13,%rax,8)
incq %rax
cmpq %rax, %rbx
jne 0x3294d
movl $0x0, -0x2c(%rbp)
movb $0x1, %r15b
movq -0x40(%rbp), %rax
movl %eax, -0x38(%rbp)
jmp 0x32982
movl $0x0, -0x2c(%rbp)
movb $0x1, %r15b
maxss -0x34(%rbp), %xmm0
movss %xmm0, -0x34(%rbp)
cmpl $0x0, -0x44(%rbp)
sete %r14b
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x4e010
movss -0x34(%rbp), %xmm1
cvtsd2ss %xmm0, %xmm0
leaq 0x20(%rbp), %rax
ucomiss 0x1c(%rax), %xmm0
ja 0x329c8
andb %r14b, %r15b
je 0x32852
movl -0x44(%rbp), %r14d
incl %r14d
movq -0xc0(%rbp), %rdi
movq -0x58(%rbp), %r15
movq %r15, %rsi
movq -0xb0(%rbp), %rdx
movq -0x68(%rbp), %rcx
callq 0x4e010
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x88(%rbp)
movq -0x80(%rbp), %rdi
movq -0x98(%rbp), %rsi
movq -0x78(%rbp), %rdx
movq %r15, %rcx
movq -0xa0(%rbp), %r8
movl %r14d, -0x44(%rbp)
movl %r14d, %r9d
callq 0x32c92
movss -0x88(%rbp), %xmm0
maxss -0x4c(%rbp), %xmm0
movq -0x60(%rbp), %rax
movq %r15, -0x60(%rbp)
movss %xmm0, -0x4c(%rbp)
jmp 0x32849
movq -0x80(%rbp), %rdi
callq 0x20c0
xorps %xmm1, %xmm1
cvtss2sd -0x48(%rbp), %xmm1
movsd 0x1e5ea(%rip), %xmm0    # 0x51040
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtss2sd -0x4c(%rbp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd -0x34(%rbp), %xmm2
leaq 0x283de(%rip), %rdi     # 0x5ae4f
movl 0x68(%rbp), %esi
movl -0x44(%rbp), %edx
movb $0x3, %al
callq 0x20f0
movl -0x38(%rbp), %esi
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movss -0x84(%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 0x283d5(%rip), %rdi     # 0x5ae8d
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