// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/mutation.c
// Function: Mutation_force
int Mutation_force(float *Force, int N_Cart,
		   float C_size, float C_stab, float C_dist, 
		   char *AAwt, int *Posmut, char *AAmut, int Nmut,
		   int **clist, int **cnum, struct interaction *Int_list,
		   struct residue *seq, int nres, atom *atoms,
		   struct Reference Ref_kin, int *kin_atom)
{

  int am=-1, ai, j, nc=0, iter;
  for(j=0; j<N_Cart; j++)Force[j]=0;
  for(int kmut=0; kmut<Nmut; kmut++){
    int pmut=Posmut[kmut];
    int aaw=Code_AA(AAwt[kmut]);   // wild-type aa
    int aam=Code_AA(AAmut[kmut]);  // mutated aa
    double dsize=(natm_norm[aam]-natm_norm[aaw]);
    if((aaw==20)||(aam==20)){iter=2;} // gap!
    else{iter=1;}
    /* Since atoms in the gap are removed, we have to analyze
       the flanking residues */
    int p1=-1;
    for(int it=0; it<iter; it++){
      if(iter==2){
	if(it==0){
	  while((pmut>=0)&&(clist[pmut][0]<=0))pmut--;
	  if(pmut<0)continue;
	  p1=pmut;
	}else if(it==1){
	  pmut=Posmut[kmut]+1; while((pmut<nres)&&(clist[pmut][0]<=0))pmut++;
	  if(pmut>=nres)continue;
	}
      }
      int *ic=clist[pmut]; // residue in contact with pmut
      int *ii=cnum[pmut];  // interaction label of the contact
      while(*ic >= 0){
	//if(abs(pmut-(*ic))<3)goto next; // short range contact discarded
	int i1=Int_list[*ii].i1, i2=Int_list[*ii].i2;
	if(atoms[i1].res==pmut){
	  am=i1; ai=i2;
	}else if(atoms[i2].res==pmut){
	  am=i2; ai=i1;
	}else{
	  printf("ERROR in mutation, interaction %d res %d - %d pmut= %d\n",
		 *ii, atoms[i1].res, atoms[i2].res, pmut); exit(8);
	}
	int aai=Code_AA(seq[*ic].amm);
	ic++; ii++;
	
	int res_i=atoms[ai].res;
	//if((abs(res_i-pmut)<3))goto next; //&&(FORCE_TYPE==1)

	// contacts of main chain atoms of mutant residue are discarded (?)
	if(PRINT_FLANK &&(res_i==p1)){
	  printf("Gap flanking residues %d and %d\n", p1, pmut);
	  PRINT_FLANK=0;
	  //}else if(Mainchain(atoms[am].name)){
	  //continue; //&& FORCE_TYPE!=3
	}

	float f =C_size*dsize;
	f+=C_dist*(D_Res[aam][aai]-D_Res[aaw][aai]);
	float dE=(Econt[aam][aai]-Econt[aaw][aai]);
	if(((aaw==20)&&(dE<0))|| // Insertion, attracting
	   ((aam==20)&&(dE>0)))continue; // Deletion, repelling
	f+=C_stab*dE;

	// Record contacts considered for force computation
	if(nc<ncmax){
	  mut_cont[nc]=aai; mut_res[nc]=res_i;
	  wt_cont[nc]=aaw; wt_res[nc]=pmut;
	}
	nc++;


	double r[3], r2=0, *rm=atoms[am].r, *ri=atoms[ai].r;
	for(j=0; j<3; j++){
	  r[j]=rm[j]-ri[j]; r2+=r[j]*r[j];
	}
	f/=pow(r2, Exp);

	int km=3*kin_atom[am], ki=3*kin_atom[ai]; 
	for(j=0; j<3; j++){
	  float fj=f*r[j];
	  Force[km+j]+=fj; //?
	  Force[ki+j]-=fj;
	}
      } // end contact
    } // end iter (in case of gap)
  }
  //printf("Force computation based on %d contacts\n", nc);
  return(nc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, 0x78(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, 0x70(%rsp)
movss %xmm2, 0x30(%rsp)
movss %xmm1, 0x2c(%rsp)
movss %xmm0, 0x24(%rsp)
movq %rdi, 0x58(%rsp)
movl 0x100(%rsp), %eax
movq 0xe0(%rsp), %rbx
xorl %ecx, %ecx
testl %esi, %esi
cmovlel %ecx, %esi
movq 0x58(%rsp), %rdx
cmpq %rcx, %rsi
je 0x39d00
andl $0x0, (%rdx,%rcx,4)
incq %rcx
jmp 0x39cf2
movslq %eax, %r14
xorl %eax, %eax
testl %r9d, %r9d
cmovlel %eax, %r9d
movq %r9, 0x80(%rsp)
xorl %eax, %eax
movl $0x0, 0x10(%rsp)
movq %r14, 0x88(%rsp)
cmpq 0x80(%rsp), %rax
je 0x3a0f0
movq %rax, %r15
movq 0x48(%rsp), %rax
movl (%rax,%r15,4), %r12d
movq 0x70(%rsp), %rax
movsbl (%rax,%r15), %edi
callq 0x1d91c
movl %eax, %ebp
movq 0x78(%rsp), %rax
movq %r15, 0x50(%rsp)
movsbl (%rax,%r15), %edi
callq 0x1d91c
movq %r12, %r8
movslq %eax, %rcx
leaq 0x3ae7e(%rip), %rax     # 0x74bf0
movss (%rax,%rcx,4), %xmm0
movl %ebp, 0x28(%rsp)
movslq %ebp, %rdx
subss (%rax,%rdx,4), %xmm0
movq %rdx, 0x60(%rsp)
cmpl $0x14, %edx
sete %al
movq %rcx, 0x68(%rsp)
cmpl $0x14, %ecx
sete %cl
orb %al, %cl
movb %cl, 0xf(%rsp)
movzbl %cl, %eax
incl %eax
movl %eax, 0x38(%rsp)
mulss 0x24(%rsp), %xmm0
movss %xmm0, 0x34(%rsp)
xorl %edx, %edx
pushq $-0x1
popq %rax
movl %eax, 0x14(%rsp)
cmpl 0x38(%rsp), %edx
je 0x3a0e3
cmpb $0x0, 0xf(%rsp)
je 0x39e31
testl %edx, %edx
je 0x39dfc
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
movslq (%rax,%rcx,4), %r8
movq %r8, %rax
incq %rax
cmpq %r14, %rax
jge 0x39e21
movq (%rbx,%rax,8), %rcx
incl %r8d
incq %rax
cmpl $0x0, (%rcx)
jle 0x39de6
jmp 0x39e31
cmpl $-0x1, %r8d
pushq $-0x1
popq %rax
cmovll %r8d, %eax
testl %r8d, %r8d
js 0x3a0d9
movl %r8d, %ecx
movq (%rbx,%rcx,8), %rcx
cmpl $0x0, (%rcx)
jg 0x39e2c
decl %r8d
jmp 0x39e07
incl %r8d
movl %r8d, %eax
jmp 0x3a0d9
movl %r8d, 0x14(%rsp)
movl %edx, 0x40(%rsp)
movslq %r8d, %rax
movq (%rbx,%rax,8), %r9
movq 0xe8(%rsp), %rcx
movq (%rcx,%rax,8), %r13
movq %r8, 0x18(%rsp)
movslq (%r9), %rax
testq %rax, %rax
js 0x3a0c2
movslq (%r13), %rsi
imulq $0xc8, %rsi, %rdx
movq 0xf0(%rsp), %rdi
movl (%rdi,%rdx), %ecx
movl 0x4(%rdi,%rdx), %r10d
movslq %ecx, %r14
imulq $0x70, %r14, %rdx
movq 0x108(%rsp), %r15
movl 0x64(%r15,%rdx), %edx
movslq %r10d, %rdi
cmpl %r8d, %edx
jne 0x39e98
movq %rdi, %r14
movl %ecx, %r10d
jmp 0x39eaa
imulq $0x70, %rdi, %rcx
movl 0x64(%r15,%rcx), %ecx
cmpl %r8d, %ecx
jne 0x3a106
movl %r10d, 0x44(%rsp)
movq %r13, %rbx
movq %r9, %r13
shlq $0x5, %rax
movq 0xf8(%rsp), %rcx
movsbl 0x13(%rcx,%rax), %edi
callq 0x1d91c
movl %eax, %r12d
imulq $0x70, %r14, %rbp
movl 0x64(%r15,%rbp), %r15d
cmpl $0x0, 0x2a07e(%rip)     # 0x63f5c
je 0x39f05
cmpl 0x14(%rsp), %r15d
jne 0x39f05
leaq 0x1c750(%rip), %rdi     # 0x5663e
movl 0x14(%rsp), %esi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x20f0
andl $0x0, 0x2a057(%rip)     # 0x63f5c
movq %r13, %r9
addq $0x4, %r9
movq %rbx, %r13
addq $0x4, %r13
movq 0x68(%rsp), %rdi
cmpl $0x14, %edi
sete %cl
movq 0x60(%rsp), %r8
cmpl $0x14, %r8d
sete %sil
movslq %r12d, %rax
leaq 0x2a24b(%rip), %rdx     # 0x64180
movq (%rdx), %rdx
movq (%rdx,%rdi,8), %rdi
movss (%rdi,%rax,4), %xmm1
movq (%rdx,%r8,8), %rdx
subss (%rdx,%rax,4), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
seta %dil
ucomiss %xmm0, %xmm1
seta %dl
testb %dil, %sil
movq 0x18(%rsp), %r8
jne 0x39e4d
andb %dl, %cl
jne 0x39e4d
movl 0x10(%rsp), %ecx
cmpl 0x29fe6(%rip), %ecx     # 0x63f60
jge 0x39fb0
movslq 0x10(%rsp), %rcx
leaq 0x3a948(%rip), %rdx     # 0x748d0
movl %r12d, (%rdx,%rcx,4)
leaq 0x3aacd(%rip), %rdx     # 0x74a60
movl %r15d, (%rdx,%rcx,4)
leaq 0x3a612(%rip), %rdx     # 0x745b0
movl 0x28(%rsp), %esi
movl %esi, (%rdx,%rcx,4)
leaq 0x3a794(%rip), %rdx     # 0x74740
movl %r8d, (%rdx,%rcx,4)
imulq $0x50, 0x68(%rsp), %rcx
leaq 0x2a813(%rip), %rdx     # 0x647d0
addq %rdx, %rcx
movss (%rcx,%rax,4), %xmm0
imulq $0x50, 0x60(%rsp), %rcx
addq %rdx, %rcx
subss (%rcx,%rax,4), %xmm0
mulss 0x30(%rsp), %xmm0
addss 0x34(%rsp), %xmm0
mulss 0x2c(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x3c(%rsp)
movslq 0x44(%rsp), %rbx
imulq $0x70, %rbx, %rax
movq 0x108(%rsp), %rcx
addq %rcx, %rax
addq %rcx, %rbp
xorps %xmm0, %xmm0
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x3a032
movsd (%rax,%rcx,8), %xmm1
subsd (%rbp,%rcx,8), %xmm1
movsd %xmm1, 0x90(%rsp,%rcx,8)
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rcx
jmp 0x3a00b
cvtss2sd 0x3a55e(%rip), %xmm1    # 0x74598
movq %r9, %r15
callq 0x20a0
movq %r15, %r9
movq 0x18(%rsp), %r8
cvtss2sd 0x3c(%rsp), %xmm1
divsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
movq 0x140(%rsp), %rcx
imull $0x3, (%rcx,%rbx,4), %eax
imull $0x3, (%rcx,%r14,4), %ecx
cvtss2sd %xmm0, %xmm0
movslq %ecx, %rcx
cltq
movq 0x58(%rsp), %rdx
leaq (%rdx,%rax,4), %rax
leaq (%rdx,%rcx,4), %rcx
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x3a0b9
movsd 0x90(%rsp,%rdx,8), %xmm1
mulsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss (%rax,%rdx,4), %xmm2
addss %xmm1, %xmm2
movss %xmm2, (%rax,%rdx,4)
movss (%rcx,%rdx,4), %xmm2
subss %xmm1, %xmm2
movss %xmm2, (%rcx,%rdx,4)
incq %rdx
jmp 0x3a081
incl 0x10(%rsp)
jmp 0x39e4d
movl %r8d, %eax
movq 0xe0(%rsp), %rbx
movq 0x88(%rsp), %r14
movl 0x40(%rsp), %edx
incl %edx
movl %eax, %r8d
jmp 0x39dbd
movq 0x50(%rsp), %rax
incq %rax
jmp 0x39d26
movl 0x10(%rsp), %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1c4f9(%rip), %rdi     # 0x56606
xorl %eax, %eax
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270