// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1d0, %rsp            # imm = 0x1D0
movl 0x70(%rbp), %eax
movl 0x68(%rbp), %eax
movq 0x60(%rbp), %rax
leaq 0x20(%rbp), %rax
movq %rax, -0x1c8(%rbp)
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movss %xmm0, -0x34(%rbp)
movq 0x60(%rbp), %rdx
movl 0x70(%rbp), %ecx
leaq 0x370d8(%rip), %rsi     # 0x80088
xorl %eax, %eax
movb %al, -0x1b1(%rbp)
leaq -0x100(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x1200
movq -0x1c0(%rbp), %rdi
leaq 0x33399(%rip), %rsi     # 0x7c372
callq 0x11b0
movq -0x1c0(%rbp), %rsi
movq %rax, %rcx
movb -0x1b1(%rbp), %al
movq %rcx, -0x108(%rbp)
leaq 0x353a1(%rip), %rdi     # 0x7e39d
callq 0x10d0
movq -0x28(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x8(%rbp), %rdx
callq 0x1bf00
movl -0xc(%rbp), %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movl %eax, -0x10c(%rbp)
movl -0x10c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x118(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1b0(%rbp)
movq %rcx, %rsp
movq %rax, -0x120(%rbp)
movl -0x10c(%rbp), %eax
movl %eax, %edx
leaq 0xf(,%rdx,4), %rsi
andq $-0x10, %rsi
movq %rsp, %rax
subq %rsi, %rax
movq %rax, %rsp
movq %rdx, -0x128(%rbp)
movl -0x10c(%rbp), %edx
leaq 0xf(,%rdx,4), %rdi
andq $-0x10, %rdi
movq %rsp, %rsi
subq %rdi, %rsi
movq %rsi, -0x1a0(%rbp)
movq %rsi, %rsp
movq %rdx, -0x130(%rbp)
movq %rcx, -0x138(%rbp)
movq %rax, -0x140(%rbp)
movl -0xc(%rbp), %eax
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x1a8(%rbp)
movq %rdi, %rsp
movq %rax, -0x148(%rbp)
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
callq 0x496f0
movq -0x1a0(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0xc(%rbp), %edx
callq 0x49770
movl %eax, -0x14c(%rbp)
movl $0x0, -0x110(%rbp)
movl -0x110(%rbp), %eax
imull $0x3, -0x14c(%rbp), %ecx
cmpl %ecx, %eax
jge 0x4914a
movq -0x1a0(%rbp), %rax
movslq -0x110(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
movq -0x138(%rbp), %rax
movslq -0x110(%rbp), %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x49102
movq -0x1a0(%rbp), %rdi
movl -0x14c(%rbp), %esi
callq 0x49810
movq -0x1c8(%rbp), %rax
movss %xmm0, -0x150(%rbp)
movss -0x150(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x2cea1(%rip), %xmm0    # 0x76020
addsd %xmm0, %xmm1
movsd 0x32e95(%rip), %xmm0    # 0x7c020
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x154(%rbp)
movss 0x2cee5(%rip), %xmm0    # 0x76088
mulss 0x20(%rax), %xmm0
movss %xmm0, -0x158(%rbp)
movss -0x158(%rbp), %xmm0
movss -0x34(%rbp), %xmm1
cvtsi2ssl 0x18(%rbp), %xmm2
mulss %xmm2, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x15c(%rbp)
movl 0x18(%rbp), %eax
movl %eax, -0x160(%rbp)
movss 0x35b79(%rip), %xmm0    # 0x7ed5c
movss %xmm0, -0x164(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x168(%rbp)
movl $0x0, -0x110(%rbp)
movl -0x110(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4927a
movq 0x10(%rbp), %rax
movslq -0x110(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movaps 0x3326a(%rip), %xmm1    # 0x7c490
pand %xmm1, %xmm0
movss -0x168(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x49267
movq 0x10(%rbp), %rax
movslq -0x110(%rbp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movaps 0x33239(%rip), %xmm1    # 0x7c490
pand %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x168(%rbp)
jmp 0x49269
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x49200
movss -0x15c(%rbp), %xmm0
mulss -0x168(%rbp), %xmm0
ucomiss -0x164(%rbp), %xmm0
jbe 0x492ff
movl 0x68(%rbp), %esi
movss -0x15c(%rbp), %xmm0
mulss -0x168(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movss -0x164(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x36dd9(%rip), %rdi     # 0x80096
movb $0x2, %al
callq 0x10d0
movss -0x164(%rbp), %xmm0
divss -0x168(%rbp), %xmm0
movss %xmm0, -0x15c(%rbp)
movss -0x158(%rbp), %xmm0
movss -0x34(%rbp), %xmm1
mulss -0x15c(%rbp), %xmm1
divss %xmm1, %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x160(%rbp)
movl -0x1c(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1d0(%rbp)
movq %rcx, %rsp
movq %rax, -0x170(%rbp)
movl $0x0, -0x110(%rbp)
movl -0x110(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x4937a
movq -0x1d0(%rbp), %rax
movq 0x10(%rbp), %rcx
movslq -0x110(%rbp), %rdx
movss (%rcx,%rdx,4), %xmm0
mulss -0x15c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x110(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x4932f
movq -0x1b0(%rbp), %rcx
movq -0x108(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
movq -0x30(%rbp), %r8
xorl %r9d, %r9d
xorps %xmm0, %xmm0
callq 0x49a60
movq -0x28(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x8(%rbp), %rdx
callq 0x1bf00
movl $0x0, -0x174(%rbp)
movl $0x1, -0x178(%rbp)
movl $0x0, -0x17c(%rbp)
movl $0x0, -0x180(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x184(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x188(%rbp)
jmp 0x493ee
cmpl $0x0, -0x178(%rbp)
je 0x49408
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0x49427
movl -0x174(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x174(%rbp)
cmpl $0x0, -0x174(%rbp)
jne 0x49425
jmp 0x49641
jmp 0x49427
movq -0x1d0(%rbp), %rdx
movq -0x28(%rbp), %rdi
movl -0xc(%rbp), %esi
movl -0x1c(%rbp), %ecx
callq 0x1c4f0
movq -0x140(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x14c(%rbp), %edx
callq 0x49770
movq -0x140(%rbp), %rdi
movl -0x14c(%rbp), %esi
callq 0x49810
movss %xmm0, -0x150(%rbp)
movss -0x150(%rbp), %xmm0
ucomiss -0x184(%rbp), %xmm0
jbe 0x4948e
movss -0x150(%rbp), %xmm0
movss %xmm0, -0x184(%rbp)
movl -0x174(%rbp), %eax
cmpl -0x160(%rbp), %eax
je 0x494ba
cmpl $0x0, -0x178(%rbp)
je 0x4952c
movss -0x150(%rbp), %xmm0
ucomiss -0x154(%rbp), %xmm0
jbe 0x4952c
movl $0x0, -0x178(%rbp)
movl $0x0, -0x110(%rbp)
movl -0x110(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x49520
movq -0x1d0(%rbp), %rax
movslq -0x110(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq %xmm0, %rcx
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
xorq %rdx, %rcx
movq %rcx, %xmm0
movslq -0x110(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0x494ce
movl -0x174(%rbp), %eax
movl %eax, -0x180(%rbp)
movq -0x1a8(%rbp), %rdx
movq -0x138(%rbp), %rdi
movq -0x140(%rbp), %rsi
movl -0x14c(%rbp), %ecx
callq 0x70af0
movq -0x1c8(%rbp), %rax
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x18c(%rbp)
movss -0x18c(%rbp), %xmm0
ucomiss 0x1c(%rax), %xmm0
ja 0x49587
cmpl $0x0, -0x178(%rbp)
jne 0x4963c
cmpl $0x0, -0x17c(%rbp)
jne 0x4963c
movq -0x1a8(%rbp), %rdx
movq -0x1a0(%rbp), %rdi
movl -0x17c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x17c(%rbp)
movq -0x140(%rbp), %rsi
movl -0x14c(%rbp), %ecx
callq 0x70af0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x18c(%rbp)
movq -0x108(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
movq -0x140(%rbp), %rcx
movq -0x30(%rbp), %r8
movl -0x17c(%rbp), %r9d
movss -0x18c(%rbp), %xmm0
callq 0x49a60
movq -0x138(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x198(%rbp), %rax
movq %rax, -0x140(%rbp)
movss -0x18c(%rbp), %xmm0
ucomiss -0x188(%rbp), %xmm0
jbe 0x4963a
movss -0x18c(%rbp), %xmm0
movss %xmm0, -0x188(%rbp)
jmp 0x4963c
jmp 0x493ec
movq -0x108(%rbp), %rdi
callq 0x10a0
movl 0x68(%rbp), %esi
movss -0x34(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x2c9e7(%rip), %xmm0    # 0x76048
divsd %xmm1, %xmm0
movss -0x188(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
movss -0x184(%rbp), %xmm2
cvtss2sd %xmm2, %xmm2
movl -0x17c(%rbp), %edx
leaq 0x36a45(%rip), %rdi     # 0x800cf
movb $0x3, %al
callq 0x10d0
movss -0x15c(%rbp), %xmm0
cvtsi2ssl -0x180(%rbp), %xmm1
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movss -0x15c(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
movss -0x15c(%rbp), %xmm2
mulss -0x168(%rbp), %xmm2
cvtss2sd %xmm2, %xmm2
movl -0x180(%rbp), %esi
leaq 0x36a37(%rip), %rdi     # 0x8010d
movb $0x3, %al
callq 0x10d0
movq -0x118(%rbp), %rax
movq %rax, %rsp
xorl %eax, %eax
movq %rbp, %rsp
popq %rbp
retq
nop