// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movq %r8, 0x50(%rsp)
movl %ecx, %r11d
movq %rdx, %r12
movq %rsi, %r10
movq %rdi, %rbx
xorl %ebp, %ebp
movl %ebp, 0x44(%rsp)
movl %ebp, 0x40(%rsp)
movq %rdi, 0x68(%rsp)
movl $0x1, 0x5c(%rsp)
movl $0x0, %eax
movq %rax, (%rsp)
cmpl $0x3, %ecx
jl 0x14a6d
movq %r9, 0x60(%rsp)
movl 0xd0(%rsp), %r9d
leal -0x1(%r11), %esi
leaq 0x18(%r12), %rax
movq %rax, 0x78(%rsp)
movb $0x20, %al
movl %eax, 0x3c(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movl $0x1, %r13d
xorl %ebp, %ebp
xorl %ecx, %ecx
movq $0x0, 0x48(%rsp)
movl $0x0, 0x14(%rsp)
movl $0x0, 0x2c(%rsp)
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq %r10, 0x30(%rsp)
movq %r11, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
leaq (,%r13,2), %r15
addq %r13, %r15
shlq $0x6, %r15
movq (%r10,%r15), %rax
testq %rax, %rax
je 0x149a4
addq %r10, %r15
cmpq $0x0, 0x28(%r15)
je 0x146e3
movl 0x64(%rax), %edi
cmpl %edx, %edi
jne 0x146e7
movl %edx, %edi
movl %ecx, %r14d
movl (%rsp), %edx
testl %edx, %edx
js 0x1470a
cmpl $0x0, 0x2c(%rsp)
js 0x1470a
movl 0x20(%r15), %ecx
cmpl %edx, %ecx
jl 0x1470a
cmpl %r14d, %ecx
jg 0x1470a
cmpl $0x0, 0x30(%r15)
jne 0x1470a
movzbl 0x18(%rax), %ecx
cmpl $0x43, %ecx
je 0x147b8
cmpl $0x4f, %ecx
je 0x146af
cmpl $0x4e, %ecx
jne 0x146b5
cmpb $0x20, 0x19(%rax)
je 0x1470a
xorl %edx, %edx
movzbl 0x3c(%rsp), %ecx
leal -0x41(%rcx), %esi
cmpl $0xf, %esi
ja 0x1484a
movl %edi, %r8d
movl $0x8041, %edi           # imm = 0x8041
btl %esi, %edi
movl %r8d, %edi
jae 0x1484a
movq 0x8(%rsp), %rsi
jmp 0x1470a
movl %edx, %edi
jmp 0x14707
movzbl 0x68(%rax), %edx
movl %edx, 0x3c(%rsp)
addl $-0x41, %edx
cmpl $0xf, %edx
movl %edi, %r8d
ja 0x14720
movl $0x8041, %edi           # imm = 0x8041
btl %edx, %edi
movl %r8d, %edi
jae 0x14720
movl %ecx, %r14d
incq %r13
movl %r14d, %ecx
movl %edi, %edx
cmpq %rsi, %r13
jne 0x14638
jmp 0x149a4
movl $0x0, 0x40(%rsp)
movslq 0x20(%r15), %r14
xorl %ebp, %ebp
cmpl %r11d, %r14d
jge 0x147af
imulq $0x70, %r14, %rdx
addq 0x78(%rsp), %rdx
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
cmpl %edi, 0x4c(%rdx)
jg 0x147d6
movzbl (%rdx), %esi
cmpl $0x43, %esi
je 0x14777
cmpl $0x4e, %esi
je 0x14771
cmpl $0x4f, %esi
movq 0x8(%rsp), %rsi
jne 0x1478f
cmpb $0x54, 0x1(%rdx)
je 0x147d3
cmpb $0x58, 0x1(%rdx)
jne 0x1478f
jmp 0x147d3
cmpb $0x20, 0x1(%rdx)
jmp 0x14788
cmpb $0x41, 0x1(%rdx)
jne 0x14784
movq 0x8(%rsp), %rsi
jmp 0x1479e
cmpb $0x42, 0x1(%rdx)
movq 0x8(%rsp), %rsi
je 0x1479b
movl (%rsp), %edi
testl %edi, %edi
cmovsl %r14d, %edi
movl %edi, (%rsp)
movl %r8d, %edi
incq %r14
addq $0x70, %rdx
cmpl %r14d, %r11d
jne 0x14743
movl %r11d, %r14d
jmp 0x147d6
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
jmp 0x14836
cmpb $0x41, 0x19(%rax)
je 0x1470a
movb $0x1, %dl
cmpb $0x20, 0x19(%rax)
jne 0x146b7
jmp 0x1470a
movl %r8d, %edi
movl (%rsp), %edx
testl %edx, %edx
js 0x14836
decl %r14d
movl %edx, %ecx
imulq $0x70, %rcx, %rcx
movl 0x58(%r12,%rcx), %ecx
movl %ecx, 0x2c(%rsp)
testl %ecx, %ecx
js 0x14840
movslq %r14d, %rcx
movq 0x50(%rsp), %rdx
movslq (%rdx,%rcx,4), %rcx
movl %edi, %r8d
leal 0x1(%rcx), %edi
movq 0x60(%rsp), %rdx
leaq (%rdx,%rcx,4), %rcx
movslq (%rcx), %rdx
imulq $0x70, %rdx, %rdx
decl %edi
addq $-0x4, %rcx
cmpl %r8d, 0x64(%r12,%rdx)
jg 0x1480e
movq %rdi, 0x48(%rsp)
xorl %ebp, %ebp
movl %r9d, 0x14(%rsp)
movl %r8d, %edi
jmp 0x1466e
movl %r9d, 0x14(%rsp)
jmp 0x14707
movl %r9d, 0x14(%rsp)
jmp 0x1470a
testb %dl, %dl
je 0x1485a
cmpb $0x42, 0x19(%rax)
je 0x14870
cmpb $0x47, 0x19(%rax)
je 0x14870
addl $-0x46, %ecx
cmpl $0x13, %ecx
ja 0x14870
movl $0xa0005, %eax          # imm = 0xA0005
btl %ecx, %eax
jb 0x146dc
movl %edi, 0x18(%rsp)
movl %r9d, 0x1c(%rsp)
movq %r15, %rdi
movl %r13d, %esi
movl %r11d, %edx
callq 0x14222
movslq 0x20(%rax), %r8
movq 0x50(%rsp), %rax
movslq (%rax,%r8,4), %rax
testq %rax, %rax
js 0x14987
cmpl 0x48(%rsp), %eax
movq 0x30(%rsp), %r10
movq 0x60(%rsp), %rcx
movl 0x1c(%rsp), %r9d
movq 0x8(%rsp), %rsi
movl 0x18(%rsp), %edi
jg 0x1497d
movslq (%rcx,%rax,4), %rax
imulq $0x70, %rax, %rax
cmpl %edi, 0x64(%r12,%rax)
jne 0x1497d
leaq 0x80(%rsp), %rdi
leaq 0x41fb1(%rip), %rsi     # 0x56892
movl %ebp, %edx
xorl %eax, %eax
movq %r8, 0x70(%rsp)
callq 0x2270
movsbl 0x80(%rsp), %edx
subq $0x8, %rsp
leaq 0x70(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
movq %r15, %rcx
movq 0x78(%rsp), %r8
leaq 0x48(%rsp), %r9
movq 0x50(%rsp), %r15
pushq %r15
pushq %r14
pushq 0x38(%rsp)
pushq 0x70(%rsp)
leaq 0x84(%rsp), %rax
pushq %rax
callq 0x13c74
addq $0x30, %rsp
testl %eax, %eax
je 0x14987
movq 0x68(%rsp), %rax
movb $0x74, -0x158(%rax)
movl %r14d, -0x144(%rax)
movl %r15d, -0x13c(%rax)
movl 0x2c(%rsp), %ecx
movl %ecx, -0x134(%rax)
movl 0x14(%rsp), %ecx
movl %ecx, -0x130(%rax)
movl 0x1c(%rsp), %r9d
incl %r9d
incl %ebp
movq 0x30(%rsp), %r10
movq 0x20(%rsp), %r11
jmp 0x14996
movq 0x20(%rsp), %r11
jmp 0x1470a
movq 0x30(%rsp), %r10
movq 0x20(%rsp), %r11
movl 0x1c(%rsp), %r9d
movq 0x8(%rsp), %rsi
movl 0x18(%rsp), %edi
jmp 0x1470a
movl 0x44(%rsp), %r9d
movl 0xd8(%rsp), %r13d
testl %r13d, %r13d
movq %r9, (%rsp)
jle 0x14a6d
testl %r9d, %r9d
jle 0x14a65
leaq 0x64(%r12), %rax
movq %rax, 0x8(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
imulq $0x158, %r15, %rax      # imm = 0x158
leaq (%rbx,%rax), %rsi
movslq 0x10(%rbx,%rax), %r8
movl $0x0, %eax
cmpl %r8d, %r11d
jle 0x14a1b
movq 0x8(%rsi), %rax
movq (%rax), %rax
movl 0x64(%rax), %ecx
imulq $0x70, %r8, %rdx
addq 0x8(%rsp), %rdx
movl %r11d, %edi
subl %r8d, %edi
xorl %eax, %eax
cmpl %ecx, (%rdx)
jne 0x14a1b
addl -0x4(%rdx), %eax
addq $0x70, %rdx
decl %edi
jne 0x14a0c
cmpl %r13d, %eax
jl 0x14a50
movl %r14d, %eax
cmpq %rax, %r15
je 0x14a4d
movslq %r14d, %rax
imulq $0x158, %rax, %rdi      # imm = 0x158
addq %rbx, %rdi
movl $0x158, %edx            # imm = 0x158
callq 0x21b0
movq (%rsp), %r9
movq 0x20(%rsp), %r11
movq 0x30(%rsp), %r10
incl %r14d
incq %r15
cmpq %r9, %r15
jne 0x149d7
movl %r14d, %eax
movq %rax, (%rsp)
jmp 0x14a6d
movq $0x0, (%rsp)
testl %r11d, %r11d
movq (%rsp), %r8
jle 0x14b3d
leal -0x1(%r8), %ecx
leaq 0x2c0(%rbx), %rdx
movl 0x10(%rbx), %esi
movl %r11d, %eax
addq $0x64, %r12
xorl %edi, %edi
xorl %r8d, %r8d
movslq %esi, %r9
cmpq %r9, %rdi
jl 0x14aba
cmpl %ecx, %r8d
jge 0x14aba
leal 0x1(%r8), %r9d
cmpl %ecx, %r9d
jge 0x14ab7
movslq %r8d, %rsi
imulq $0x158, %rsi, %rsi      # imm = 0x158
movl (%rdx,%rsi), %esi
movl %r9d, %r8d
movslq %r8d, %r9
imulq $0x158, %r9, %r9        # imm = 0x158
movq 0x8(%rbx,%r9), %r9
movq (%r9), %r9
movl 0x64(%r9), %r9d
cmpl (%r12), %r9d
jne 0x14adb
movl %r8d, -0x8(%r12)
incq %rdi
addq $0x70, %r12
cmpq %rdi, %rax
jne 0x14a94
testl %r11d, %r11d
movq (%rsp), %r8
jle 0x14b3d
shlq $0x6, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
movq (%r10,%rcx), %rdx
testq %rdx, %rdx
je 0x14b3d
movslq 0x5c(%rdx), %rdx
testq %rdx, %rdx
js 0x14b31
movq 0x28(%r10,%rcx), %rdi
testq %rdi, %rdi
je 0x14b31
imulq $0x158, %rdx, %rsi      # imm = 0x158
cmpq %rdi, 0x8(%rbx,%rsi)
jne 0x14b31
addq %rbx, %rsi
cmpb $0x74, (%rsi)
jne 0x14b31
movl %edx, 0x3c(%r10,%rcx)
addq $0xc0, %rcx
cmpq %rcx, %rax
jne 0x14afa
testl %r8d, %r8d
jle 0x14ba1
movq %rbx, %r14
movl (%rsp), %eax
imulq $0x158, %rax, %r12      # imm = 0x158
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x8(%r14,%r13), %rax
movq (%rax), %rax
movl 0x64(%rax), %r15d
cmpl %ebx, %r15d
je 0x14b83
movsbl 0x68(%rax), %esi
leal 0x1(%r15), %edx
xorl %ebp, %ebp
leaq 0x41d1d(%rip), %rdi     # 0x56896
xorl %eax, %eax
callq 0x20f0
movl %r15d, %ebx
incl %ebp
leaq 0x41d06(%rip), %rdi     # 0x56892
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
addq $0x158, %r13            # imm = 0x158
cmpq %r13, %r12
jne 0x14b57
movl $0xa, %edi
callq 0x2060
movq (%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)