// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/align_tnm.c
// Function: Purge_not_aligned
void Purge_not_aligned(struct chain *chains1, atom *atoms1, int *natoms1,
		       struct chain *chains2, atom *atoms2, int *natoms2)
{
  // Not aligned atoms are purged, except side chain atoms in aligned residues
  int k1=0, k2=0, j_old=0, i, j, ichain=-1;
  int notali=0, notalimax=1000; atom notali_atom[notalimax];
  printf("Not aligned atoms: ");
  struct chain *ch1=NULL, *ch2=NULL;
  for(i=0; i<*natoms1; i++){
    atom *a1=atoms1+i;
    if(a1->chain!=ichain){
      ichain=a1->chain;
      ch1=chains1+ichain; ch1->ini_atom=-1;
      ch2=chains2+ch1->match; ch2->ini_atom=-1;
      printf("Chain %d ",ichain);
    }
    j=a1->ali;
    if(j>=0){
      // if((j>=0)||
      //  ((ch1->alignres[ch1->inires+a1->res]>=0)&&(a1->main==0))){ //
      // aligned atom or side chain atom in aligned residue
      if(ch1->ini_atom<0)ch1->ini_atom=k1;
      if(i!=k1)atoms1[k1]=*a1;
      // if(j>=0){
      if(ch2->ini_atom<0)ch2->ini_atom=k2;
      if(j!=k2){
	for(int k=j_old+1; k<j; k++){
	  if(notali>=notalimax)break;
	  notali_atom[notali]=atoms2[k]; notali++;
	}
	atoms2[k2]=atoms2[j];
      }
      atoms1[k1].ali=k2; j_old=j; k2++;
      // }else{printf("Saved: %s/%c%d", a1->name, a1->aa, a1->res);}
      k1++;
    }else{
      // not aligned
      printf(" %s/%c%d", a1->name, a1->aa, a1->res);
      ch1->natoms--;
    }
  }
  printf("\nn1= %d n2= %d\n", k1, k2);
  if(k1 != *natoms1){
    printf("WARNING, eliminating %d not aligned atoms of prot 1 (%d)\n",
	   *natoms1-k1, *natoms1); *natoms1=k1;
  }
  if(k2 != *natoms2){
    printf("WARNING, eliminating %d not aligned atoms of prot 2 (%d)\n",
	   *natoms2-k2, *natoms2); *natoms2=k2;
    atom *a1=notali_atom;
    for(int k=0; k<notali; k++){
      printf(" %s/%c%d/%d", a1->name, a1->aa, a1->res, a1->chain); a1++;
    }
    printf("\n");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b5d8, %rsp          # imm = 0x1B5D8
movq %r9, 0x28(%rsp)
movq %r8, %rbx
movq %rcx, 0x48(%rsp)
movq %rdx, %r12
movq %rsi, 0x10(%rsp)
movq %rdi, 0x40(%rsp)
leaq 0x3eed2(%rip), %rdi     # 0x50221
xorl %eax, %eax
callq 0x20f0
movq %rbx, 0x38(%rsp)
leaq 0x70(%rbx), %rax
movq %rax, 0x30(%rsp)
pushq $-0x1
popq %r9
pushq $0xe
popq %r14
xorl %r8d, %r8d
xorl %r15d, %r15d
xorl %ecx, %ecx
xorl %r13d, %r13d
movq $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
xorl %ebx, %ebx
movslq (%r12), %rax
cmpq %rax, %r8
jge 0x11543
movl %ecx, (%rsp)
movl %ebx, %ebp
movl %r13d, 0x4(%rsp)
movq %r12, %rbx
imulq $0x70, %r8, %r13
movq 0x10(%rsp), %rax
movl 0x6c(%rax,%r13), %r12d
cmpl %r9d, %r12d
je 0x1140a
movslq %r12d, %rax
imulq $0x70, %rax, %rax
movq 0x40(%rsp), %rcx
orl $-0x1, (%rcx,%rax)
leaq (%rcx,%rax), %rdx
movq %rdx, 0x8(%rsp)
movslq 0x40(%rcx,%rax), %rax
imulq $0x70, %rax, %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, 0x18(%rsp)
orl $-0x1, (%rcx,%rax)
leaq 0x3ee42(%rip), %rdi     # 0x50235
movl %r12d, %esi
xorl %eax, %eax
movq %r8, %r14
callq 0x20f0
movq %r14, %r8
pushq $0xe
popq %r14
movl %r12d, %r9d
addq 0x10(%rsp), %r13
movslq 0x54(%r13), %rax
testq %rax, %rax
js 0x114c0
movq 0x8(%rsp), %rcx
cmpl $0x0, (%rcx)
movq %rbx, %r12
movl %ebp, %ebx
jns 0x11432
movq 0x8(%rsp), %rcx
movl %ebx, (%rcx)
movl %ebx, %ecx
movslq %ebx, %rdx
cmpq %rcx, %r8
je 0x1144e
imulq $0x70, %rdx, %rdi
addq 0x10(%rsp), %rdi
movq %r14, %rcx
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x18(%rsp), %rcx
cmpl $0x0, (%rcx)
movl 0x4(%rsp), %r13d
jns 0x11465
movq 0x18(%rsp), %rcx
movl %r13d, (%rcx)
cmpl %r13d, %eax
je 0x11526
movl %r9d, 0x24(%rsp)
movq %r8, %rbp
movslq %r15d, %r15
movslq (%rsp), %r8
imulq $0x70, %r8, %r9
incq %r8
addq 0x30(%rsp), %r9
imulq $0x70, %r15, %rcx
leaq (%rsp,%rcx), %r10
addq $0x50, %r10
xorl %r11d, %r11d
cmpq %rax, %r8
jge 0x11502
cmpq $0x3e7, %r15            # imm = 0x3E7
jg 0x11502
leaq (%r10,%r11), %rdi
leaq (%r9,%r11), %rsi
movq %r14, %rcx
rep  movsq (%rsi), %es:(%rdi)
incq %r15
incq %r8
addq $0x70, %r11
jmp 0x11498
leaq 0x18(%r13), %rsi
movsbl 0x68(%r13), %edx
movl 0x64(%r13), %ecx
leaq 0x3ed6b(%rip), %rdi     # 0x5023f
xorl %eax, %eax
movq %r8, %r14
movl %r9d, %r12d
callq 0x20f0
movl %r12d, %r9d
movq %r14, %r8
pushq $0xe
popq %r14
movq 0x8(%rsp), %rax
decl 0x4(%rax)
movl (%rsp), %eax
movq %rbx, %r12
movl 0x4(%rsp), %r13d
movl %ebp, %ebx
jmp 0x11539
movslq %r13d, %rcx
imulq $0x70, %rcx, %rdi
movq 0x38(%rsp), %rcx
addq %rcx, %rdi
imulq $0x70, %rax, %rsi
addq %rcx, %rsi
movq %r14, %rcx
rep  movsq (%rsi), %es:(%rdi)
movq %rbp, %r8
movl 0x24(%rsp), %r9d
imulq $0x70, %rdx, %rcx
movq 0x10(%rsp), %rdx
movl %r13d, 0x54(%rdx,%rcx)
incl %r13d
incl %ebx
incq %r8
movl %eax, %ecx
jmp 0x1138b
leaq 0x3ecfe(%rip), %rdi     # 0x50248
movl %ebx, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x20f0
movl (%r12), %edx
movl %edx, %esi
subl %ebx, %esi
je 0x11572
leaq 0x3ecf1(%rip), %rdi     # 0x50258
xorl %eax, %eax
callq 0x20f0
movl %ebx, (%r12)
movq 0x28(%rsp), %rbx
movl (%rbx), %edx
movl %edx, %esi
subl %r13d, %esi
je 0x115d9
leaq 0x3ed0b(%rip), %rdi     # 0x50292
xorl %eax, %eax
callq 0x20f0
movl %r13d, (%rbx)
testl %r15d, %r15d
movl $0x0, %ebp
cmovgl %r15d, %ebp
leaq 0xbc(%rsp), %r14
leaq 0x3ed20(%rip), %rbx     # 0x502cc
subl $0x1, %ebp
jb 0x115d1
leaq -0x54(%r14), %rsi
movsbl -0x4(%r14), %edx
movl -0x8(%r14), %ecx
movl (%r14), %r8d
movq %rbx, %rdi
xorl %eax, %eax
callq 0x20f0
addq $0x70, %r14
jmp 0x115ac
pushq $0xa
popq %rdi
callq 0x2060
addq $0x1b5d8, %rsp          # imm = 0x1B5D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq