// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/simulation.c
// Function: Make_conformations
int Make_conformations(char type, int N_frames, double *d_phi, float *d_Cart,
		       float factor, float coeff, float rmsd0,
		       float *coord_all_f, float *coord_opt_f,
		       float *coord_ref, float *coord_ref2,
		       int PRINT_SIMUL_PDB, FILE *file_out, FILE *file_pdb,
		       float *coord_str1, float *coord_str2,
		       struct Para_simul Para_simul,
		       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,
		       char *INT_TYPE, float s0, char *nameout,
		       int anhar)
{
  /* type: C= Cartesian interpolation T= torsional interpolation R= random */
  float *sigma2;
  if(anhar){sigma2=NM.sigma2_anhar;}
  else{sigma2=NM.sigma2;}

  float Ene=Energy_clashes(coord_str1, Ref.N_ref), Ene_thr=40*Ene;

  // Initialization

  char summary3[400]; sprintf(summary3, "Summary_%s_simul.dat", nameout);
  int k, i; 
  Set_bonds_measure(bonds, natoms, atoms);
  Put_coord(coord_all_f, bonds, natoms);
  Write_ref_coord(coord_ref2, Ref.N_ref, coord_all_f, Ref.atom_num);

  float *sigma=NULL;
  if(type=='R'){
    if(Ini_ch){
      Allocate_tors(&Diff_s, naxes, NM.N_Cart, NM.N);
      ali_sim.alignres=malloc(nres*sizeof(int));
      Ini_ch=0;
      ali_sim.seq_id=100; ali_sim.mammoth=0;
      for(i=0; i<nres; i++)ali_sim.alignres[i]=i;
      idum=IDUM;
    }
    sigma=malloc(NM.N*sizeof(float));
    for(k=0; k<NM.N; k++){
      if(sigma2[k]>0){sigma[k]=sqrt(sigma2[k]);}
      else{sigma[k]=0;}
    }
  }else if(type=='T'){
    float d_thr=0.01, d_max=0;
    for(i=0; i<naxes; i++)if(fabs(d_phi[i])>d_max)d_max=fabs(d_phi[i]);
    if(d_max>d_thr){
      float f=d_thr/d_max; for(i=0; i<naxes; i++)d_phi[i]*=f;
      printf("Reducing angular changes by a factor %.3f\n", f);
    }
  }


  int k_opt=0, nn=N_frames; if(type!='R')nn=1000;
  float rmsd_sim=0, rmsd1=0, rmsd2=rmsd0, rmsd_min=1000, rmsd1_min=0;

  for(k=0; k<=nn; k++){

    if(k==0)goto Print;
    if(type=='C'){
      float ck=k*coeff;
      for(i=0; i<Ref.N_Cart; i++)coord_ref[i]=coord_str1[i]+ck*d_Cart[i];
    }else{
      if(type=='R'){ // Random simulation starting from PDB
	Extract_dphi(d_phi, NM.Tors, sigma, NM.N, naxes, factor, &idum);
      }
      Build_up(bonds, natoms, d_phi, naxes);
      Put_coord(coord_all_f, bonds, natoms);
      Write_ref_coord(coord_ref, Ref.N_ref, coord_all_f, Ref.atom_num);
    }
    Ene=Energy_clashes(coord_ref, Ref.N_ref);

    // RMSD computation
    rmsd_sim=rmsd_mclachlan_f(coord_ref2, coord_ref, Ref.mass_atom, Ref.N_ref);
    for(i=0; i<Ref.N_Cart; i++)coord_ref2[i]=coord_ref[i];
    rmsd1=rmsd_mclachlan_f(coord_str1, coord_ref, Ref.mass_atom, Ref.N_ref);
    if(coord_str2){
      rmsd2=rmsd_mclachlan_f(coord_str2, coord_ref, Ref.mass_atom, Ref.N_ref);
      if(rmsd2<rmsd_min){
	Put_coord(coord_opt_f, bonds, natoms); 
	rmsd_min=rmsd2; rmsd1_min=rmsd1; k_opt=k;
      }
    }

    /*if(type=='R'){     // Projection of conformation change on normal modes
      atom atoms_sim[natoms]; int cc;
      for(i=0; i<natoms; i++)atoms_sim[i]=atoms[i];
      Examine_confchange(&Diff_s, bonds, axe, NULL, 0, summary3, nameout, "", 
			 atoms, coord_str1, seq, nres, N_diso, natoms,
			 atoms_sim, coord_ref, seq, nres, N_diso, natoms,
			 Int_list, N_int, INT_TYPE, s0, NM.N, Ref, NM, //&J, 
			 k, ali_sim, Para_simul, k, 
			 NULL, NULL, NULL, 0, anhar);
			 }*/

  Print:
    fprintf(file_out, "%d %.3f %.3f %.4f %.3g\n",
	    k, rmsd_sim, rmsd1, rmsd2, Ene);
    if((PRINT_SIMUL_PDB)&&((type=='R')||((k==0)&&(type=='T')))){
      if(Ene < Ene_thr){
	Print_PDB(file_pdb, atoms, natoms, coord_all_f, seq, k, rmsd_sim);
      }
    }
    if((type!='R')&&(rmsd2>rmsd_min))break;
  }
  char str[1000];
  sprintf(str, "# Min.RMSD= %.2f Starting_RMSD= %.2f"
	  " Opt_coeff=%.3f factor= %.3f type=%c\n",
	  rmsd_min, rmsd0, coeff*k_opt, factor, type);
  fprintf(file_out, "%s", str); printf("%s", str);
  fprintf(file_out, "&\n");
  if((PRINT_SIMUL_PDB)&&(type!='R')&&(k_opt>=0)){
    if(type=='C'){
      fprintf(file_pdb, "REMARK: predicted Cartesian mutation\n");
    }else{
      fprintf(file_pdb, "REMARK: predicted Torsional mutation\n");
    }
    Print_PDB(file_pdb, atoms, natoms, coord_opt_f, seq, k, rmsd1_min);
  }
  if(sigma)free(sigma);
  return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x618, %rsp            # imm = 0x618
movq %r9, 0x68(%rsp)
movq %r8, %r13
movss %xmm2, 0x44(%rsp)
movss %xmm1, 0x4c(%rsp)
movss %xmm0, 0x48(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x58(%rsp)
movl %esi, 0x24(%rsp)
movl %edi, 0xc(%rsp)
leaq 0x820(%rsp), %rdx
movq 0x6e8(%rsp), %r14
movl 0x6d0(%rsp), %ebp
movq 0x6c8(%rsp), %r12
movq 0x678(%rsp), %rdi
cmpl $0x0, 0x870(%rsp)
leaq 0x730(%rsp), %rax
leaq 0x790(%rsp), %rcx
cmoveq %rax, %rcx
movq (%rcx), %r15
movl (%rdx), %ebx
movl %ebx, %esi
callq 0x32b71
movss %xmm0, 0x20(%rsp)
leaq 0x234bf(%rip), %rsi     # 0x5b625
leaq 0xa0(%rsp), %rdi
movq 0x868(%rsp), %rdx
xorl %eax, %eax
callq 0x2260
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x1512b
movq %r14, %rdi
movl %ebp, %r8d
testl %ebp, %ebp
jle 0x381d6
leaq 0x8(%rdi), %rax
xorl %ecx, %ecx
movq %r13, %rdx
leaq (%rcx,%rcx,2), %rsi
shlq $0x6, %rsi
cmpl $0x0, 0x20(%rdi,%rsi)
js 0x381d6
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x381ae
incq %rcx
addq $0xc0, %rax
addq %rsi, %rdx
cmpq %r8, %rcx
jne 0x3819d
movq %r8, 0x78(%rsp)
leaq 0x820(%rsp), %rax
movq 0x28(%rax), %rbp
testl %ebx, %ebx
movq 0x38(%rsp), %r8
movq 0x678(%rsp), %r9
movl 0xc(%rsp), %r12d
jle 0x3823c
xorl %eax, %eax
movq 0x658(%rsp), %rcx
movl (%rbp,%rax,4), %edx
leal (%rdx,%rdx,2), %edx
movslq %edx, %rdx
leaq (,%rdx,4), %rsi
addq %r13, %rsi
xorl %edx, %edx
movss (%rsi,%rdx), %xmm0
movss %xmm0, (%rcx,%rdx)
addq $0x4, %rdx
cmpl $0xc, %edx
jne 0x3821e
incq %rax
addq %rdx, %rcx
cmpq %rbx, %rax
jne 0x38207
leaq 0x708(%rsp), %r14
movl 0x6e0(%rsp), %eax
cmpb $0x52, %r12b
movq %r13, 0x88(%rsp)
jne 0x3836e
cmpl $0x0, 0x30c5c(%rip)     # 0x68ec0
je 0x382dc
movl 0x6f8(%rsp), %r12d
movl (%r14), %ecx
movl 0xc(%r14), %edx
leaq 0x4129c(%rip), %rdi     # 0x79518
movl 0x6e0(%rsp), %esi
callq 0x11454
movslq %r12d, %r13
leaq (,%r13,4), %rdi
callq 0x21d0
movq %rax, 0x412d1(%rip)     # 0x79570
xorl %ecx, %ecx
movl %ecx, 0x30c19(%rip)     # 0x68ec0
movl $0x42c80000, 0x412a7(%rip) # imm = 0x42C80000
movl %ecx, 0x412c1(%rip)     # 0x79578
testl %r13d, %r13d
jle 0x382cc
movl %r12d, %ecx
xorl %edx, %edx
movl %edx, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x382c1
movq $-0xb2a3, 0x41239(%rip) # imm = 0xFFFF4D5D
movl 0xc(%rsp), %r12d
movl %r12d, %r13d
movslq (%r14), %r12
leaq (,%r12,4), %rdi
callq 0x21d0
testq %r12, %r12
movq %rax, 0x30(%rsp)
jle 0x383c5
xorl %r13d, %r13d
xorps %xmm2, %xmm2
movq 0x38(%rsp), %r8
movq 0x6e8(%rsp), %rdi
movq 0x678(%rsp), %r9
movss (%r15,%r13,4), %xmm1
ucomiss %xmm2, %xmm1
xorps %xmm0, %xmm0
jbe 0x38356
jb 0x38331
xorps %xmm0, %xmm0
sqrtss %xmm1, %xmm0
jmp 0x38356
movaps %xmm1, %xmm0
callq 0x21c0
xorps %xmm2, %xmm2
movq 0x30(%rsp), %rax
movq 0x678(%rsp), %r9
movq 0x6e8(%rsp), %rdi
movq 0x38(%rsp), %r8
movss %xmm0, (%rax,%r13,4)
incq %r13
cmpq %r13, %r12
jne 0x38318
movl 0xc(%rsp), %r12d
jmp 0x3845e
cmpb $0x54, %r12b
jne 0x38455
testl %eax, %eax
movq 0x58(%rsp), %rdx
jle 0x383df
movl %eax, %eax
xorps %xmm0, %xmm0
xorl %ecx, %ecx
movapd 0x1efa0(%rip), %xmm1    # 0x57330
movaps 0x1ef89(%rip), %xmm2    # 0x57320
movsd (%rdx,%rcx,8), %xmm3
movapd %xmm3, %xmm4
andpd %xmm1, %xmm4
xorps %xmm5, %xmm5
cvtss2sd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
jbe 0x383bb
xorps %xmm0, %xmm0
cvtsd2ss %xmm3, %xmm0
andps %xmm2, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x38397
jmp 0x383e2
movq 0x38(%rsp), %r8
movq 0x6e8(%rsp), %rdi
movq 0x678(%rsp), %r9
movl %r13d, %r12d
jmp 0x3845e
xorps %xmm0, %xmm0
ucomiss 0x1ef07(%rip), %xmm0    # 0x572f0
jbe 0x38455
movss 0x1eefd(%rip), %xmm1    # 0x572f0
divss %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
cmpl $0x0, 0x6e0(%rsp)
jle 0x38427
movl 0x6e0(%rsp), %eax
xorl %ecx, %ecx
movsd (%rdx,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x38411
leaq 0x2320c(%rip), %rdi     # 0x5b63a
movb $0x1, %al
callq 0x20f0
movq $0x0, 0x30(%rsp)
movq 0x38(%rsp), %r8
movq 0x6e8(%rsp), %rdi
movq 0x678(%rsp), %r9
jmp 0x3845e
movq $0x0, 0x30(%rsp)
movsbl %r12b, %edx
cmpb $0x52, %r12b
movl $0x3e8, %eax            # imm = 0x3E8
movl 0x24(%rsp), %ecx
cmovnel %eax, %ecx
movl %ecx, 0x24(%rsp)
testl %ecx, %ecx
js 0x38885
movl %edx, 0x60(%rsp)
movq 0x650(%rsp), %r15
movss 0x20(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
mulss 0x2294e(%rip), %xmm0    # 0x5adec
movss %xmm0, 0x20(%rsp)
movq 0x40(%r14), %rax
movq %rax, 0x98(%rsp)
movl (%r14), %eax
movl %eax, 0x64(%rsp)
leaq 0x820(%rsp), %rax
movl 0x4(%rax), %r12d
movq 0x10(%rax), %rax
movq %rax, 0x90(%rsp)
leaq 0x8(%rdi), %rax
movq %rax, 0x70(%rsp)
movq $0x0, 0x50(%rsp)
movss 0x1edf7(%rip), %xmm0    # 0x572e0
movss %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
movss 0x44(%rsp), %xmm0
movss %xmm0, 0x14(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x1c(%rsp)
movss %xmm0, 0x28(%rsp)
xorl %r14d, %r14d
movq %rbp, 0x80(%rsp)
testl %r14d, %r14d
je 0x3856a
movl 0xc(%rsp), %r13d
cmpb $0x43, %r13b
jne 0x38574
testl %r12d, %r12d
jle 0x38668
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
mulss 0x4c(%rsp), %xmm0
xorl %eax, %eax
movss (%r8,%rax,4), %xmm1
mulss %xmm0, %xmm1
addss (%r9,%rax,4), %xmm1
movss %xmm1, (%r15,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x38547
jmp 0x38668
movl 0xc(%rsp), %r13d
jmp 0x387a6
cmpb $0x52, %r13b
jne 0x385b2
movq 0x58(%rsp), %rdi
movq 0x98(%rsp), %rsi
movq 0x30(%rsp), %rdx
movl 0x64(%rsp), %ecx
movl 0x6e0(%rsp), %r8d
movss 0x48(%rsp), %xmm0
leaq 0x40f6b(%rip), %r9      # 0x79510
callq 0x32e8e
movq 0x6e8(%rsp), %rdi
movl 0x6d0(%rsp), %r13d
movl %r13d, %esi
movq 0x58(%rsp), %rdx
movl 0x6e0(%rsp), %ecx
callq 0x154bb
movq 0x6e8(%rsp), %r8
testl %r13d, %r13d
movq 0x88(%rsp), %rdi
movq 0x78(%rsp), %r9
jle 0x3862c
movq 0x70(%rsp), %rax
xorl %ecx, %ecx
movq %rdi, %rdx
leaq (%rcx,%rcx,2), %rsi
shlq $0x6, %rsi
cmpl $0x0, 0x20(%r8,%rsi)
js 0x3862c
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x38604
incq %rcx
addq $0xc0, %rax
addq %rsi, %rdx
cmpq %r9, %rcx
jne 0x385f2
testl %ebx, %ebx
movl 0xc(%rsp), %r13d
jle 0x38668
xorl %eax, %eax
movq %r15, %rcx
movl (%rbp,%rax,4), %edx
leal (%rdx,%rdx,2), %edx
movslq %edx, %rdx
leaq (%rdi,%rdx,4), %rsi
xorl %edx, %edx
movss (%rsi,%rdx), %xmm0
movss %xmm0, (%rcx,%rdx)
addq $0x4, %rdx
cmpl $0xc, %edx
jne 0x3864a
incq %rax
addq %rdx, %rcx
cmpq %rbx, %rax
jne 0x3863a
movq %r15, %rdi
movl %ebx, %esi
callq 0x32b71
movss %xmm0, 0x2c(%rsp)
movq 0x658(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq 0x90(%rsp), %rdx
movl %ebx, %ecx
callq 0x4e010
testl %r12d, %r12d
jle 0x386b0
xorl %eax, %eax
movss (%r15,%rax,4), %xmm1
movss %xmm1, (%rbp,%rax,4)
incq %rax
cmpq %rax, %r12
jne 0x3869c
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rsp)
movq 0x678(%rsp), %rdi
movq %r15, %rsi
movq 0x90(%rsp), %rbp
movq %rbp, %rdx
movl %ebx, %ecx
callq 0x4e010
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x1c(%rsp)
movq 0x680(%rsp), %rdi
testq %rdi, %rdi
je 0x3879e
movq %r15, %rsi
movq %rbp, %rdx
movl %ebx, %ecx
callq 0x4e010
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x10(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movq 0x80(%rsp), %rbp
jbe 0x387a6
cmpl $0x0, 0x6d0(%rsp)
movq 0x6e8(%rsp), %rdi
movq 0x78(%rsp), %r8
jle 0x3877c
movq 0x70(%rsp), %rax
xorl %ecx, %ecx
movq 0x68(%rsp), %rdx
leaq (%rcx,%rcx,2), %rsi
shlq $0x6, %rsi
cmpl $0x0, 0x20(%rdi,%rsi)
js 0x3877c
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rax,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x38754
incq %rcx
addq $0xc0, %rax
addq %rsi, %rdx
cmpq %r8, %rcx
jne 0x38743
movl %r14d, %eax
movq %rax, 0x50(%rsp)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
movss 0x1c(%rsp), %xmm0
movss %xmm0, 0x18(%rsp)
jmp 0x387a6
movq 0x80(%rsp), %rbp
xorps %xmm0, %xmm0
cvtss2sd 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x1c(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x14(%rsp), %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x2c(%rsp), %xmm3
movq 0x668(%rsp), %rdi
leaq 0x22e8c(%rip), %rsi     # 0x5b665
movl %r14d, %edx
movb $0x4, %al
callq 0x2190
cmpl $0x0, 0x660(%rsp)
je 0x38849
testl %r14d, %r14d
setne %al
cmpb $0x54, %r13b
setne %cl
cmpb $0x52, %r13b
setne %dl
orb %al, %cl
testb %cl, %dl
jne 0x38849
movss 0x20(%rsp), %xmm0
ucomiss 0x2c(%rsp), %xmm0
jbe 0x38849
movq 0x670(%rsp), %rdi
movq 0x6c8(%rsp), %rsi
movl 0x6d0(%rsp), %edx
movq 0x88(%rsp), %rcx
movq 0x6f0(%rsp), %r8
movl %r14d, %r9d
movss 0x28(%rsp), %xmm0
callq 0x32c92
cmpb $0x52, %r13b
movq 0x6e8(%rsp), %rdi
je 0x38864
movss 0x14(%rsp), %xmm0
ucomiss 0x10(%rsp), %xmm0
ja 0x3889e
leal 0x1(%r14), %r13d
cmpl 0x24(%rsp), %r14d
movl %r13d, %r14d
movq 0x38(%rsp), %r8
movq 0x678(%rsp), %r9
jne 0x3851e
jmp 0x388a1
xorps %xmm0, %xmm0
movss %xmm0, 0x18(%rsp)
movsd 0x2252a(%rip), %xmm0    # 0x5adc0
xorl %r13d, %r13d
xorl %r15d, %r15d
jmp 0x388b3
movl %r14d, %r13d
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
movl 0x60(%rsp), %edx
movq 0x50(%rsp), %r15
xorps %xmm2, %xmm2
cvtsi2ss %r15d, %xmm2
xorps %xmm1, %xmm1
cvtss2sd 0x44(%rsp), %xmm1
mulss 0x4c(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
xorps %xmm3, %xmm3
cvtss2sd 0x48(%rsp), %xmm3
leaq 0x22d9f(%rip), %rsi     # 0x5b67d
leaq 0x230(%rsp), %rbx
movq %rbx, %rdi
movb $0x4, %al
callq 0x2260
movq %rbx, %rdi
movq 0x668(%rsp), %r14
movq %r14, %rsi
callq 0x2100
leaq 0x1e962(%rip), %rdi     # 0x5726c
movq %rbx, %rsi
xorl %eax, %eax
callq 0x20f0
leaq 0x1f810(%rip), %rdi     # 0x5812b
movl $0x2, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2280
cmpb $0x52, 0xc(%rsp)
je 0x389a1
cmpl $0x0, 0x660(%rsp)
je 0x389a1
testl %r15d, %r15d
js 0x389a1
cmpb $0x43, 0xc(%rsp)
leaq 0x22d78(%rip), %rax     # 0x5b6c7
leaq 0x22d97(%rip), %rdi     # 0x5b6ed
cmoveq %rax, %rdi
movl $0x25, %esi
movl $0x1, %edx
movq 0x670(%rsp), %rbx
movq %rbx, %rcx
callq 0x2280
movq %rbx, %rdi
movq 0x6c8(%rsp), %rsi
movl 0x6d0(%rsp), %edx
movq 0x68(%rsp), %rcx
movq 0x6f0(%rsp), %r8
movl %r13d, %r9d
movss 0x18(%rsp), %xmm0
callq 0x32c92
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x389b0
callq 0x2050
xorl %eax, %eax
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq