// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/dof_tnm.c
// Function: Set_side_chains
int Set_side_chains(struct axe *axes,
		    struct bond *bonds,
		    atom *atoms,
		    int natoms,
		    int *refatom,
		    int *atom_ref,
		    int Nmain,
		    int MIN_INT)
{
  int N_axes=0, ndof=0, numdof=0, sidedof=Nmain;
  int first_atom=-1, last_atom=0, last_ref=0; //first_ref=0
  int ibond, ires=-1, i;
  char type[20], aa=' ';
  struct axe *axe=axes;
  struct bond *bond;
  int iref, oldref=1;
  int last_main=0, first_side=0;
  int move_atom; // last_move_atom=0;

  for(ibond=1; ibond<(natoms-1); ibond++){
    bond=bonds+ibond;
    if(bond->atom==NULL)break;
    if(bond->previous==NULL)continue;
    if(bond->atom->res!=ires){
      // Residue start
      ires=bond->atom->res;
      aa=bond->atom->aa;
      if((aa=='G')||(aa=='A')||(aa=='P'))continue;
      // degrees of freedom
      first_side=sidedof;
      ndof=0; numdof=0;
      // Find first and last atom in side chain
      first_atom=-1;
      for(i=bond->i_atom; i<natoms; i++){
	atom *atom_i=atoms+i;
	if((atom_i->res>ires)||
	   //(strncmp(atom_i->name,"C ", 2)==0)||
	   //(strncmp(atom_i->name,"O ", 2)==0)||
	   (strncmp(atom_i->name,"OT", 2)==0)||
	   (strncmp(atom_i->name,"OX", 2)==0))
	  break;
	if((strncmp(atom_i->name,"N ", 2)==0)||
	   (strncmp(atom_i->name,"CA", 2)==0)||
	   (strncmp(atom_i->name,"CB", 2)==0))continue;
	if(first_atom<0)first_atom=i;
      }

      if(first_atom<0)continue;
      //last_move_atom=first_atom;

      last_atom=i-1;
      /*printf(" Res %c first_atom= %s %d last_atom= %s %d\n",
	     aa, atoms[first_atom].name, first_atom,
	     atoms[last_atom].name, last_atom);*/
      last_main=atoms[first_atom].last_dof_main;
      if(last_main<0)continue;
      //first_ref=refatom[first_atom];
      last_ref=refatom[last_atom];
      while(atoms[atom_ref[last_ref]].res>ires)last_ref--;
    }

    if((first_atom<0)||
       (last_main<0)||
       (bond->i_atom < first_atom)||
       (bond->i_atom > last_atom)){
      continue;
    }else if((bond->terminal)||
	     (strncmp(bond->atom->name,"N ", 2)==0)||
	     (strncmp(bond->atom->name,"CA", 2)==0)||
	     (strncmp(bond->atom->name,"C ", 2)==0)||
	     (strncmp(bond->atom->name,"O ", 2)==0)){
      continue;
    }else if((aa=='G')||(aa=='A')||(aa=='P')){
      continue;     // exit if gly, ala or pro
    }else if((strncmp(bond->atom->name,"CB", 2)==0)||
	     (strncmp(bond->atom->name,"CG", 2)==0)){
      goto test_ref;
    }else if((aa=='F')||(aa=='W')||
	     (aa=='Y')||(aa=='H')){
      continue;  // if aromatic ring, exit
    }

  test_ref:
    move_atom=(Next_bond(bond, ibond, natoms))->i_atom;
    iref=refatom[move_atom];
    if((iref<0)||(iref>last_ref)||
       (atoms[atom_ref[iref]].res!=ires))
      continue;

    sprintf(type, "%1d", numdof);
    if(Set_dof(&axe, &N_axes, type[0], bond, move_atom, &ndof,
	       &oldref, refatom, natoms, last_atom, last_ref)){
      struct axe *axe1=axe-1;
      (axe1)->type='t';
      (axe1)->last_atom=last_atom;
      (axe1)->last_kin=last_ref;
      (axe1)->last_main=last_main;
      (axe1)->first_side=first_side;
      sidedof++;
      numdof++;
    }
  }

  if(MIN_INT>0){
    // Test number of interactions
    int n=0, a;
    for(a=0; a<N_axes; a++){
      axe=axes+a;
      int num_int=0;
      for(i=axe->first_atom; i< natoms; i++){
	if(atoms[i].res!=axe->bond->atom->res)break;
	num_int+=atoms[i].N_int;
      }
      if(num_int >= MIN_INT){
	if(a!=n){axes[n]=*axe;} n++;
      }
    }
    N_axes=n;
  }

  // First and last degree of freedom for each atom
  int iatom=axes[0].first_atom, nmax=N_axes-1; ndof=0;
  for(i=0; i<natoms; i++){
    if((i >= iatom)&&(ndof < nmax)){
      ndof++; if(ndof<nmax)iatom=axes[ndof+1].first_atom;
    }
    if(axes[ndof].bond->atom->res==atoms[i].res)atoms[i].last_dof_side=ndof;
  }

  // Association between main chain degrees of freedom and bonds
  for(i=0; i<natoms; i++){
    bond=bonds+i;
    if(bond->atom==NULL)break;
    atom *atom=bond->atom;
    int n=atom->last_dof_side; if(n<0)continue;
    if((bond->previous!=NULL)&&(axes[n].bond==bond->previous)){
      if(axes[n].type=='t')bond->torsion=n;
    }
  }

  // Print degrees of freedom of side chains
  ires=-1;
  for(i=0; i<N_axes; i++){
    axe=axes+i;
    if(axe->bond->atom->res!=ires){
      ires=axe->bond->atom->res; numdof=0;
      printf("-%c%d:", axe->bond->atom->aa, ires+1);
    }
    numdof++;
    printf("%1d", numdof);
  }
  printf("\n");

  return(N_axes);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x80(%rsp)
movq %r8, 0x68(%rsp)
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %rbx
movl 0xe8(%rsp), %r9d
xorl %ebp, %ebp
movl %ebp, 0x48(%rsp)
movl %ebp, 0x44(%rsp)
movq %rdi, 0x50(%rsp)
movl $0x1, 0x7c(%rsp)
movl %ecx, %r11d
cmpl $0x3, %ecx
movq %rcx, 0x8(%rsp)
movq %r11, 0x20(%rsp)
movq %rsi, 0x38(%rsp)
jl 0x14d82
movl 0xe0(%rsp), %eax
movl %eax, 0x40(%rsp)
leal -0x1(%rcx), %esi
leaq 0x64(%r12), %rax
movq %rax, 0x88(%rsp)
movb $0x20, %al
movl %eax, 0x5c(%rsp)
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl $0x1, %edi
xorl %ebp, %ebp
xorl %r13d, %r13d
movq $0x0, 0x60(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x4c(%rsp)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq %rsi, 0x28(%rsp)
leaq (%rdi,%rdi,2), %r15
shlq $0x6, %r15
movq (%r14,%r15), %rdx
testq %rdx, %rdx
je 0x14d82
addq %r14, %r15
cmpq $0x0, 0x28(%r15)
je 0x14997
movl 0x64(%rdx), %r10d
cmpl %eax, %r10d
movq %rdx, 0x70(%rsp)
jne 0x1499c
movl %eax, %r10d
movl %r13d, %edx
testl %r8d, %r8d
js 0x149b9
cmpl $0x0, 0x4c(%rsp)
js 0x149b9
movl 0x20(%r15), %eax
cmpl %r8d, %eax
jl 0x149b9
cmpl %edx, %eax
jg 0x149b9
cmpl $0x0, 0x30(%r15)
jne 0x149b9
movq %rdx, 0x10(%rsp)
movl %r10d, (%rsp)
movl %r8d, 0x4(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x70(%rsp), %r13
addq $0x18, %r13
movl $0x2, %edx
movq %r13, %rdi
leaq 0x41642(%rip), %rsi     # 0x55f23
callq 0x2070
testl %eax, %eax
je 0x14d44
movl $0x2, %edx
movq %r13, %rdi
leaq 0x4086d(%rip), %rsi     # 0x5516a
callq 0x2070
testl %eax, %eax
je 0x14d44
movl $0x2, %edx
movq %r13, %rdi
leaq 0x4160d(%rip), %rsi     # 0x55f26
callq 0x2070
testl %eax, %eax
je 0x14d44
movl $0x2, %edx
movq %r13, 0x70(%rsp)
movq %r13, %rdi
leaq 0x415ef(%rip), %rsi     # 0x55f29
callq 0x2070
testl %eax, %eax
je 0x14d44
movzbl 0x5c(%rsp), %r14d
leal -0x41(%r14), %eax
cmpl $0xf, %eax
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r11
movl 0xe8(%rsp), %r9d
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %r8d
movl (%rsp), %r10d
ja 0x14bb3
movl $0x8041, %edx           # imm = 0x8041
btl %eax, %edx
jae 0x14bb3
movq 0x38(%rsp), %r14
jmp 0x14d69
movl %eax, %r10d
jmp 0x149b6
movzbl 0x68(%rdx), %eax
movl %eax, 0x5c(%rsp)
addl $-0x41, %eax
cmpl $0xf, %eax
ja 0x149d0
movl $0x8041, %edx           # imm = 0x8041
btl %eax, %edx
jae 0x149d0
movl %r13d, %edx
incq %rdi
movl %edx, %r13d
movl %r10d, %eax
cmpq %rsi, %rdi
jne 0x14847
jmp 0x14d82
movl $0x0, 0x44(%rsp)
movl 0x20(%r15), %edx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
cmpl %ecx, %edx
jge 0x14b27
movslq %edx, %rdx
imulq $0x70, %rdx, %rbp
addq 0x88(%rsp), %rbp
movq %rdi, 0x18(%rsp)
movl %r10d, (%rsp)
movb $0x1, %r14b
cmpl %r10d, (%rbp)
jg 0x14b0a
movq %rdx, 0x10(%rsp)
movl %r8d, 0x4(%rsp)
leaq -0x4c(%rbp), %rdi
movl $0x2, %edx
movq %rdi, 0x30(%rsp)
leaq 0x414fe(%rip), %rsi     # 0x55f2c
callq 0x2070
testl %eax, %eax
je 0x14ae0
movl $0x2, %edx
movq 0x30(%rsp), %rdi
leaq 0x414e3(%rip), %rsi     # 0x55f2f
callq 0x2070
testl %eax, %eax
je 0x14ae0
movl $0x2, %edx
movq 0x30(%rsp), %rdi
leaq 0x414b9(%rip), %rsi     # 0x55f23
callq 0x2070
testl %eax, %eax
je 0x14add
movl $0x2, %edx
movq 0x30(%rsp), %rdi
leaq 0x406e6(%rip), %rsi     # 0x5516a
callq 0x2070
testl %eax, %eax
je 0x14add
movl $0x2, %edx
movq 0x30(%rsp), %rdi
leaq 0x3c726(%rip), %rsi     # 0x511c4
callq 0x2070
testl %eax, %eax
je 0x14add
movl 0x4(%rsp), %r8d
testl %r8d, %r8d
movq 0x10(%rsp), %rdx
cmovsl %edx, %r8d
xorl %r14d, %r14d
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r11
movl 0xe8(%rsp), %r9d
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl (%rsp), %r10d
jmp 0x14b0a
xorl %r14d, %r14d
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r11
movl 0xe8(%rsp), %r9d
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %r8d
movl (%rsp), %r10d
movq 0x10(%rsp), %rdx
testb %r14b, %r14b
jne 0x14b22
incq %rdx
addq $0x70, %rbp
cmpq %rdx, %r11
jne 0x14a02
movl %r11d, %edx
movq 0x38(%rsp), %r14
xorl %ebp, %ebp
testl %r8d, %r8d
js 0x14b99
decl %edx
movl %r8d, %eax
imulq $0x70, %rax, %rax
movl 0x58(%r12,%rax), %eax
movl %eax, 0x4c(%rsp)
testl %eax, %eax
js 0x14ba6
movq %rdx, 0x10(%rsp)
movslq %edx, %rax
movq 0x68(%rsp), %rdx
movslq (%rdx,%rax,4), %rax
movl %r10d, %r13d
leal 0x1(%rax), %r10d
movq 0x80(%rsp), %rdx
leaq (%rdx,%rax,4), %rax
movslq (%rax), %rdx
imulq $0x70, %rdx, %rdx
decl %r10d
addq $-0x4, %rax
cmpl %r13d, 0x64(%r12,%rdx)
jg 0x14b68
movq %r10, 0x60(%rsp)
xorl %ebp, %ebp
movl 0x40(%rsp), %eax
movl %eax, 0x30(%rsp)
movl %r13d, %r10d
movq 0x10(%rsp), %rdx
jmp 0x14882
movl 0x40(%rsp), %eax
movl %eax, 0x30(%rsp)
jmp 0x149b6
movl 0x40(%rsp), %eax
movl %eax, 0x30(%rsp)
jmp 0x149b9
movl $0x2, %edx
movq 0x70(%rsp), %r13
movq %r13, %rdi
leaq 0x3c5fd(%rip), %rsi     # 0x511c4
callq 0x2070
testl %eax, %eax
je 0x14c01
movl $0x2, %edx
movq %r13, %rdi
leaq 0x4144e(%rip), %rsi     # 0x5602d
callq 0x2070
testl %eax, %eax
je 0x14c01
addl $-0x46, %r14d
cmpl $0x13, %r14d
ja 0x14c01
movl $0xa0005, %eax          # imm = 0xA0005
btl %r14d, %eax
jb 0x14d3f
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x144fa
movslq 0x20(%rax), %r14
movq 0x68(%rsp), %rax
movslq (%rax,%r14,4), %rax
testq %rax, %rax
js 0x14d3f
cmpl 0x60(%rsp), %eax
movq 0x20(%rsp), %r11
movl 0xe8(%rsp), %r9d
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %r8d
movl (%rsp), %r10d
movq 0x10(%rsp), %rdx
jg 0x14d73
movq 0x80(%rsp), %rcx
movslq (%rcx,%rax,4), %rax
imulq $0x70, %rax, %rax
cmpl %r10d, 0x64(%r12,%rax)
jne 0x14d73
leaq 0x90(%rsp), %rdi
leaq 0x41bf8(%rip), %rsi     # 0x5687a
movq %rdx, %r13
movl %ebp, %edx
xorl %eax, %eax
callq 0x2260
movsbl 0x90(%rsp), %edx
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %r15, %rcx
movl %r14d, %r8d
leaq 0x4c(%rsp), %r9
movq 0x68(%rsp), %r14
pushq %r14
pushq %r13
pushq 0x20(%rsp)
pushq 0x88(%rsp)
leaq 0xa4(%rsp), %rax
pushq %rax
callq 0x13d1c
addq $0x30, %rsp
testl %eax, %eax
je 0x14d0d
movq 0x50(%rsp), %rax
movb $0x74, -0x158(%rax)
movl %r13d, -0x144(%rax)
movl %r14d, -0x13c(%rax)
movl 0x4c(%rsp), %ecx
movl %ecx, -0x134(%rax)
movl 0x30(%rsp), %ecx
movl %ecx, -0x130(%rax)
incl 0x40(%rsp)
incl %ebp
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r11
movl 0xe8(%rsp), %r9d
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %r8d
movl (%rsp), %r10d
movq %r13, %rdx
jmp 0x149b9
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %rcx
movq 0x20(%rsp), %r11
movl 0xe8(%rsp), %r9d
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl 0x4(%rsp), %r8d
movl (%rsp), %r10d
movq 0x10(%rsp), %rdx
jmp 0x149b9
movq 0x38(%rsp), %r14
movq 0x8(%rsp), %rcx
jmp 0x149b9
testl %r9d, %r9d
jle 0x14e84
movslq 0x48(%rsp), %r10
testq %r10, %r10
jle 0x14e63
leaq 0x64(%r12), %r14
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r14, 0x10(%rsp)
imulq $0x158, %r15, %rax      # imm = 0x158
leaq (%rbx,%rax), %rdx
movq %rdx, 0x30(%rsp)
movslq 0x10(%rbx,%rax), %rdi
movl $0x0, %eax
cmpl %edi, %ecx
jle 0x14df4
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movl 0x64(%rax), %r8d
imulq $0x70, %rdi, %rdx
addq %r14, %rdx
movl %ecx, %esi
subl %edi, %esi
xorl %eax, %eax
cmpl %r8d, (%rdx)
jne 0x14df4
addl -0x4(%rdx), %eax
addq $0x70, %rdx
decl %esi
jne 0x14de4
cmpl %r9d, %eax
jl 0x14e55
movl %r13d, %eax
cmpq %rax, %r15
je 0x14e52
movslq %r13d, %rax
imulq $0x158, %rax, %rdi      # imm = 0x158
addq %rbx, %rdi
movl $0x158, %edx            # imm = 0x158
movq 0x30(%rsp), %rsi
movq %rbx, %r14
movl %r13d, %ebx
movl %ebp, %r13d
movq %r12, %rbp
movq %r10, %r12
callq 0x21a0
movq %r12, %r10
movq %rbp, %r12
movl %r13d, %ebp
movl %ebx, %r13d
movq %r14, %rbx
movq 0x10(%rsp), %r14
movl 0xe8(%rsp), %r9d
movq 0x20(%rsp), %r11
movq 0x8(%rsp), %rcx
incl %r13d
incq %r15
cmpq %r10, %r15
jne 0x14da9
jmp 0x14e70
xorl %r13d, %r13d
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x50(%rsp)
movl %r13d, 0x48(%rsp)
movq 0x38(%rsp), %r14
movl 0x10(%rbx), %eax
movl 0x48(%rsp), %r13d
movl $0x0, 0x44(%rsp)
testl %ecx, %ecx
jle 0x14f0d
leal -0x1(%r13), %r10d
leaq 0x2c0(%rbx), %rdx
addq $0x64, %r12
xorl %esi, %esi
xorl %edi, %edi
movslq %eax, %r8
cmpq %r8, %rsi
setge %r8b
cmpl %r10d, %edi
setl %r9b
andb %r8b, %r9b
cmpb $0x1, %r9b
jne 0x14ede
leal 0x1(%rdi), %r8d
cmpl %r10d, %r8d
jge 0x14edb
movslq %edi, %rax
imulq $0x158, %rax, %rax      # imm = 0x158
movl (%rdx,%rax), %eax
movl %r8d, %edi
movslq %edi, %r8
imulq $0x158, %r8, %r8        # imm = 0x158
movq 0x8(%rbx,%r8), %r8
movq (%r8), %r8
movl 0x64(%r8), %r8d
cmpl (%r12), %r8d
jne 0x14eff
movl %edi, -0x8(%r12)
incq %rsi
addq $0x70, %r12
cmpq %rsi, %r11
jne 0x14eab
jmp 0x14f0f
xorl %edi, %edi
movl %edi, 0x44(%rsp)
testl %ecx, %ecx
jle 0x14f64
shlq $0x6, %r11
leaq (%r11,%r11,2), %rax
xorl %ecx, %ecx
movq (%r14,%rcx), %rdx
testq %rdx, %rdx
je 0x14f64
movslq 0x5c(%rdx), %rdx
testq %rdx, %rdx
js 0x14f58
movq 0x28(%r14,%rcx), %rdi
testq %rdi, %rdi
je 0x14f58
imulq $0x158, %rdx, %rsi      # imm = 0x158
cmpq %rdi, 0x8(%rbx,%rsi)
jne 0x14f58
addq %rbx, %rsi
cmpb $0x74, (%rsi)
jne 0x14f58
movl %edx, 0x3c(%r14,%rcx)
addq $0xc0, %rcx
cmpq %rcx, %rax
jne 0x14f21
testl %r13d, %r13d
movq %r13, 0x30(%rsp)
jle 0x14fcd
addq $-0x158, %rbx           # imm = 0xFEA8
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
leaq 0x418f8(%rip), %r15     # 0x5687a
movq 0x160(%rbx), %rax
movq (%rax), %rax
movl 0x64(%rax), %r12d
cmpl %r14d, %r12d
je 0x14fb1
movsbl 0x68(%rax), %esi
leal 0x1(%r12), %edx
xorl %ebp, %ebp
leaq 0x418d7(%rip), %rdi     # 0x5687e
xorl %eax, %eax
callq 0x20f0
movl %r12d, %r14d
incl %ebp
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
addq $0x158, %rbx            # imm = 0x158
decq %r13
jne 0x14f82
jmp 0x14fd2
movq 0x50(%rsp), %rbx
movq %rbx, 0x50(%rsp)
movl $0xa, %edi
callq 0x2060
movq 0x30(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq