// File: /workspace/github_c_makefile_star2_O3/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 0x3a1d0
testl %esi, %esi
jle 0x3a1d9
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 0x3a1d9
testl %r9d, %r9d
je 0x3a3de
movl $0x0, %r13d
movl $0x0, %ebp
testl %r12d, %r12d
jle 0x3a3b7
movl 0x60(%rsp), %r8d
movl %r12d, %r10d
addq $0x68, %r15
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %r13d, %r13d
testl %r8d, %r8d
je 0x3a2d2
movq -0x40(%r15), %rax
movq %r14, %rsi
testq %rax, %rax
je 0x3a333
leaq (%rsi,%rsi,2), %r12
shlq $0x6, %r12
addq %rcx, %r12
cmpl $0x2, %r9d
jg 0x3a23d
movslq -0x34(%r15), %rdx
testq %rdx, %rdx
js 0x3a24d
testl %r9d, %r9d
je 0x3a2ff
movsd 0x58(%r12), %xmm0
movsd %xmm0, -0x10(%r15)
incl %r13d
leal 0x1(%rbp), %edx
movq 0x28(%rax), %rbx
testq %rbx, %rbx
je 0x3a2f7
testl %r8d, %r8d
jne 0x3a26f
movq (%rbx), %rax
cmpl $0x0, 0x54(%rax)
js 0x3a2f7
cmpl $0x1, %r9d
jg 0x3a285
cmpl $0x0, -0x30(%r15)
js 0x3a2a2
testl %r9d, %r9d
je 0x3a348
movsd -0x10(%r15), %xmm0
divsd 0x58(%r12), %xmm0
mulsd 0x60(%r12), %xmm0
movsd %xmm0, -0x8(%r15)
incl %r13d
leal 0x2(%rbp), %eax
movq 0x28(%rbx), %rdx
testq %rdx, %rdx
je 0x3a2fb
testl %r8d, %r8d
jne 0x3a2bc
movq (%rdx), %rdx
cmpl $0x0, 0x54(%rdx)
js 0x3a2fb
addl $0x3, %ebp
testl %r9d, %r9d
je 0x3a316
movsd 0x68(%r12), %xmm0
movsd %xmm0, (%r15)
jmp 0x3a330
movq -0x68(%r15), %rax
movslq 0x54(%rax), %rsi
testq %rsi, %rsi
js 0x3a333
movq -0x40(%r15), %rax
testq %rax, %rax
je 0x3a333
movq (%rax), %rdx
cmpl $0x0, 0x54(%rdx)
jns 0x3a21a
jmp 0x3a333
movl %edx, %ebp
jmp 0x3a333
movl %eax, %ebp
jmp 0x3a333
movsd 0x58(%r12), %xmm0
subsd -0x10(%r15), %xmm0
movsd %xmm0, (%rdi,%rdx,8)
jmp 0x3a24a
movslq -0x2c(%r15), %rax
testq %rax, %rax
js 0x3a333
movsd 0x68(%r12), %xmm0
subsd (%r15), %xmm0
movsd %xmm0, (%rdi,%rax,8)
incl %r13d
incq %r14
addq $0xc0, %r15
cmpq %r14, %r10
jne 0x3a201
jmp 0x3a3b7
movsd 0x60(%r12), %xmm0
divsd 0x58(%r12), %xmm0
movl %r9d, 0x4(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdi, 0x18(%rsp)
movq %r10, 0x10(%rsp)
callq 0x2150
movsd %xmm0, 0x8(%rsp)
movsd -0x8(%r15), %xmm0
divsd -0x10(%r15), %xmm0
callq 0x2150
movq 0x10(%rsp), %r10
movl 0x60(%rsp), %r8d
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rcx
movl 0x4(%rsp), %r9d
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
movslq -0x30(%r15), %rax
movsd %xmm1, (%rdi,%rax,8)
jmp 0x3a29f
leaq 0x214e4(%rip), %rdi     # 0x5b8a2
movl %r13d, %esi
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 0x21481(%rip), %rdi     # 0x5b866
xorl %eax, %eax
callq 0x20f0
leaq 0x219ad(%rip), %rdi     # 0x5bda0
callq 0x2090
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x3a3cf