// File: /workspace/github_c_makefile_star2_O0/ugobas[P]tnm/align_tnm.c
// Function: Align_atoms
int Align_atoms(atom *atoms1, atom *atoms2,          // Output
		struct chain *ch1, struct chain *ch2) // Input
{
  int ini1=ch1->ini_atom, natoms1=ch1->natoms, ini_res1=ch1->ini_res;
  int ini2=ch2->ini_atom, natoms2=ch2->natoms, ini_res2=ch2->ini_res;
  int *alignres=ch1->alignres;
  int n=0, i, i1, i2max=ini2+natoms2;
  printf("Aligning atoms in chain %d %c %d residues and %d %c %d residues\n",
	 ch1->match,ch1->label,ch1->nres, ch2->match,ch2->label,ch2->nres);

  if(0){
    for(i1=0; i1<ch1->nres; i1++){
      printf("%c ", ch1->seq[i1]);
      int i2=ch1->alignres[i1];
      if(i2>=0){printf("%c\n", ch2->seq[i2]);}
      else{printf("-\n");}
    }
    printf("chains %c %c\n", ch1->label, ch2->label);
    exit(8);
  }

  // Initialize
  for(i=ini1; i<(ini1+natoms1); i++)atoms1[i].ali=-1;
  for(i=ini2; i<(ini2+natoms2); i++)atoms2[i].ali=-1;
  // Look if aligned
  int ares2=ini2;
  atom *atom1;
  for(i1=ini1; i1<(ini1+natoms1); i1++){
    atom1=atoms1+i1;
    int res2=alignres[atom1->res-ini_res1];
    if(res2<0){continue;} res2+=ini_res2;
    int iali=-1, i2=ares2;
    atom *atom2=atoms2+ares2; ;
    while((atom2->res<res2)&&(i2<i2max)){atom2++; i2++;}
    ares2=i2;
    while((i2<i2max)&&(atom2->res==res2)){
      if((atom2->ali<0)&&(atom2->name[1]==atom1->name[1])){
	if((atom2->name[0]==atom1->name[0])&&
	   (atom2->name[2]==atom1->name[2])){
	  atom1->ali=i2; atom2->ali=i1; n++; break; // Same name
	}else if(iali<0){
	  iali=i2; // Same side-chain index
	}
      }
      i2++; atom2++;
    }
    if(atom1->ali>=0)continue;
    // For main chain atoms we require that the name coincides exactly
    // For side chain atoms, the same side-chain index is enough
    atom1->main=Is_atom_main(atom1->name, ch1->type);
    if((atom1->main==0)&&(iali>=0)){
      atom1->ali=iali; (atoms2+iali)->ali=i1; n++; continue;
    }
    // Not aligned atom
    if(iali>=0){atom2=atoms2+iali;}
    else{atom2=atoms2+ares2;}
    printf("Not aligned: 1: %s%d%c %c 2: %s%d%c %c",
	   atom1->name, atom1->res, atom1->aa, ch1->label,
	   atom2->name, atom2->res, atom2->aa, ch2->label);
    printf(" Aligned res: %d iali: %d\n", ares2, iali);
  }

