// File: /workspace/github_c_makefile_star2_O1/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
movl %r8d, %r12d
movq %rdx, %r15
testq %rdi, %rdi
je 0x3a211
testl %esi, %esi
jle 0x3a21a
movl %esi, %edx
shlq $0x3, %rdx
movq %rdi, %r13
xorl %esi, %esi
movl %r9d, %ebx
movq %rcx, %r14
callq 0x2110
movq %r13, %rdi
movq %r14, %rcx
movl %ebx, %r9d
jmp 0x3a21a
testl %r9d, %r9d
je 0x3a40a
movl $0x0, %esi
movl $0x0, %ebp
testl %r12d, %r12d
jle 0x3a3e6
movl 0x60(%rsp), %r14d
movl %r12d, %r10d
addq $0x68, %r15
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %esi, %esi
movl %r9d, (%rsp)
movq %rcx, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
movq %r10, 0x8(%rsp)
testl %r14d, %r14d
jne 0x3a266
movq -0x68(%r15), %rax
cmpl $0x0, 0x54(%rax)
js 0x3a3d3
movq %r12, %r8
testl %r14d, %r14d
jne 0x3a276
movq -0x68(%r15), %rax
movslq 0x54(%rax), %r8
movq -0x40(%r15), %rax
testq %rax, %rax
je 0x3a3d3
testl %r14d, %r14d
jne 0x3a295
movq (%rax), %rdx
cmpl $0x0, 0x54(%rdx)
js 0x3a3d3
leaq (%r8,%r8,2), %rbx
shlq $0x6, %rbx
addq %rcx, %rbx
cmpl $0x2, %r9d
jg 0x3a2ad
cmpl $0x0, -0x34(%r15)
js 0x3a2da
testl %r9d, %r9d
je 0x3a2bf
movsd 0x58(%rbx), %xmm0
movsd %xmm0, -0x10(%r15)
jmp 0x3a2d8
movslq -0x34(%r15), %rdx
testq %rdx, %rdx
js 0x3a2da
movsd 0x58(%rbx), %xmm0
subsd -0x10(%r15), %xmm0
movsd %xmm0, (%rdi,%rdx,8)
incl %esi
leal 0x1(%rbp), %edx
movq 0x28(%rax), %r13
testq %r13, %r13
je 0x3a323
testl %r14d, %r14d
jne 0x3a2f5
movq (%r13), %rax
cmpl $0x0, 0x54(%rax)
js 0x3a323
cmpl $0x1, %r9d
jg 0x3a306
cmpl $0x0, -0x30(%r15)
js 0x3a387
testl %r9d, %r9d
je 0x3a32a
movsd -0x10(%r15), %xmm0
divsd 0x58(%rbx), %xmm0
mulsd 0x60(%rbx), %xmm0
movsd %xmm0, -0x8(%r15)
jmp 0x3a385
movl %edx, %ebp
jmp 0x3a3d3
cmpl $0x0, -0x30(%r15)
js 0x3a387
movsd 0x60(%rbx), %xmm0
divsd 0x58(%rbx), %xmm0
movl %esi, 0x4(%rsp)
callq 0x2150
movsd %xmm0, 0x20(%rsp)
movsd -0x8(%r15), %xmm0
divsd -0x10(%r15), %xmm0
callq 0x2150
movq 0x8(%rsp), %r10
movl 0x4(%rsp), %esi
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rcx
movl (%rsp), %r9d
movsd 0x20(%rsp), %xmm1
subsd %xmm0, %xmm1
movslq -0x30(%r15), %rax
movsd %xmm1, (%rdi,%rax,8)
incl %esi
leal 0x2(%rbp), %eax
movq 0x28(%r13), %rdx
testq %rdx, %rdx
je 0x3a3b5
testl %r14d, %r14d
jne 0x3a3a1
movq (%rdx), %rdx
cmpl $0x0, 0x54(%rdx)
js 0x3a3b5
addl $0x3, %ebp
testl %r9d, %r9d
je 0x3a3b9
movsd 0x68(%rbx), %xmm0
movsd %xmm0, (%r15)
jmp 0x3a3d1
movl %eax, %ebp
jmp 0x3a3d3
movslq -0x2c(%r15), %rax
testq %rax, %rax
js 0x3a3d3
movsd 0x68(%rbx), %xmm0
subsd (%r15), %xmm0
movsd %xmm0, (%rdi,%rax,8)
incl %esi
incq %r12
addq $0xc0, %r15
cmpq %r12, %r10
jne 0x3a253
leaq 0x214fd(%rip), %rdi     # 0x5b8ea
movl %ebp, %edx
movl %r9d, %ecx
xorl %eax, %eax
callq 0x20f0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2149d(%rip), %rdi     # 0x5b8ae
xorl %eax, %eax
callq 0x20f0
leaq 0x219c9(%rip), %rdi     # 0x5bde8
callq 0x2090
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3a3fb