// File: /workspace/github_c_makefile_star2_O2/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 $0x608, %rsp            # imm = 0x608
movq %r9, 0x68(%rsp)
movq %r8, %rbx
movss %xmm2, 0x3c(%rsp)
movss %xmm1, 0x4c(%rsp)
movss %xmm0, 0x40(%rsp)
movq %rcx, %r13
movq %rdx, 0x50(%rsp)
movl %esi, 0x18(%rsp)
movl %edi, 0xc(%rsp)
leaq 0x810(%rsp), %rdx
movq 0x6d8(%rsp), %r15
movq 0x668(%rsp), %rdi
cmpl $0x0, 0x860(%rsp)
leaq 0x720(%rsp), %rax
leaq 0x780(%rsp), %rcx
cmoveq %rax, %rcx
movq 0x648(%rsp), %r12
movq (%rcx), %r14
movl (%rdx), %ebp
movl %ebp, %esi
callq 0x2f3a6
movss %xmm0, 0x14(%rsp)
leaq 0x21b4e(%rip), %rsi     # 0x5553d
leaq 0x90(%rsp), %rdi
movq 0x858(%rsp), %rdx
xorl %eax, %eax
callq 0x2260
movq %r15, %rdi
movl 0x6c0(%rsp), %r15d
movl %r15d, %esi
movq 0x6b8(%rsp), %rdx
callq 0x139f6
movq %rbx, %rdi
movq 0x6d8(%rsp), %rsi
movl %r15d, %edx
movl %ebp, 0x1c(%rsp)
callq 0x2f358
leaq 0x810(%rsp), %rax
movq 0x28(%rax), %rcx
movq %r12, %rdi
movl %ebp, %esi
movl 0xc(%rsp), %ebp
movq %rbx, 0x70(%rsp)
movq %rbx, %rdx
movq %rcx, 0x88(%rsp)
callq 0x2f9b8
cmpb $0x52, %bpl
jne 0x33ae3
cmpl $0x0, 0x30451(%rip)     # 0x63ec0
leaq 0x6f8(%rsp), %rax
movslq (%rax), %r15
je 0x33b56
movl 0x6e8(%rsp), %ebx
movl 0xc(%rax), %edx
leaq 0x40a87(%rip), %rdi     # 0x74518
movl 0x6d0(%rsp), %esi
movl %r15d, %ecx
callq 0x10444
movslq %ebx, %rbx
leaq (,%rbx,4), %rdi
callq 0x21d0
movq %rax, 0x40ab9(%rip)     # 0x74570
andl $0x0, 0x30402(%rip)     # 0x63ec0
movl $0x42c80000, 0x40a90(%rip) # imm = 0x42C80000
andl $0x0, 0x40aa9(%rip)     # 0x74578
xorl %ecx, %ecx
testl %ebx, %ebx
cmovlel %ecx, %ebx
cmpq %rcx, %rbx
je 0x33b4b
movl %ecx, (%rax,%rcx,4)
incq %rcx
jmp 0x33ad6
cmpb $0x54, %bpl
jne 0x33bb3
movl 0x6d0(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
xorl %ecx, %ecx
xorps %xmm0, %xmm0
movapd 0x1d735(%rip), %xmm1    # 0x51240
movaps 0x1d71e(%rip), %xmm2    # 0x51230
movq 0x50(%rsp), %rdx
leaq 0x810(%rsp), %rbx
cmpq %rcx, %rax
je 0x33bc6
movsd (%rdx,%rcx,8), %xmm3
movapd %xmm3, %xmm4
andpd %xmm1, %xmm4
cvtss2sd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
jbe 0x33b46
cvtsd2ss %xmm3, %xmm0
andps %xmm2, %xmm0
incq %rcx
jmp 0x33b1f
movq $-0xb2a3, 0x409ba(%rip) # imm = 0xFFFF4D5D
movq %r15, %rdi
shlq $0x2, %rdi
callq 0x21d0
movq %r15, %rcx
xorl %r15d, %r15d
testl %ecx, %ecx
movl $0x0, %ebx
cmovgl %ecx, %ebx
xorps %xmm2, %xmm2
movq %rax, 0x28(%rsp)
cmpq %r15, %rbx
je 0x33bbc
movss (%r14,%r15,4), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x33ba8
ucomiss %xmm2, %xmm1
jb 0x33b98
sqrtss %xmm1, %xmm0
jmp 0x33ba8
movaps %xmm1, %xmm0
callq 0x21c0
xorps %xmm2, %xmm2
movq 0x28(%rsp), %rax
movss %xmm0, (%rax,%r15,4)
incq %r15
jmp 0x33b7a
movq $0x0, 0x28(%rsp)
leaq 0x810(%rsp), %rbx
jmp 0x33c10
ucomiss 0x1d633(%rip), %xmm0    # 0x51200
jbe 0x33c07
movss 0x1d629(%rip), %xmm1    # 0x51200
divss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x33bf9
movsd (%rdx,%rcx,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
jmp 0x33be1
leaq 0x21952(%rip), %rdi     # 0x55552
movb $0x1, %al
callq 0x20f0
movq $0x0, 0x28(%rsp)
movq 0x640(%rsp), %r15
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x24(%rsp)
mulss 0x210d0(%rip), %xmm0    # 0x54cfc
movss %xmm0, 0x14(%rsp)
cmpb $0x52, %bpl
movl $0x3e8, %eax            # imm = 0x3E8
movl 0x18(%rsp), %ecx
cmovnel %eax, %ecx
movl %ecx, 0x18(%rsp)
leaq 0x6f8(%rsp), %rax
movq 0x40(%rax), %rcx
movq %rcx, 0x80(%rsp)
movl (%rax), %eax
movl %eax, 0x64(%rsp)
movl 0x4(%rbx), %r14d
movq 0x10(%rbx), %rax
movq %rax, 0x78(%rsp)
xorl %ebx, %ebx
testl %r14d, %r14d
cmovlel %ebx, %r14d
movss 0x1d572(%rip), %xmm0    # 0x511f0
movss %xmm0, 0x10(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0x30(%rsp)
movl $0x0, 0x48(%rsp)
movss %xmm0, 0x20(%rsp)
movss 0x3c(%rsp), %xmm2
movss %xmm0, 0x44(%rsp)
movq %r13, 0x58(%rsp)
cmpl 0x18(%rsp), %ebx
movq 0x668(%rsp), %rcx
jg 0x33f31
testl %ebx, %ebx
je 0x33e81
cmpb $0x43, %bpl
movss %xmm2, 0x8(%rsp)
jne 0x33d02
cvtsi2ss %ebx, %xmm0
mulss 0x4c(%rsp), %xmm0
xorl %eax, %eax
cmpq %rax, %r14
je 0x33d8b
movss (%r13,%rax,4), %xmm1
mulss %xmm0, %xmm1
addss (%rcx,%rax,4), %xmm1
movss %xmm1, (%r15,%rax,4)
incq %rax
jmp 0x33cde
cmpb $0x52, %bpl
jne 0x33d38
movq 0x50(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x28(%rsp), %rdx
movl 0x64(%rsp), %ecx
movl 0x6d0(%rsp), %r8d
movss 0x40(%rsp), %xmm0
leaq 0x407dd(%rip), %r9      # 0x74510
callq 0x2f6b3
movq 0x6d8(%rsp), %rdi
movl 0x6c0(%rsp), %esi
movq 0x50(%rsp), %rdx
movl 0x6d0(%rsp), %ecx
callq 0x13d70
movq 0x70(%rsp), %rbp
movq %rbp, %rdi
movq 0x6d8(%rsp), %rsi
movl 0x6c0(%rsp), %edx
callq 0x2f358
movq %r15, %rdi
movl 0x1c(%rsp), %esi
movq %rbp, %rdx
movq 0x88(%rsp), %rcx
callq 0x2f9b8
movq %r15, %rdi
movl 0x1c(%rsp), %ebp
movl %ebp, %esi
callq 0x2f3a6
movss %xmm0, 0x24(%rsp)
movq %r12, %rdi
movq %r15, %rsi
movq 0x78(%rsp), %rdx
movl %ebp, %ecx
callq 0x47a64
movq 0x668(%rsp), %rdi
movsd %xmm0, 0x30(%rsp)
xorl %eax, %eax
cmpq %rax, %r14
je 0x33dd7
movss (%r15,%rax,4), %xmm0
movss %xmm0, (%r12,%rax,4)
incq %rax
jmp 0x33dc1
movq %r15, %rsi
movq 0x78(%rsp), %r13
movq %r13, %rdx
movl 0x1c(%rsp), %ebp
movl %ebp, %ecx
callq 0x47a64
cvtsd2ss 0x30(%rsp), %xmm1
movss %xmm1, 0x30(%rsp)
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x670(%rsp), %rdi
testq %rdi, %rdi
je 0x33e72
movq %r15, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x47a64
cvtsd2ss %xmm0, %xmm2
movss 0x10(%rsp), %xmm0
ucomiss %xmm2, %xmm0
jbe 0x33e67
movq 0x68(%rsp), %rdi
movq 0x6d8(%rsp), %rsi
movl 0x6c0(%rsp), %edx
movss %xmm2, 0x8(%rsp)
callq 0x2f358
movss 0x8(%rsp), %xmm2
movl %ebx, 0x48(%rsp)
movss %xmm2, 0x10(%rsp)
movss 0x20(%rsp), %xmm0
movss %xmm0, 0x44(%rsp)
movl 0xc(%rsp), %ebp
movq 0x58(%rsp), %r13
jmp 0x33e81
movl 0xc(%rsp), %ebp
movq 0x58(%rsp), %r13
movss 0x8(%rsp), %xmm2
cvtss2sd 0x30(%rsp), %xmm0
cvtss2sd 0x20(%rsp), %xmm1
movss %xmm2, 0x8(%rsp)
cvtss2sd %xmm2, %xmm2
cvtss2sd 0x24(%rsp), %xmm3
movq 0x658(%rsp), %rdi
leaq 0x216d1(%rip), %rsi     # 0x5557d
movl %ebx, %edx
movb $0x4, %al
callq 0x2190
cmpl $0x0, 0x650(%rsp)
je 0x33f17
testl %ebx, %ebx
setne %al
cmpb $0x54, %bpl
setne %cl
cmpb $0x52, %bpl
setne %dl
orb %al, %cl
testb %cl, %dl
jne 0x33f17
movss 0x14(%rsp), %xmm0
ucomiss 0x24(%rsp), %xmm0
jbe 0x33f17
movq 0x660(%rsp), %rdi
movq 0x6b8(%rsp), %rsi
movl 0x6c0(%rsp), %edx
movq 0x70(%rsp), %rcx
movq 0x6e0(%rsp), %r8
movl %ebx, %r9d
movss 0x30(%rsp), %xmm0
callq 0x2f4bd
cmpb $0x52, %bpl
movss 0x8(%rsp), %xmm2
je 0x33f2a
ucomiss 0x10(%rsp), %xmm2
ja 0x33f31
incl %ebx
jmp 0x33cac
cvtss2sd 0x10(%rsp), %xmm0
movl 0x48(%rsp), %r12d
cvtsi2ss %r12d, %xmm2
cvtss2sd 0x3c(%rsp), %xmm1
mulss 0x4c(%rsp), %xmm2
cvtss2sd %xmm2, %xmm2
cvtss2sd 0x40(%rsp), %xmm3
leaq 0x21637(%rip), %rsi     # 0x55595
leaq 0x220(%rsp), %r15
movq %r15, %rdi
movl %ebp, %edx
movb $0x4, %al
callq 0x2260
movq %r15, %rdi
movq 0x658(%rsp), %r14
movq %r14, %rsi
callq 0x2100
leaq 0x1d1f0(%rip), %rdi     # 0x5117c
movq %r15, %rsi
xorl %eax, %eax
callq 0x20f0
leaq 0x1e0ae(%rip), %rdi     # 0x5204b
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x2280
cmpb $0x52, %bpl
je 0x34019
cmpl $0x0, 0x650(%rsp)
je 0x34019
testl %r12d, %r12d
js 0x34019
cmpb $0x43, %bpl
leaq 0x21614(%rip), %rax     # 0x555df
leaq 0x21633(%rip), %rdi     # 0x55605
cmoveq %rax, %rdi
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
movq 0x660(%rsp), %r14
movq %r14, %rcx
callq 0x2280
movq %r14, %rdi
movq 0x6b8(%rsp), %rsi
movl 0x6c0(%rsp), %edx
movq 0x68(%rsp), %rcx
movq 0x6e0(%rsp), %r8
movl %ebx, %r9d
movss 0x44(%rsp), %xmm0
callq 0x2f4bd
movq 0x28(%rsp), %rdi
callq 0x2050
xorl %eax, %eax
addq $0x608, %rsp            # imm = 0x608
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq