// File: /workspace/github_c_makefile_star2_O3/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 $0x358, %rsp            # imm = 0x358
movl %ecx, %ebx
movss %xmm0, -0x80(%rbp)
movl %edi, -0x4c(%rbp)
movl 0x1d8(%rbp), %edi
testl %edi, %edi
leaq 0x98(%rbp), %rcx
leaq 0xf8(%rbp), %rax
cmoveq %rcx, %rax
cmpl %ebx, 0x188(%rbp)
jne 0x33a24
movl %r9d, -0x7c(%rbp)
movq %rsp, -0xa0(%rbp)
movq 0x1d0(%rbp), %rcx
movq %rcx, -0x38(%rbp)
leaq 0x188(%rbp), %r8
movl 0x60(%rbp), %ecx
movq %rcx, -0x60(%rbp)
movq 0x58(%rbp), %rcx
movq %rcx, -0x88(%rbp)
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq $-0xb2a3, 0x45c48(%rip) # imm = 0xFFFF4D5D
leal (%rbx,%rbx,2), %r10d
movl 0x4(%r8), %ecx
movl %ebx, %r9d
movq %rsp, %r8
imulq $0x70, %r9, %r13
subq %r13, %r8
movq %r8, %rsp
movq %rsp, %r15
leaq 0xf(,%r9,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
testl %ebx, %ebx
movq %rdx, -0x78(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x90(%rbp)
movq %r15, -0xb0(%rbp)
movl %edi, -0x40(%rbp)
movq %rsi, -0x68(%rbp)
jle 0x33a3e
movl %r10d, -0x30(%rbp)
movq %r9, -0x98(%rbp)
xorl %r12d, %r12d
movq %rdx, %r14
leaq (%r8,%r12), %rdi
leaq (%r14,%r12), %rax
movl $0xe, %ecx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
callq 0x25a4d
movq -0x90(%rbp), %r8
movss %xmm0, (%r15)
addq $0x4, %r15
addq $0x70, %r12
cmpq %r12, %r13
jne 0x33923
movl -0x30(%rbp), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
testl %ebx, %ebx
movq %rdx, -0x70(%rbp)
jle 0x33a65
xorl %eax, %eax
movq -0x78(%rbp), %r8
movq %r8, %rcx
movq %rdx, %rdi
movq -0x38(%rbp), %r14
movq -0x98(%rbp), %r10
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rcx,%rsi,2), %xmm0
movss %xmm0, (%rdi,%rsi)
addq $0x4, %rsi
cmpl $0xc, %esi
jne 0x3399d
incq %rax
addq $0x70, %rcx
addq %rsi, %rdi
cmpq %r10, %rax
jne 0x3399b
movq %rsp, %r13
movq -0x48(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
testl %ebx, %ebx
jle 0x33a82
leaq 0x188(%rbp), %rax
movq 0x28(%rax), %rax
xorl %ecx, %ecx
movq %r13, %r9
movslq (%rax,%rcx,4), %rsi
imulq $0x70, %rsi, %rdi
addq %r8, %rdi
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsd2ss (%rdi,%rsi,2), %xmm0
movss %xmm0, (%r9,%rsi)
addq $0x4, %rsi
cmpl $0xc, %esi
jne 0x33a00
incq %rcx
addq %rsi, %r9
cmpq %r10, %rcx
jne 0x339f3
jmp 0x33a82
leaq 0x281e8(%rip), %rdi     # 0x5bc13
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2090
movl %r10d, %eax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, -0x70(%rbp)
movq %rdx, %rsp
movq %rsp, %r12
subq %rax, %r12
movq %r12, %rsp
jmp 0x33a69
movq -0x48(%rbp), %rcx
movq %rsp, %r13
leaq 0xf(,%rcx,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq -0x38(%rbp), %r14
leaq 0x275f1(%rip), %rsi     # 0x5b07a
leaq -0x120(%rbp), %rdi
movq %r14, %rdx
movq -0x68(%rbp), %r15
movq %r15, %rcx
xorl %eax, %eax
callq 0x2270
leaq 0x23758(%rip), %rsi     # 0x57200
leaq -0x120(%rbp), %rdi
callq 0x2220
movq %rax, -0x30(%rbp)
leaq 0x275d3(%rip), %rsi     # 0x5b092
leaq -0x1f0(%rbp), %rdi
movq %r14, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0x2270
leaq -0x1f0(%rbp), %rdi
movq -0x30(%rbp), %r14
leaq 0x2371b(%rip), %rsi     # 0x57200
callq 0x2220
movq %rax, %r15
leaq 0x275b6(%rip), %rdi     # 0x5b0aa
movl $0xd, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2290
cmpl $0x0, -0x40(%rbp)
je 0x33b3e
leaq 0x27449(%rip), %rdi     # 0x5af5c
movl $0x25, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2290
leaq 0x27456(%rip), %rdi     # 0x5af82
movl $0x2a, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2290
leaq 0x70(%rbp), %rax
cmpl $0x0, 0x35377(%rip)     # 0x68ec0
movq -0x60(%rbp), %r14
je 0x33b81
movl (%rax), %ecx
leaq 0x459b8(%rip), %rdi     # 0x79510
movl -0x7c(%rbp), %esi
movq -0x48(%rbp), %rdx
callq 0x10f90
movslq %r14d, %rdi
shlq $0x2, %rdi
callq 0x21e0
movq %rax, 0x459f1(%rip)     # 0x79568
movl $0x0, 0x3533f(%rip)     # 0x68ec0
movl $0x42c80000, 0x459c5(%rip) # imm = 0x42C80000
movl $0x0, 0x459db(%rip)     # 0x79570
testl %r14d, %r14d
movq -0x38(%rbp), %rdx
jle 0x33c3a
movq 0x459bf(%rip), %rax     # 0x79568
movl %r14d, %ecx
incl %r14d
andl $-0x2, %r14d
decq %rcx
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x1d458(%rip), %xmm1    # 0x51020
xorl %ecx, %ecx
movdqa 0x1d45e(%rip), %xmm2    # 0x51030
pxor %xmm2, %xmm0
pcmpeqd %xmm3, %xmm3
movdqa 0x1d45e(%rip), %xmm4    # 0x51040
movdqa %xmm1, %xmm5
pxor %xmm2, %xmm5
movdqa %xmm5, %xmm6
pcmpgtd %xmm0, %xmm6
pcmpeqd %xmm0, %xmm5
pshufd $0xf5, %xmm5, %xmm7     # xmm7 = xmm5[1,1,3,3]
pand %xmm6, %xmm7
pshufd $0xf5, %xmm6, %xmm5     # xmm5 = xmm6[1,1,3,3]
por %xmm7, %xmm5
movd %xmm5, %esi
notl %esi
testb $0x1, %sil
je 0x33c17
movl %ecx, (%rax,%rcx,4)
pxor %xmm3, %xmm5
pextrw $0x4, %xmm5, %esi
testb $0x1, %sil
je 0x33c2d
leal 0x1(%rcx), %esi
movl %esi, 0x4(%rax,%rcx,4)
addq $0x2, %rcx
paddq %xmm4, %xmm1
cmpq %rcx, %r14
jne 0x33be2
movq %r15, -0x40(%rbp)
leaq 0x27473(%rip), %rsi     # 0x5b0b8
leaq -0x380(%rbp), %rdi
xorl %eax, %eax
callq 0x2270
movq %rsp, %r15
movq -0x48(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %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, -0x38(%rbp)
testl %ecx, %ecx
movq -0x58(%rbp), %rax
jle 0x33cd9
xorl %r14d, %r14d
pxor %xmm2, %xmm2
movss (%rax,%r14,4), %xmm1
ucomiss %xmm2, %xmm1
xorps %xmm0, %xmm0
jbe 0x33cc6
jb 0x33cb7
xorps %xmm0, %xmm0
sqrtss %xmm1, %xmm0
jmp 0x33cc6
movaps %xmm1, %xmm0
callq 0x21d0
xorps %xmm2, %xmm2
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
movss %xmm0, (%rcx,%r14,4)
incq %r14
cmpq %r14, -0x38(%rbp)
jne 0x33c9e
xorps %xmm0, %xmm0
movq -0x30(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl %ebx, %edx
movq -0x70(%rbp), %rcx
movq -0x88(%rbp), %r8
xorl %r9d, %r9d
callq 0x3285f
leaq 0x273d0(%rip), %rsi     # 0x5b0d0
xorps %xmm0, %xmm0
movq -0x40(%rbp), %rdi
xorl %edx, %edx
movb $0x1, %al
callq 0x21a0
cmpl $0x0, -0x4c(%rbp)
jle 0x33e21
leaq 0x70(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x68(%rbp)
movl $0x1, %r14d
movq %rbx, -0xa8(%rbp)
movq %r15, %rdi
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rbx
movl %ebx, %r8d
movss -0x80(%rbp), %xmm0
leaq 0x457b3(%rip), %r9      # 0x79508
callq 0x32b27
testl %ebx, %ebx
jle 0x33d7b
xorl %eax, %eax
movss (%r13,%rax,4), %xmm0
addss (%r15,%rax,4), %xmm0
movss %xmm0, (%r12,%rax,4)
incq %rax
cmpq %rax, %rbx
jne 0x33d60
movq -0x70(%rbp), %rdi
movq %r12, %rsi
movq -0xb0(%rbp), %rdx
movq -0xa8(%rbp), %rbx
movl %ebx, %ecx
callq 0x4dfa0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x58(%rbp)
cvtss2sd %xmm0, %xmm0
movq -0x40(%rbp), %rdi
leaq 0x27321(%rip), %rsi     # 0x5b0d0
movl %r14d, %edx
movb $0x1, %al
callq 0x21a0
movq -0x30(%rbp), %rdi
movq -0x78(%rbp), %rsi
movl %ebx, %edx
movq %r12, %rcx
movq -0x88(%rbp), %r8
movl %r14d, %r9d
movss -0x58(%rbp), %xmm0
callq 0x3285f
testl %ebx, %ebx
jle 0x33e10
xorl %eax, %eax
movq %r12, %rcx
movq -0x90(%rbp), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtss2sd (%rcx,%rsi), %xmm0
movsd %xmm0, (%rdx,%rsi,2)
addq $0x4, %rsi
cmpq $0xc, %rsi
jne 0x33dec
addq $0x70, %rdx
incl %eax
addq %rsi, %rcx
cmpl %ebx, %eax
jne 0x33dea
leal 0x1(%r14), %eax
cmpl -0x4c(%rbp), %r14d
movl %eax, %r14d
jne 0x33d33
leaq 0x271ab(%rip), %rdi     # 0x5afd3
leaq -0x1f0(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq -0x40(%rbp), %rdi
callq 0x20c0
leaq 0x27293(%rip), %rdi     # 0x5b0d9
leaq -0x120(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq -0x30(%rbp), %rdi
callq 0x20c0
cmpl $0x0, 0x3505c(%rip)     # 0x68ec0
jne 0x33eb8
subq $0x40, %rsp
movups 0x456cf(%rip), %xmm0    # 0x79540
movups %xmm0, 0x30(%rsp)
movups 0x456b3(%rip), %xmm0    # 0x79530
movups %xmm0, 0x20(%rsp)
movups 0x45697(%rip), %xmm0    # 0x79520
movups %xmm0, 0x10(%rsp)
movups 0x4567b(%rip), %xmm0    # 0x79510
movups %xmm0, (%rsp)
callq 0x10fed
addq $0x40, %rsp
movq 0x456bf(%rip), %rdi     # 0x79568
callq 0x2050
movl $0x1, 0x35008(%rip)     # 0x68ec0
movq -0xa0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq