// File: /workspace/github_c_makefile_star2_O2/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, 0x70(%rsp)
movq %r8, 0x60(%rsp)
movq %rdx, %r12
movq %rsi, %r10
movq %rdi, %r9
leaq 0x44(%rsp), %rax
andl $0x0, (%rax)
leaq 0x40(%rsp), %rax
andl $0x0, (%rax)
movl 0xe8(%rsp), %r11d
movl 0xe0(%rsp), %eax
movl %eax, 0x1c(%rsp)
leaq 0x78(%rsp), %rax
movq %rdi, (%rax)
leaq 0x6c(%rsp), %rax
movl $0x1, (%rax)
leal -0x1(%rcx), %eax
movq %rcx, 0x48(%rsp)
movslq %ecx, %r14
movslq %eax, %rsi
leaq 0x18(%rdx), %rax
movq %rax, 0x88(%rsp)
pushq $-0x1
popq %rax
movb $0x20, %cl
movl %ecx, 0x50(%rsp)
pushq $0x1
popq %rdi
movl %eax, %edx
movl $0x0, 0x2c(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x58(%rsp)
xorl %ecx, %ecx
movl %eax, %r8d
xorl %ebp, %ebp
movq %r9, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rsi, 0x20(%rsp)
cmpq %rsi, %rdi
jge 0x13697
imulq $0xc0, %rdi, %r15
movq (%r10,%r15), %rax
testq %rax, %rax
je 0x13697
addq %r10, %r15
cmpq $0x0, 0x28(%r15)
je 0x133ad
movl 0x64(%rax), %r13d
cmpl %edx, %r13d
movq %rdi, 0x10(%rsp)
jne 0x133b4
movl %edx, %ebx
movl %ecx, %r13d
testl %r8d, %r8d
js 0x133ea
cmpl $0x0, 0x2c(%rsp)
js 0x133ea
movl 0x20(%r15), %ecx
cmpl %r8d, %ecx
jl 0x133ea
cmpl %r13d, %ecx
jg 0x133ea
cmpl $0x0, 0x30(%r15)
jne 0x133ea
movzbl 0x18(%rax), %ecx
cmpl $0x43, %ecx
je 0x13519
cmpl $0x4f, %ecx
je 0x13374
cmpl $0x4e, %ecx
jne 0x1337a
cmpb $0x20, 0x19(%rax)
je 0x133ea
movl %r8d, 0x8(%rsp)
xorl %r8d, %r8d
movzbl 0x50(%rsp), %ecx
leal -0x41(%rcx), %esi
cmpl $0xf, %esi
ja 0x1353a
movl $0x8041, %edx           # imm = 0x8041
btl %esi, %edx
jae 0x1353a
movq 0x20(%rsp), %rsi
movl 0x8(%rsp), %r8d
jmp 0x133ea
movl %edx, %ebx
movl %ecx, %r13d
jmp 0x133ea
movq %r10, %rbx
movl %r11d, %r10d
movzbl 0x68(%rax), %edx
movl %edx, 0x50(%rsp)
addl $-0x41, %edx
cmpl $0xf, %edx
ja 0x133f7
movl %r8d, %r11d
movl $0x8041, %r8d           # imm = 0x8041
btl %edx, %r8d
jae 0x133f7
movl %ecx, %eax
movl %r11d, %r8d
movl %r10d, %r11d
movq %rbx, %r10
movl %r13d, %ebx
movq %rax, %r13
incq %rdi
movl %ebx, %edx
movl %r13d, %ecx
jmp 0x132e6
andl $0x0, 0x40(%rsp)
movslq 0x20(%r15), %rdx
movq 0x48(%rsp), %rsi
cmpl %esi, %edx
cmovgl %edx, %esi
movq %rsi, %rbp
imulq $0x70, %rdx, %rsi
addq 0x88(%rsp), %rsi
pushq $-0x1
popq %r8
movq 0x10(%rsp), %rdi
movl %r10d, %r11d
movq %rbx, %r10
movl %r13d, %ebx
cmpq %r14, %rdx
jge 0x1348d
cmpl %ebx, 0x4c(%rsi)
jg 0x13488
movzbl (%rsi), %edi
cmpl $0x43, %edi
je 0x13460
cmpl $0x4e, %edi
je 0x1345a
cmpl $0x4f, %edi
movq 0x10(%rsp), %rdi
jne 0x13478
cmpb $0x54, 0x1(%rsi)
je 0x13488
cmpb $0x58, 0x1(%rsi)
jne 0x13478
jmp 0x13488
cmpb $0x20, 0x1(%rsi)
jmp 0x13471
cmpb $0x41, 0x1(%rsi)
jne 0x1346d
movq 0x10(%rsp), %rdi
jmp 0x1347f
cmpb $0x42, 0x1(%rsi)
movq 0x10(%rsp), %rdi
je 0x1347f
testl %r8d, %r8d
cmovsl %edx, %r8d
incq %rdx
addq $0x70, %rsi
jmp 0x1342b
movl %edx, %r13d
jmp 0x13490
movq %rbp, %r13
xorl %ebp, %ebp
testl %r8d, %r8d
movq 0x20(%rsp), %rsi
js 0x134ff
decl %r13d
movl %r8d, %ecx
imulq $0x70, %rcx, %rcx
movl 0x58(%r12,%rcx), %ecx
movl %ecx, 0x2c(%rsp)
testl %ecx, %ecx
js 0x1350c
movq %r13, 0x8(%rsp)
movslq %r13d, %rcx
movq 0x60(%rsp), %rdx
movslq (%rdx,%rcx,4), %rcx
leal 0x1(%rcx), %r13d
movq 0x70(%rsp), %rdx
leaq (%rdx,%rcx,4), %rcx
movslq (%rcx), %rdx
imulq $0x70, %rdx, %rdx
decl %r13d
addq $-0x4, %rcx
cmpl %ebx, 0x64(%r12,%rdx)
jg 0x134d1
movq %r13, 0x58(%rsp)
xorl %ebp, %ebp
movl 0x1c(%rsp), %ecx
movl %ecx, 0x18(%rsp)
movq 0x8(%rsp), %r13
jmp 0x13328
movl 0x1c(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x133af
movl 0x1c(%rsp), %eax
movl %eax, 0x18(%rsp)
jmp 0x133ea
cmpb $0x41, 0x19(%rax)
je 0x133ea
movl %r8d, 0x8(%rsp)
movb $0x1, %r8b
cmpb $0x20, 0x19(%rax)
jne 0x13382
jmp 0x133a6
testb %r8b, %r8b
je 0x1354b
cmpb $0x42, 0x19(%rax)
je 0x13561
cmpb $0x47, 0x19(%rax)
je 0x13561
addl $-0x46, %ecx
cmpl $0x13, %ecx
ja 0x13561
movl $0xa0005, %eax          # imm = 0xA0005
btl %ecx, %eax
jb 0x133a1
movq %rdi, %rsi
movq %r15, %rdi
movq 0x48(%rsp), %rdx
callq 0x12fb0
movslq 0x20(%rax), %rdx
movq 0x60(%rsp), %rax
movslq (%rax,%rdx,4), %rax
testq %rax, %rax
js 0x13676
cmpl 0x58(%rsp), %eax
movq 0x30(%rsp), %r9
movq 0x38(%rsp), %r10
movl 0xe8(%rsp), %r11d
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
movl 0x8(%rsp), %r8d
jg 0x133ea
movq 0x70(%rsp), %rcx
movslq (%rcx,%rax,4), %rax
imulq $0x70, %rax, %rax
cmpl %ebx, 0x64(%r12,%rax)
jne 0x133ea
movl %r8d, 0x8(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x3d1ec(%rip), %rsi     # 0x507ca
movq %rdx, 0x80(%rsp)
movl %ebp, %edx
xorl %eax, %eax
callq 0x2260
movsbl 0x90(%rsp), %edx
subq $0x8, %rsp
leaq 0x80(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
movq %r15, %rcx
movq 0x88(%rsp), %r8
leaq 0x48(%rsp), %r9
movq 0x60(%rsp), %r15
pushq %r15
pushq %r13
pushq 0x60(%rsp)
pushq 0x80(%rsp)
leaq 0x94(%rsp), %rax
pushq %rax
callq 0x12a3b
addq $0x30, %rsp
testl %eax, %eax
je 0x13676
movq 0x78(%rsp), %rax
movb $0x74, -0x158(%rax)
movl %r13d, -0x144(%rax)
movl %r15d, -0x13c(%rax)
movl 0x2c(%rsp), %ecx
movl %ecx, -0x134(%rax)
movl 0x18(%rsp), %ecx
movl %ecx, -0x130(%rax)
incl 0x1c(%rsp)
incl %ebp
movq 0x30(%rsp), %r9
movq 0x38(%rsp), %r10
movl 0xe8(%rsp), %r11d
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %rdi
jmp 0x133a6
movl 0x44(%rsp), %r15d
testl %r11d, %r11d
jle 0x1375d
xorl %ebx, %ebx
testl %r15d, %r15d
movl %r15d, %r13d
cmovlel %ebx, %r13d
leaq 0x64(%r12), %rax
movq %rax, 0x10(%rsp)
xorl %r15d, %r15d
movq %r13, 0x50(%rsp)
cmpq %r13, %rbx
je 0x1375d
imulq $0x158, %rbx, %rax      # imm = 0x158
leaq (%r9,%rax), %rsi
movslq 0x10(%r9,%rax), %rax
imulq $0x70, %rax, %rcx
addq 0x10(%rsp), %rcx
xorl %edx, %edx
cmpq %r14, %rax
jge 0x13707
movl (%rcx), %edi
movq 0x8(%rsi), %r8
movq (%r8), %r8
cmpl 0x64(%r8), %edi
jne 0x13707
addl -0x4(%rcx), %edx
incq %rax
addq $0x70, %rcx
jmp 0x136e7
cmpl %r11d, %edx
jl 0x13755
movl %r15d, %eax
cmpq %rax, %rbx
je 0x13752
movslq %r15d, %rax
imulq $0x158, %rax, %rdi      # imm = 0x158
addq %r9, %rdi
movl $0x158, %edx            # imm = 0x158
movq %r15, %r13
movq %r12, %r15
movl %ebp, %r12d
movl %r11d, %ebp
callq 0x21a0
movl %ebp, %r11d
movl %r12d, %ebp
movq %r15, %r12
movq %r13, %r15
movq 0x50(%rsp), %r13
movq 0x38(%rsp), %r10
movq 0x30(%rsp), %r9
incl %r15d
incq %rbx
jmp 0x136c3
movl 0x10(%r9), %edx
leal -0x1(%r15), %eax
leaq 0x2c0(%r9), %rcx
xorl %esi, %esi
movq 0x48(%rsp), %r11
testl %r11d, %r11d
cmovlel %esi, %r11d
addq $0x64, %r12
xorl %edi, %edi
cmpq %rsi, %r11
je 0x137d4
movslq %edx, %r8
cmpq %r8, %rsi
jl 0x137aa
cmpl %eax, %edi
jge 0x137aa
leal 0x1(%rdi), %r8d
cmpl %eax, %r8d
jge 0x137a7
movslq %edi, %rdx
imulq $0x158, %rdx, %rdx      # imm = 0x158
movl (%rcx,%rdx), %edx
movl %r8d, %edi
movslq %edi, %r8
imulq $0x158, %r8, %r8        # imm = 0x158
movq 0x8(%r9,%r8), %r8
movq (%r8), %r8
movl 0x64(%r8), %r8d
cmpl (%r12), %r8d
jne 0x137cb
movl %edi, -0x8(%r12)
incq %rsi
addq $0x70, %r12
jmp 0x13780
imulq $0xc0, %r11, %rax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x13822
movq (%r10,%rcx), %rdx
testq %rdx, %rdx
je 0x13822
movslq 0x5c(%rdx), %rdx
testq %rdx, %rdx
js 0x13819
movq 0x28(%r10,%rcx), %rdi
testq %rdi, %rdi
je 0x13819
imulq $0x158, %rdx, %rsi      # imm = 0x158
cmpq %rdi, 0x8(%r9,%rsi)
jne 0x13819
addq %r9, %rsi
cmpb $0x74, (%rsi)
jne 0x13819
movl %edx, 0x3c(%r10,%rcx)
addq $0xc0, %rcx
jmp 0x137dd
testl %r15d, %r15d
movl $0x0, %eax
movq %r15, 0x10(%rsp)
cmovgl %r15d, %eax
imulq $0x158, %rax, %rbx      # imm = 0x158
pushq $-0x1
popq %r13
leaq 0x3cf85(%rip), %r14     # 0x507ca
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x13896
movq 0x8(%r9,%r15), %rax
movq (%rax), %rax
movl 0x64(%rax), %r12d
cmpl %r13d, %r12d
je 0x1387a
movsbl 0x68(%rax), %esi
leal 0x1(%r12), %edx
xorl %ebp, %ebp
leaq 0x3cf5e(%rip), %rdi     # 0x507ce
xorl %eax, %eax
callq 0x20f0
movl %r12d, %r13d
incl %ebp
movq %r14, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
addq $0x158, %r15            # imm = 0x158
movq 0x30(%rsp), %r9
jmp 0x13848
pushq $0xa
popq %rdi
callq 0x2060
movq 0x10(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)