// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/simulation.c
// Function: Change_internal
int Change_internal(double *diff_phi, int naxe,
		    struct bond *bonds, struct bond *bonds2,
		    int natoms, int type, int same)
{
  // type=0: only dofs active in the TNM. Write differences without changing.
  // type=1: change torsion 2: also change angles 3: also change length
  // Pseudo-bonds coordinates (disordered loops) are always changed
  // Same: bonds2 corresponds to same structure, without gaps

  if(diff_phi){
    for(int i=0; i<naxe; i++)diff_phi[i]=0;
  }else if(type==0){
    printf("WARNING in Change_internal, vector for writing differences ");
    printf("has not been provided\n"); return(-1);
  }

  struct bond *bond1=bonds, *b1, *bond2=bonds2; int n=0, m=0;
  for(int i=0; i<natoms; i++){
    bond1=bonds+i;
    if((same==0) && bond1->atom->ali<0)continue;
    //if(bond1->atom->main==0)continue;
    b1=bond1->previous;
    if(same){bond2=bonds2+i;}
    else{bond2=bonds2+bond1->atom->ali;}
    if(b1 && (same || b1->atom->ali>=0)){
      m++;
      if((type >= 3)||(bond1->len >=0)){ // bond length
	if(type){
	  bond1->l=bond2->l; n++;
	}else if(bond1->len >=0){
	  diff_phi[bond1->len]=bond2->l-bond1->l; n++;
	}
      }
      b1=b1->previous;
      if(b1 && (same || b1->atom->ali>=0)){
	m++;
	if((type >= 2)||(bond1->angle >=0)){   // bond angle
	  if(type){
	    bond1->r_cos_theta=bond2->r_cos_theta*(bond1->l/bond2->l);
	    n++;
	  }else if(bond1->angle >=0){
	    diff_phi[bond1->angle]=
	      acos(bond2->r_cos_theta/bond2->l)-
	      acos(bond1->r_cos_theta/bond1->l);
	    n++;
	  }
	}
	b1=b1->previous;
	if(b1 && (same || b1->atom->ali>=0)){
	  m++;
	  if((type)||(bond1->torsion >=0)){  // torsion angle
	    if(type){
	      bond1->phi=bond2->phi; n++;
	    }else if(bond1->torsion >=0){
	      diff_phi[bond1->torsion]=bond2->phi-bond1->phi; n++;
	    }
	  }
	}
      }
    }
  }
  printf("%d out of %d internal coordinates of type<=%d and pseudobonds changed\n",
	 n, m, type);
  return(0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r12
testq %rdi, %rdi
je 0x35444
xorl %eax, %eax
testl %esi, %esi
cmovlel %eax, %esi
cmpq %rax, %rsi
je 0x3544d
andq $0x0, (%rdi,%rax,8)
incq %rax
jmp 0x35435
testl %r9d, %r9d
je 0x35649
xorl %r13d, %r13d
testl %r8d, %r8d
cmovlel %r13d, %r8d
movl 0x60(%rsp), %r10d
addq $0x68, %r12
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq %r13, %r8
je 0x3561e
testl %r10d, %r10d
je 0x3553e
movq -0x40(%r12), %rax
movq %r13, %rsi
testq %rax, %rax
je 0x355a1
imulq $0xc0, %rsi, %rbx
addq %rcx, %rbx
cmpl $0x2, %r9d
jg 0x354ab
movslq -0x34(%r12), %rdx
testq %rdx, %rdx
js 0x354b9
testl %r9d, %r9d
je 0x3556f
movsd 0x58(%rbx), %xmm0
movsd %xmm0, -0x10(%r12)
incl %ebp
leal 0x1(%r15), %edx
movq 0x28(%rax), %r14
testq %r14, %r14
je 0x35565
testl %r10d, %r10d
jne 0x354dc
movq (%r14), %rax
cmpl $0x0, 0x54(%rax)
js 0x35565
cmpl $0x1, %r9d
jg 0x354f3
cmpl $0x0, -0x30(%r12)
js 0x3550d
testl %r9d, %r9d
je 0x355b0
movsd -0x10(%r12), %xmm0
divsd 0x58(%rbx), %xmm0
mulsd 0x60(%rbx), %xmm0
movsd %xmm0, -0x8(%r12)
incl %ebp
leal 0x2(%r15), %eax
movq 0x28(%r14), %rdx
testq %rdx, %rdx
je 0x3556a
testl %r10d, %r10d
jne 0x35528
movq (%rdx), %rdx
cmpl $0x0, 0x54(%rdx)
js 0x3556a
addl $0x3, %r15d
testl %r9d, %r9d
je 0x35585
movsd 0x68(%rbx), %xmm0
movsd %xmm0, (%r12)
jmp 0x3559f
movq -0x68(%r12), %rax
movslq 0x54(%rax), %rsi
testq %rsi, %rsi
js 0x355a1
movq -0x40(%r12), %rax
testq %rax, %rax
je 0x355a1
movq (%rax), %rdx
cmpl $0x0, 0x54(%rdx)
jns 0x35488
jmp 0x355a1
movl %edx, %r15d
jmp 0x355a1
movl %eax, %r15d
jmp 0x355a1
movsd 0x58(%rbx), %xmm0
subsd -0x10(%r12), %xmm0
movsd %xmm0, (%rdi,%rdx,8)
jmp 0x354b7
movslq -0x2c(%r12), %rax
testq %rax, %rax
js 0x355a1
movsd 0x68(%rbx), %xmm0
subsd (%r12), %xmm0
movsd %xmm0, (%rdi,%rax,8)
incl %ebp
incq %r13
addq $0xc0, %r12
jmp 0x35465
movsd 0x60(%rbx), %xmm0
divsd 0x58(%rbx), %xmm0
movl %r9d, 0x4(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
callq 0x2150
movsd %xmm0, 0x8(%rsp)
movsd -0x8(%r12), %xmm0
divsd -0x10(%r12), %xmm0
callq 0x2150
movl 0x60(%rsp), %r10d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %r8
movl 0x4(%rsp), %r9d
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movslq -0x30(%r12), %rax
movsd %xmm1, (%rdi,%rax,8)
jmp 0x3550b
leaq 0x201dd(%rip), %rdi     # 0x55802
xorl %r14d, %r14d
movl %ebp, %esi
movl %r15d, %edx
movl %r9d, %ecx
xorl %eax, %eax
callq 0x20f0
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20176(%rip), %rdi     # 0x557c6
xorl %eax, %eax
callq 0x20f0
leaq 0x206a2(%rip), %rdi     # 0x55d00
callq 0x2090
pushq $-0x1
popq %r14
jmp 0x35637