// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/simulation.c
// Function: Simulate_ensemble_Cart
void Simulate_ensemble_Cart(int N_struct, float factor, char *name,
			    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)
{
  float *sigma2;
  if(anhar){sigma2=NM.sigma2_anhar;}
  else{sigma2=NM.sigma2;}

  if(natoms!=Ref.N_ref){
    printf("WARNING, cannot simulate Cartesian ensemble\n"); return;
  }
  idum=IDUM;
  int i, k, n3=3*natoms, N_Cart=Ref.N_Cart, N_ref=Ref.N_ref;

  atom atoms_sim[natoms]; float mass[natoms];
  for(i=0; i<natoms; i++){
    atoms_sim[i]=atoms[i];
    mass[i]=Mass(atoms+i);
  }
  float coord_all_1[n3], coord_all[n3];
  Write_all_coord_atom(coord_all_1, atoms, natoms);
  float coord_ref_1[N_Cart]; // coord_ref[N_Cart];
  Write_ref_coord_atom(coord_ref_1, N_ref, atoms, Ref.atom_num);

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

  // Initialization for Examine_confchange
  if(Ini_ch){
    Allocate_tors(&Diff_s, naxes, 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;
  char summary3[400];
  sprintf(summary3, "Summary_%s_ensemble.dat", nameout);

  struct ali_atoms ali_a; Copy_ali(&ali_a, Ref);
  float dx[N_Cart], sigma[NM.N];
  for(k=0; k<NM.N; k++){
    if(sigma2[k]>0){sigma[k]=sqrt(sigma2[k]);}
    else{sigma[k]=0;}
  }
  // Write initial PDB
  Print_PDB(file_pdb, atoms, natoms, coord_all_1, seq, 0, 0.00);
  fprintf(file_out, "%d %.3f\n", 0, 0.0);
  for(k=1; k<=N_struct; k++){
    Extract_dx(dx, NM.Cart, sigma, NM.N, N_Cart, factor, &idum);
    for(i=0; i<N_Cart; i++){
      coord_all[i]=coord_ref_1[i]+dx[i];
    }
    float rmsd=rmsd_mclachlan_f(coord_all_1, coord_all, mass, natoms);
    fprintf(file_out, "%d %.3f\n", k, rmsd);
    Print_PDB(file_pdb, atoms, natoms, coord_all, seq, k, rmsd);
    Put_atoms(atoms_sim, coord_all, natoms);
    /*Examine_confchange(&Diff_s, bonds, axe, NULL, 0, summary3, nameout, "",
		       atoms, coord_ref_1, seq, nres, N_diso, natoms,
		       atoms_sim, coord_ref, seq, nres, N_diso, natoms,  //&J, 
		       Ref, ali_a, Int_list, N_int, INT_TYPE, s0, NM.N, NM,
		       k, ali_sim, Para_simul, k, NULL, NULL, NULL, 0, anhar);
    */
  }
  printf("Distances printed in %s\n", name_out); fclose(file_out);
  printf("Simulated structures printed in %s\n", name_pdb);
  fclose(file_pdb);
  if(Ini_ch==0){Empty_tors(Diff_s); free(ali_sim.alignres); Ini_ch=1;}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
movq %rdx, %r13
movss %xmm0, -0x88(%rbp)
movl %edi, -0x4c(%rbp)
movq %rcx, %rdx
movl 0x1d8(%rbp), %edi
testl %edi, %edi
leaq 0x98(%rbp), %rcx
leaq 0xf8(%rbp), %rax
cmoveq %rcx, %rax
movl 0x188(%rbp), %ecx
cmpl %edx, %ecx
jne 0x341aa
movq %rcx, -0x70(%rbp)
movq %rsi, -0x38(%rbp)
movl %edi, -0x40(%rbp)
movl %r9d, -0x84(%rbp)
movq %rsp, -0xa8(%rbp)
movq 0x1d0(%rbp), %rcx
movq %rcx, -0x48(%rbp)
leaq 0x188(%rbp), %rsi
movl 0x60(%rbp), %ecx
movl %ecx, -0x60(%rbp)
movq 0x58(%rbp), %rcx
movq %rcx, -0x98(%rbp)
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq $-0xb2a3, 0x45869(%rip) # imm = 0xFFFF4D5D
leal (%rdx,%rdx,2), %eax
movl %eax, -0x30(%rbp)
movl 0x4(%rsi), %eax
movq %rax, -0x78(%rbp)
movl %edx, %ebx
movq %rsp, %rax
imulq $0x70, %rbx, %r15
subq %r15, %rax
movq %rax, -0xa0(%rbp)
movq %rax, %rsp
movq %rsp, %rcx
leaq 0xf(,%rbx,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x90(%rbp)
movq %rcx, %rsp
movq %rdx, -0x80(%rbp)
testl %edx, %edx
jle 0x33d2d
xorl %r14d, %r14d
movq -0x90(%rbp), %r12
movq -0xa0(%rbp), %rax
leaq (%rax,%r14), %rdi
leaq (%r14,%r13), %rax
movl $0xe, %ecx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
callq 0x25e79
movss %xmm0, (%r12)
addq $0x4, %r12
addq $0x70, %r14
cmpq %r14, %r15
jne 0x33cf8
movl -0x30(%rbp), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
movq %rsp, %r15
subq %rax, %r15
movq %r15, %rsp
cmpl $0x0, -0x80(%rbp)
movq -0x70(%rbp), %r8
jle 0x33d8c
xorl %eax, %eax
movq %r13, %rcx
movq -0x30(%rbp), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rcx,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpl $0xc, %esi
jne 0x33d67
incq %rax
addq $0x70, %rcx
addq %rsi, %rdx
cmpq %rbx, %rax
jne 0x33d65
movq %rsp, %rbx
movq -0x78(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0x68(%rbp)
subq %rax, %rbx
movq %rbx, %rsp
testl %r8d, %r8d
jle 0x33dec
leaq 0x188(%rbp), %rax
movq 0x28(%rax), %rax
xorl %ecx, %ecx
movq %rbx, %rdx
movslq (%rax,%rcx,4), %rsi
imulq $0x70, %rsi, %rdi
addq %r13, %rdi
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rdi,%rsi,2), %xmm0
movss %xmm0, (%rdx,%rsi)
addq $0x4, %rsi
cmpl $0xc, %esi
jne 0x33dcb
incq %rcx
addq %rsi, %rdx
cmpq %r8, %rcx
jne 0x33dbe
leaq 0x272cf(%rip), %rsi     # 0x5b0c2
leaq -0x110(%rbp), %rdi
movq -0x48(%rbp), %r14
movq %r14, %rdx
movq -0x38(%rbp), %r12
movq %r12, %rcx
xorl %eax, %eax
callq 0x2260
leaq 0x2342a(%rip), %rsi     # 0x57240
leaq -0x110(%rbp), %rdi
callq 0x2210
leaq 0x272b1(%rip), %rsi     # 0x5b0da
leaq -0x1e0(%rbp), %rdi
movq %r14, %rdx
movq %rax, %r14
movq %r12, %rcx
xorl %eax, %eax
callq 0x2260
leaq -0x1e0(%rbp), %rdi
leaq 0x233f2(%rip), %rsi     # 0x57240
callq 0x2210
movq %rax, %r12
leaq 0x27295(%rip), %rdi     # 0x5b0f2
movl $0xd, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2280
cmpl $0x0, -0x40(%rbp)
je 0x33ea7
leaq 0x27128(%rip), %rdi     # 0x5afa4
movl $0x25, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x2280
leaq 0x27135(%rip), %rdi     # 0x5afca
movl $0x2a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2280
movq %r14, -0x40(%rbp)
leaq 0x70(%rbp), %rax
cmpl $0x0, 0x3500a(%rip)     # 0x68ec0
movl -0x60(%rbp), %r14d
je 0x33ef1
movl (%rax), %ecx
leaq 0x45653(%rip), %rdi     # 0x79518
movl -0x84(%rbp), %esi
movq -0x78(%rbp), %rdx
callq 0x11454
movslq %r14d, %rdi
shlq $0x2, %rdi
callq 0x21d0
movq %rax, 0x45689(%rip)     # 0x79570
movl $0x0, 0x34fcf(%rip)     # 0x68ec0
movq %r12, -0x38(%rbp)
movl $0x42c80000, 0x45659(%rip) # imm = 0x42C80000
movl $0x0, 0x4566f(%rip)     # 0x79578
testl %r14d, %r14d
jle 0x33f25
movq 0x4565b(%rip), %rax     # 0x79570
movl %r14d, %ecx
xorl %edx, %edx
movl %edx, (%rax,%rdx,4)
incq %rdx
cmpq %rdx, %rcx
jne 0x33f1a
leaq 0x271d4(%rip), %rsi     # 0x5b100
leaq -0x370(%rbp), %rdi
movq -0x48(%rbp), %rdx
xorl %eax, %eax
callq 0x2260
movq %rsp, %r12
subq -0x68(%rbp), %r12
movq %r12, %rsp
leaq 0x70(%rbp), %rax
movl (%rax), %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,4), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, -0x60(%rbp)
movq %rdx, %rsp
movq %rcx, -0x48(%rbp)
testl %ecx, %ecx
movq -0x58(%rbp), %rax
jle 0x33fb4
xorl %r14d, %r14d
xorps %xmm2, %xmm2
movss (%rax,%r14,4), %xmm1
ucomiss %xmm2, %xmm1
xorps %xmm0, %xmm0
jbe 0x33fa1
jb 0x33f92
xorps %xmm0, %xmm0
sqrtss %xmm1, %xmm0
jmp 0x33fa1
movaps %xmm1, %xmm0
callq 0x21c0
xorps %xmm2, %xmm2
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
movss %xmm0, (%rcx,%r14,4)
incq %r14
cmpq %r14, -0x48(%rbp)
jne 0x33f79
xorps %xmm0, %xmm0
movq -0x40(%rbp), %rdi
movq %r13, -0x70(%rbp)
movq %r13, %rsi
movq -0x80(%rbp), %rdx
movq -0x30(%rbp), %rcx
movq -0x98(%rbp), %r8
xorl %r9d, %r9d
callq 0x32c92
leaq 0x27138(%rip), %rsi     # 0x5b118
xorps %xmm0, %xmm0
movq -0x38(%rbp), %rdi
xorl %edx, %edx
movb $0x1, %al
callq 0x2190
cmpl $0x0, -0x4c(%rbp)
jle 0x340fd
leaq 0x70(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x1, %r14d
movq %r12, %rdi
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x78(%rbp), %r13
movl %r13d, %r8d
movss -0x88(%rbp), %xmm0
leaq 0x454df(%rip), %r9      # 0x79510
callq 0x32f5a
testl %r13d, %r13d
jle 0x34056
xorl %eax, %eax
movss (%rbx,%rax,4), %xmm0
addss (%r12,%rax,4), %xmm0
movss %xmm0, (%r15,%rax,4)
incq %rax
cmpq %rax, %r13
jne 0x3403d
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq -0x90(%rbp), %rdx
movq -0x80(%rbp), %r13
movl %r13d, %ecx
callq 0x4e010
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x58(%rbp)
cvtss2sd %xmm0, %xmm0
movq -0x38(%rbp), %rdi
leaq 0x27090(%rip), %rsi     # 0x5b118
movl %r14d, %edx
movb $0x1, %al
callq 0x2190
movq -0x40(%rbp), %rdi
movq -0x70(%rbp), %rsi
movl %r13d, %edx
movq %r15, %rcx
movq -0x98(%rbp), %r8
movl %r14d, %r9d
movss -0x58(%rbp), %xmm0
callq 0x32c92
testl %r13d, %r13d
movq -0xa0(%rbp), %rdx
jle 0x340ec
xorl %eax, %eax
movq %r15, %rcx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtss2sd (%rcx,%rsi), %xmm0
movsd %xmm0, (%rdx,%rsi,2)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x340c7
addq $0x70, %rdx
incl %eax
addq %rsi, %rcx
cmpl %r13d, %eax
jne 0x340c5
leal 0x1(%r14), %eax
cmpl -0x4c(%rbp), %r14d
movl %eax, %r14d
jne 0x3400c
leaq 0x26f17(%rip), %rdi     # 0x5b01b
leaq -0x1e0(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq -0x38(%rbp), %rdi
callq 0x20c0
leaq 0x26fff(%rip), %rdi     # 0x5b121
leaq -0x110(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq -0x40(%rbp), %rdi
callq 0x20c0
cmpl $0x0, 0x34d80(%rip)     # 0x68ec0
jne 0x34194
subq $0x40, %rsp
movups 0x453fb(%rip), %xmm0    # 0x79548
movups %xmm0, 0x30(%rsp)
movups 0x453df(%rip), %xmm0    # 0x79538
movups %xmm0, 0x20(%rsp)
movups 0x453c3(%rip), %xmm0    # 0x79528
movups %xmm0, 0x10(%rsp)
movups 0x453a7(%rip), %xmm0    # 0x79518
movups %xmm0, (%rsp)
callq 0x114b1
addq $0x40, %rsp
movq 0x453eb(%rip), %rdi     # 0x79570
callq 0x2050
movl $0x1, 0x34d2c(%rip)     # 0x68ec0
movq -0xa8(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27aaa(%rip), %rdi     # 0x5bc5b
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2090