  if(DEBUG){
    printf("%d Aligned atoms:\n", n);
    for(i=ini1; i<(ini1+30); i++){
      atom *atom1=atoms1+i;
      if(atom1->ali<0)continue;
      atom *atom2=atoms2+atom1->ali;
      printf("%s %3d %d - %s %3d %d\n",atom1->name,atom1->res,i,
	     atom2->name,atom2->res, atom1->ali);
    }
    //exit(8);
    }
  return(n);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x30(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movl 0x40(%rax), %esi
movq -0x18(%rbp), %rax
movsbl 0x34(%rax), %edx
movq -0x18(%rbp), %rax
movl 0x1c(%rax), %ecx
movq -0x20(%rbp), %rax
movl 0x40(%rax), %r8d
movq -0x20(%rbp), %rax
movsbl 0x34(%rax), %r9d
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
leaq 0x63206(%rip), %rdi     # 0x7b4ec
movl %eax, (%rsp)
movb $0x0, %al
callq 0x10d0
movl -0x24(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl -0x24(%rbp), %ecx
addl -0x28(%rbp), %ecx
cmpl %ecx, %eax
jge 0x18324
movq -0x8(%rbp), %rax
movslq -0x48(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl $0xffffffff, 0x54(%rax) # imm = 0xFFFFFFFF
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x182f6
movl -0x30(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl -0x30(%rbp), %ecx
addl -0x34(%rbp), %ecx
cmpl %ecx, %eax
jge 0x18358
movq -0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl $0xffffffff, 0x54(%rax) # imm = 0xFFFFFFFF
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x1832a
movl -0x30(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl -0x24(%rbp), %ecx
addl -0x28(%rbp), %ecx
cmpl %ecx, %eax
jge 0x185e3
movq -0x8(%rbp), %rax
movslq -0x4c(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
movl 0x64(%rcx), %ecx
subl -0x2c(%rbp), %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x64(%rbp)
cmpl $0x0, -0x64(%rbp)
jge 0x183aa
jmp 0x185d5
movl -0x38(%rbp), %eax
addl -0x64(%rbp), %eax
movl %eax, -0x64(%rbp)
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl -0x54(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movl 0x64(%rax), %ecx
xorl %eax, %eax
cmpl -0x64(%rbp), %ecx
movb %al, -0x79(%rbp)
jge 0x183f0
movl -0x6c(%rbp), %eax
cmpl -0x50(%rbp), %eax
setl %al
movb %al, -0x79(%rbp)
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x183f9
jmp 0x18410
movq -0x78(%rbp), %rax
addq $0x70, %rax
movq %rax, -0x78(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x183d3
movl -0x6c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x6c(%rbp), %ecx
xorl %eax, %eax
cmpl -0x50(%rbp), %ecx
movb %al, -0x7a(%rbp)
jge 0x18433
movq -0x78(%rbp), %rax
movl 0x64(%rax), %eax
cmpl -0x64(%rbp), %eax
sete %al
movb %al, -0x7a(%rbp)
movb -0x7a(%rbp), %al
testb $0x1, %al
jne 0x1843f
jmp 0x184ce
movq -0x78(%rbp), %rax
cmpl $0x0, 0x54(%rax)
jge 0x184b4
movq -0x78(%rbp), %rax
movsbl 0x19(%rax), %eax
movq -0x60(%rbp), %rcx
movsbl 0x19(%rcx), %ecx
cmpl %ecx, %eax
jne 0x184b4
movq -0x78(%rbp), %rax
movsbl 0x18(%rax), %eax
movq -0x60(%rbp), %rcx
movsbl 0x18(%rcx), %ecx
cmpl %ecx, %eax
jne 0x184a4
movq -0x78(%rbp), %rax
movsbl 0x1a(%rax), %eax
movq -0x60(%rbp), %rcx
movsbl 0x1a(%rcx), %ecx
cmpl %ecx, %eax
jne 0x184a4
movl -0x6c(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x54(%rax)
movl -0x4c(%rbp), %ecx
movq -0x78(%rbp), %rax
movl %ecx, 0x54(%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x184ce
cmpl $0x0, -0x68(%rbp)
jge 0x184b0
movl -0x6c(%rbp), %eax
movl %eax, -0x68(%rbp)
jmp 0x184b2
jmp 0x184b4
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
movq -0x78(%rbp), %rax
addq $0x70, %rax
movq %rax, -0x78(%rbp)
jmp 0x18416
movq -0x60(%rbp), %rax
cmpl $0x0, 0x54(%rax)
jl 0x184dd
jmp 0x185d5
movq -0x60(%rbp), %rdi
addq $0x18, %rdi
movq -0x18(%rbp), %rax
movl 0x28(%rax), %esi
callq 0x1d050
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x60(%rbp), %rax
cmpl $0x0, 0x48(%rax)
jne 0x18537
cmpl $0x0, -0x68(%rbp)
jl 0x18537
movl -0x68(%rbp), %ecx
movq -0x60(%rbp), %rax
movl %ecx, 0x54(%rax)
movl -0x4c(%rbp), %ecx
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rdx
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x54(%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x185d5
cmpl $0x0, -0x68(%rbp)
jl 0x18552
movq -0x10(%rbp), %rax
movslq -0x68(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
jmp 0x18565
movq -0x10(%rbp), %rax
movslq -0x54(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq -0x60(%rbp), %rsi
addq $0x18, %rsi
movq -0x60(%rbp), %rax
movl 0x64(%rax), %edx
movq -0x60(%rbp), %rax
movsbl 0x68(%rax), %ecx
movq -0x18(%rbp), %rax
movsbl 0x34(%rax), %r8d
movq -0x78(%rbp), %r9
addq $0x18, %r9
movq -0x78(%rbp), %rax
movl 0x64(%rax), %r11d
movq -0x78(%rbp), %rax
movsbl 0x68(%rax), %r10d
movq -0x20(%rbp), %rax
movsbl 0x34(%rax), %eax
leaq 0x62f80(%rip), %rdi     # 0x7b52d
movl %r11d, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movb $0x0, %al
callq 0x10d0
movl -0x54(%rbp), %esi
movl -0x68(%rbp), %edx
leaq 0x62f86(%rip), %rdi     # 0x7b554
movb $0x0, %al
callq 0x10d0
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x18364
movl -0x44(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nop