// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/simulation.c
// Function: Simulate_ensemble
int Simulate_ensemble(int N_struct, float factor, char *name,
		      struct Para_simul Para_simul, float Mass_sqrt,
		      atom *atoms, int natoms,
		      struct axe *axe, int naxes,
		      struct bond *bonds,
		      struct residue *seq, int nres,
		      int N_diso, struct Normal_Mode NM,
		      struct Jacobian J, struct Reference Ref,
		      struct interaction *Int_list, int N_int,
		      struct interaction **Int_KB,
		      char *INT_TYPE, float s0, char *nameout,
		      int anhar)
{
  float E_thr=factor*factor*log(4*N_struct);
  float RMSD_mod_thr=0.02;
  float RMSD_min_fact=0.1;
  float sigma_thr=Mass_sqrt*RMSD_mod_thr/factor;
  float c_thr=Mass_sqrt*RMSD_mod_thr;
  float gasdev(long *idum);

  float *sigma2; int i, k;
  if(anhar){sigma2=NM.sigma2_anhar;}
  else{sigma2=NM.sigma2;}
  float sigma[NM.N];
  for(k=0; k<NM.N; k++){
    if(sigma2[k]>0){sigma[k]=sqrt(sigma2[k]);}
    else{sigma[k]=0;}
  }
  double RMSD_all=0;
  for(k=0; k<NM.N; k++){
    if(sigma[k]<sigma_thr)break;
    RMSD_all+=sigma2[k];
  }
  RMSD_all=sqrt(RMSD_all)/Mass_sqrt;
  float RMSD_thr=factor*RMSD_all*RMSD_min_fact;

  idum=randomgenerator();
  int n3=3*natoms;
  float mass[natoms]; for(i=0; i<natoms; i++)mass[i]=Mass(atoms+i);
  float coord_all_1[n3], coord_all[n3];
  Write_all_coord_atom(coord_all_1, atoms, natoms);

  // Open files
  char name_pdb[100], name_out[200];
  sprintf(name_pdb, "%s_%s_ensemble.pdb", nameout, name);
  FILE *file_pdb=fopen(name_pdb, "w");
  sprintf(name_out, "%s_%s_ensemble.dat", nameout, name);
  FILE *file_out=fopen(name_out, "w");
  fprintf(file_out, "#struct rmsd energy modes accept\n");
  if(anhar){
    fprintf(file_out, "# Omega2 corrected for anharmonicity\n");
    fprintf(file_pdb, "REMARK Omega2 corrected for anharmonicity\n");
  }

  double d_phi[naxes]; for(i=0; i<naxes; i++)d_phi[i]=0;
  float Ene_anhar,
    Ene_anhar_ini=Energy_anharmonic(coord_all_1,atoms,natoms,
				    seq,nres,Int_list,N_int,Int_KB,20,
				    axe, naxes, d_phi);
  // Write initial PDB
  //fprintf(file_pdb, "MODEL 0  RMSD from PDB= 0.0\n");
  //Print_PDB(file_pdb, atoms, natoms, coord_all_1, seq, 0, 0.00);
  fprintf(file_out, "%d %.3f %.3g 0 1\n", 0, 0.0, 0.0);

  int nsel=0, ndisc=0, ndisc_max=50, nconf=1;
  struct bond *bonds2=Set_bonds_topology(natoms, atoms, seq);
  //struct ali_atoms ali_a; Copy_ali(&ali_a, Ref);
  while(nsel<N_struct){
    if(ndisc>=ndisc_max)break;
    Set_bonds_measure(bonds, natoms, atoms);
    int nmode=0;
    for(k=0; k<NM.N; k++){
      // Change the structure with each relevant normal mode
      if(sigma[k]<sigma_thr)break;
      float c = gasdev(&idum)*factor*sigma[k], *mode=NM.Tors[k];
      if(c<c_thr)continue;
      nmode++;
      for(i=0; i<naxes; i++){d_phi[i]=c*mode[i];}
      Copy_bonds(bonds2, bonds, natoms);
      Build_up(bonds2, natoms, d_phi, naxes);
      Put_coord(coord_all, bonds2, natoms);
      Ene_anhar=Energy_anharmonic(coord_all,atoms,natoms,
				  seq,nres,Int_list,N_int,Int_KB,20,
				  axe, naxes, d_phi);
      Ene_anhar-= Ene_anhar_ini;
      if(Ene_anhar<=E_thr)Copy_bonds(bonds, bonds2, natoms); // accept
    }
    Put_coord(coord_all, bonds, natoms);
    float rmsd=rmsd_mclachlan_f(coord_all_1, coord_all, mass, natoms);
    Ene_anhar=Energy_anharmonic(coord_all,atoms,natoms,
				seq,nres,Int_list,N_int,Int_KB,20,
				axe, naxes, d_phi);
    Ene_anhar-= Ene_anhar_ini;
    fprintf(file_out, "%d %.3f %.3g %d", nconf, rmsd, Ene_anhar, nmode);
    nconf++;
    if(Ene_anhar>E_thr || rmsd<RMSD_thr){
      ndisc++; fprintf(file_out, " 0\n"); continue;
    }
    nsel++; fprintf(file_out, " 1\n");
    Print_PDB(file_pdb, atoms, natoms, coord_all, seq, nsel, rmsd);
  }
  printf("Distances printed in %s\n", name_out); fclose(file_out);
  if(nsel)printf("%d simulated structures printed in %s\n", nsel, name_pdb);
  fclose(file_pdb);
  Set_bonds_measure(bonds, natoms, atoms);
  //if(Ini_ch==0){Empty_tors(Diff_s); free(ali_sim.alignres); Ini_ch=1;}
  if(ndisc>=ndisc_max){
    printf("WARNING, amplitude factor= %.1f", factor);
    printf(" only %d over %d structure printed in ensemble\n",nsel,N_struct);
    printf("Rejection ratio= %.1f\n", (float)nconf/nsel-1);
    return(-1);
  }
  return(nsel);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %r9, -0x60(%rbp)
movq %r8, -0xa8(%rbp)
movq %rcx, -0x58(%rbp)
movq %rdx, -0x38(%rbp)
movss %xmm1, -0x30(%rbp)
movq %rsi, %r12
movaps %xmm0, %xmm1
movq 0x1d8(%rbp), %rbx
movq %rdi, -0xd0(%rbp)
leal (,%rdi,4), %eax
cvtsi2sd %eax, %xmm0
movss %xmm1, -0x44(%rbp)
mulss %xmm1, %xmm1
cvtss2sd %xmm1, %xmm1
movsd %xmm1, -0x68(%rbp)
callq 0x2120
mulsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movss 0x25224(%rip), %xmm0    # 0x54cf0
mulss -0x30(%rbp), %xmm0
movss %xmm0, -0x74(%rbp)
cmpl $0x0, 0x1e0(%rbp)
leaq 0x98(%rbp), %rax
leaq 0xf8(%rbp), %rcx
cmoveq %rax, %rcx
movq (%rcx), %r14
movl -0x28(%rax), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0xb0(%rbp)
movq %rdx, %rsp
xorl %r15d, %r15d
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movq %rcx, -0x68(%rbp)
xorps %xmm2, %xmm2
cmpq %r15, -0x68(%rbp)
je 0x2fb61
movss (%r14,%r15,4), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x2fb4f
ucomiss %xmm2, %xmm1
jb 0x2fb44
sqrtss %xmm1, %xmm0
jmp 0x2fb4f
movaps %xmm1, %xmm0
callq 0x21c0
xorps %xmm2, %xmm2
movq -0xb0(%rbp), %rax
movss %xmm0, (%rax,%r15,4)
incq %r15
jmp 0x2fb25
cvtsd2ss -0x98(%rbp), %xmm0
movss %xmm0, -0x70(%rbp)
movss -0x74(%rbp), %xmm0
divss -0x44(%rbp), %xmm0
movss %xmm0, -0x98(%rbp)
xorps %xmm0, %xmm0
xorl %eax, %eax
movq -0x58(%rbp), %r15
movq -0xb0(%rbp), %rcx
cmpq %rax, -0x68(%rbp)
je 0x2fbb3
movss -0x98(%rbp), %xmm1
ucomiss (%rcx,%rax,4), %xmm1
ja 0x2fbb3
cvtss2sd (%r14,%rax,4), %xmm1
addsd %xmm1, %xmm0
incq %rax
jmp 0x2fb90
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %r12, -0x90(%rbp)
jb 0x2fbc9
sqrtsd %xmm0, %xmm0
jmp 0x2fbce
callq 0x2290
cvtss2sd -0x30(%rbp), %xmm1
divsd %xmm1, %xmm0
cvtss2sd -0x44(%rbp), %xmm1
movsd %xmm1, -0xd8(%rbp)
mulsd %xmm1, %xmm0
mulsd 0x23fe0(%rip), %xmm0    # 0x53bd0
movsd %xmm0, -0x30(%rbp)
callq 0x2f672
movq %rax, 0x4490f(%rip)     # 0x74510
movl %r15d, %eax
movq %rsp, %r13
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
xorl %r14d, %r14d
testl %r15d, %r15d
movq %r15, %rax
movl $0x0, %r15d
cmovgl %eax, %r15d
movq -0x38(%rbp), %r12
cmpq %r14, %r15
je 0x2fc4d
movq %r12, %rdi
callq 0x233c9
movss %xmm0, (%r13,%r14,4)
incq %r14
addq $0x70, %r12
jmp 0x2fc30
movq %r13, -0xe0(%rbp)
movq -0x58(%rbp), %rdx
leal (%rdx,%rdx,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, %rsp
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0xa0(%rbp)
movq %rcx, %rsp
movq %rdi, -0xc8(%rbp)
movq -0x38(%rbp), %rsi
callq 0x2fa25
leaq 0x251dd(%rip), %rsi     # 0x54e74
xorl %r15d, %r15d
leaq -0x150(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movq -0x90(%rbp), %r12
movq %r12, %rcx
xorl %eax, %eax
callq 0x2260
leaq 0x21491(%rip), %r14     # 0x51150
movq %r13, %rdi
movq %r14, %rsi
callq 0x2210
leaq 0x251b6(%rip), %rsi     # 0x54e87
leaq -0x220(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movq %rax, %rbx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2260
movq %r13, %rdi
movq %r14, %rsi
callq 0x2210
movq %rax, %r14
leaq 0x2519a(%rip), %rdi     # 0x54e9a
pushq $0x21
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2280
cmpl $0x0, 0x1e0(%rbp)
je 0x2fd45
leaq 0x2519e(%rip), %rdi     # 0x54ebc
pushq $0x25
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r14, %rcx
callq 0x2280
leaq 0x251ab(%rip), %rdi     # 0x54ee2
pushq $0x2a
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x2280
movq %r14, -0x50(%rbp)
movq %rbx, -0xc0(%rbp)
cvtsd2ss -0x30(%rbp), %xmm0
movss %xmm0, -0xb4(%rbp)
movq -0x60(%rbp), %rcx
movl %ecx, %eax
movq %rsp, %rbx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
testl %ecx, %ecx
movl $0x0, %r12d
cmovgl %ecx, %r12d
movq 0x50(%rbp), %r13
cmpq %r15, %r12
je 0x2fd97
andq $0x0, (%rbx,%r15,8)
incq %r15
jmp 0x2fd88
movq -0xc8(%rbp), %rdi
movq -0x38(%rbp), %r14
movq %r14, %rsi
movq -0x58(%rbp), %r15
movl %r15d, %edx
movq 0x58(%rbp), %rcx
movl 0x60(%rbp), %r8d
movq 0x1b8(%rbp), %r9
pushq %rbx
pushq -0x60(%rbp)
pushq -0xa8(%rbp)
pushq $0x14
pushq 0x1c8(%rbp)
movl 0x1c0(%rbp), %eax
pushq %rax
callq 0x19587
addq $0x30, %rsp
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x6c(%rbp)
leaq 0x25120(%rip), %rsi     # 0x54f0d
movq $0x0, -0x80(%rbp)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
movq -0x50(%rbp), %rdi
xorl %edx, %edx
movb $0x2, %al
callq 0x2190
movl %r15d, %edi
movq %r14, %rsi
movq 0x58(%rbp), %rdx
callq 0x14356
movq %rax, %r14
leaq 0x70(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0xe8(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movq %r14, -0x88(%rbp)
movq -0x80(%rbp), %rax
cmpl -0xd0(%rbp), %eax
jge 0x30104
cmpl $0x31, -0x40(%rbp)
jg 0x30104
movq %r13, %rdi
movl %r15d, %esi
movq -0x38(%rbp), %rdx
callq 0x139f6
xorl %r15d, %r15d
movl $0x0, -0x90(%rbp)
cmpq -0x68(%rbp), %r15
je 0x2ffba
movq -0xb0(%rbp), %rax
movss (%rax,%r15,4), %xmm1
movss -0x98(%rbp), %xmm0
ucomiss %xmm1, %xmm0
ja 0x2ffba
leaq 0x4466e(%rip), %rdi     # 0x74510
movss %xmm1, -0x30(%rbp)
callq 0x49cbc
movss -0x30(%rbp), %xmm1
mulss -0x44(%rbp), %xmm0
mulss %xmm0, %xmm1
movss -0x74(%rbp), %xmm0
ucomiss %xmm1, %xmm0
ja 0x2ffb2
movq -0xe8(%rbp), %rax
movq (%rax,%r15,8), %rax
xorl %ecx, %ecx
cmpq %rcx, %r12
je 0x2fef1
movss (%rax,%rcx,4), %xmm0
mulss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rbx,%rcx,8)
incq %rcx
jmp 0x2fed5
movq %r15, -0x30(%rbp)
incl -0x90(%rbp)
movq %r14, %rdi
movq %r13, %rsi
movq -0x58(%rbp), %r13
movl %r13d, %edx
callq 0x301ca
movq %r14, %rdi
movl %r13d, %esi
movq %rbx, %rdx
movq -0x60(%rbp), %rcx
callq 0x13d70
movq -0xa0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl %r13d, %edx
callq 0x2f358
movq %r15, %rdi
movq -0x38(%rbp), %rsi
movl %r13d, %edx
movq 0x58(%rbp), %rcx
movl 0x60(%rbp), %r8d
movq 0x1b8(%rbp), %r9
pushq %rbx
pushq -0x60(%rbp)
pushq -0xa8(%rbp)
pushq $0x14
pushq 0x1c8(%rbp)
movl 0x1c0(%rbp), %eax
pushq %rax
callq 0x19587
addq $0x30, %rsp
cvtsd2ss %xmm0, %xmm0
subss -0x6c(%rbp), %xmm0
movss -0x70(%rbp), %xmm1
ucomiss %xmm0, %xmm1
jae 0x2ff8f
movq 0x50(%rbp), %r13
movq -0x88(%rbp), %r14
jmp 0x2ffae
movq 0x50(%rbp), %r14
movq %r14, %rdi
movq -0x88(%rbp), %r15
movq %r15, %rsi
movl %r13d, %edx
callq 0x301ca
movq %r14, %r13
movq %r15, %r14
movq -0x30(%rbp), %r15
incq %r15
jmp 0x2fe73
movq -0xa0(%rbp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq -0x58(%rbp), %r15
movl %r15d, %edx
callq 0x2f358
movq -0xc8(%rbp), %rdi
movq %r14, %rsi
movq -0xe0(%rbp), %rdx
movl %r15d, %ecx
callq 0x47a64
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x30(%rbp)
movq %r14, %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movq 0x58(%rbp), %rcx
movl 0x60(%rbp), %r8d
movq 0x1b8(%rbp), %r9
pushq %rbx
pushq -0x60(%rbp)
pushq -0xa8(%rbp)
pushq $0x14
pushq 0x1c8(%rbp)
movl 0x1c0(%rbp), %eax
pushq %rax
callq 0x19587
addq $0x30, %rsp
cvtsd2ss %xmm0, %xmm1
subss -0x6c(%rbp), %xmm1
movss %xmm1, -0xb8(%rbp)
cvtss2sd -0x30(%rbp), %xmm0
cvtss2sd %xmm1, %xmm1
movq -0x50(%rbp), %rdi
leaq 0x24eca(%rip), %rsi     # 0x54f1f
movl -0x3c(%rbp), %r14d
movl %r14d, %edx
movl -0x90(%rbp), %ecx
movb $0x2, %al
callq 0x2190
incl %r14d
movl %r14d, -0x3c(%rbp)
movss -0xb8(%rbp), %xmm0
ucomiss -0x70(%rbp), %xmm0
ja 0x3008c
movss -0xb4(%rbp), %xmm0
ucomiss -0x30(%rbp), %xmm0
jbe 0x300b1
incl -0x40(%rbp)
leaq 0x24e99(%rip), %rdi     # 0x54f2f
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq -0x50(%rbp), %rcx
callq 0x2280
movq -0x88(%rbp), %r14
jmp 0x2fe3d
movq -0x80(%rbp), %r14
incl %r14d
leaq 0x24e5c(%rip), %rdi     # 0x54f1b
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq -0x50(%rbp), %rcx
callq 0x2280
movq -0xc0(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl %r15d, %edx
movq -0xa0(%rbp), %rcx
movq 0x58(%rbp), %r8
movq %r14, -0x80(%rbp)
movl %r14d, %r9d
movss -0x30(%rbp), %xmm0
callq 0x2f4bd
movq -0x88(%rbp), %r14
jmp 0x2fe3d
leaq 0x24e28(%rip), %rdi     # 0x54f33
leaq -0x220(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq -0x50(%rbp), %rdi
callq 0x20c0
movq -0x80(%rbp), %rbx
testl %ebx, %ebx
je 0x30141
leaq 0x24e1b(%rip), %rdi     # 0x54f4c
leaq -0x150(%rbp), %rdx
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
movq -0xc0(%rbp), %rdi
callq 0x20c0
movq %r13, %rdi
movl %r15d, %esi
movq -0x38(%rbp), %rdx
callq 0x139f6
cmpl $0x32, -0x40(%rbp)
jl 0x301b9
leaq 0x24e0a(%rip), %rdi     # 0x54f73
movsd -0xd8(%rbp), %xmm0
movb $0x1, %al
callq 0x20f0
leaq 0x24e14(%rip), %rdi     # 0x54f93
movl %ebx, %esi
movq -0xd0(%rbp), %rdx
xorl %eax, %eax
callq 0x20f0
cvtsi2ssl -0x3c(%rbp), %xmm0
cvtsi2ss %ebx, %xmm1
divss %xmm1, %xmm0
addss 0x1ae60(%rip), %xmm0    # 0x4b004
cvtss2sd %xmm0, %xmm0
leaq 0x24e14(%rip), %rdi     # 0x54fc3
movb $0x1, %al
callq 0x20f0
pushq $-0x1
popq %rbx
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq