// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/mutation.c
// Function: Match_atoms
void  Match_atoms(int *ref_atom, atom *atoms, int natoms,
		  int *atom_ref, int N_ref)
{
  /* For every atom i=1,natoms ref_atom[i] is the index of the reference
     atom that is closest to i */
  int i; for(i=0; i<natoms; i++)ref_atom[i]=-1;
  for(i=0; i<N_ref; i++)ref_atom[atom_ref[i]]=i;
  int Nr1=N_ref-1, ini_r=0, n=0;
  for(i=0; i<natoms; i++){
    if(ref_atom[i]>=0)continue;
    n++;
    atom *atom=atoms+i, *atom2;
    while((ini_r<Nr1)&&(atoms[atom_ref[ini_r]].res<atom->res))ini_r++;
    if(atoms[atom_ref[ini_r]].res != atom->res){
      printf("ERROR unmatch_atom in residue %c%d\n", atom->aa, atom->res);
      exit(8);
    }
    float d2_min=1000; int ir=ini_r, ir_min=-1;
    while(ir<N_ref){
      atom2=atoms+atom_ref[ir];
      if(atom2->res==atom->res){
	float d2=Distance_square(atom->r, atom2->r);
	if((d2<d2_min)||(ir_min<0)){d2_min=d2; ir_min=ir;}
      }
      ir++;
    }
    if(ir_min<0){
      printf("ERROR, reference atom %d in atom %d residue %c%d not valid\n",
	     ir_min, i, atom->aa, atom->res);
    }
    ref_atom[i]=ir_min;
  }
  if((N_ref+n)!=natoms){
    printf("ERROR, N_ref+%d=%d atoms matched over %d\n",
	   n, N_ref+n, natoms); exit(8);
  }
  if(0){
    for(i=0; i<natoms; i++){
      atom *atom=atoms+i;
      printf("%d %s(%c%d) ", i, atom->name, atom->aa, atom->res);
      int j=atom_ref[ref_atom[i]]; atom=atoms+j;
      printf(" ref: %d  %s(%c%d)\n", j, atom->name, atom->aa, atom->res);
    }
    exit(8);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r14
movq %rsi, %r15
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %r9d
cmovgl %edx, %r9d
cmpq %rax, %r9
je 0x38660
orl $-0x1, (%rdi,%rax,4)
incq %rax
jmp 0x38652
xorl %eax, %eax
testl %r8d, %r8d
movl $0x0, %ecx
cmovgl %r8d, %ecx
cmpq %rax, %rcx
je 0x3867f
movslq (%r14,%rax,4), %rsi
movl %eax, (%rdi,%rsi,4)
incq %rax
jmp 0x3866e
movl %edx, 0xc(%rsp)
leal -0x1(%r8), %eax
movl %eax, 0x10(%rsp)
movslq %eax, %r10
movq %r8, 0x20(%rsp)
movslq %r8d, %rbp
xorl %r12d, %r12d
xorl %esi, %esi
xorl %eax, %eax
movq %rdi, 0x38(%rsp)
movq %r9, 0x30(%rsp)
movq %r10, 0x28(%rsp)
cmpq %r9, %r12
je 0x387bb
cmpl $0x0, (%rdi,%r12,4)
jns 0x387b3
incl %esi
imulq $0x70, %r12, %rcx
leaq (%r15,%rcx), %rbx
cltq
movl 0x10(%rsp), %edx
cmpl %edx, %eax
movl %edx, %edi
cmovgl %eax, %edi
movl 0x64(%r15,%rcx), %edx
movl %esi, 0x18(%rsp)
movq %r12, 0x40(%rsp)
cmpq %r10, %rax
jge 0x38700
movslq (%r14,%rax,4), %rcx
imulq $0x70, %rcx, %rcx
cmpl %edx, 0x64(%r15,%rcx)
jge 0x386fe
incq %rax
jmp 0x386e5
movl %eax, %edi
movl %edi, 0x14(%rsp)
movslq %edi, %r12
movslq (%r14,%r12,4), %rax
imulq $0x70, %rax, %rax
cmpl %edx, 0x64(%r15,%rax)
jne 0x387d9
movss 0x18ace(%rip), %xmm0    # 0x511f0
pushq $-0x1
popq %r13
cmpq %rbp, %r12
jge 0x38772
movslq (%r14,%r12,4), %rax
imulq $0x70, %rax, %rsi
movl 0x64(%r15,%rsi), %eax
cmpl 0x64(%rbx), %eax
jne 0x3876d
addq %r15, %rsi
movq %rbx, %rdi
movss %xmm0, 0x1c(%rsp)
callq 0x12f7b
movss 0x1c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
seta %al
testl %r13d, %r13d
sets %cl
orb %al, %cl
testb %cl, %cl
jne 0x38769
movaps %xmm1, %xmm0
cmovnel %r12d, %r13d
incq %r12
jmp 0x38726
testl %r13d, %r13d
movq 0x40(%rsp), %r12
jns 0x38798
movsbl 0x68(%rbx), %ecx
movl 0x64(%rbx), %r8d
leaq 0x1def8(%rip), %rdi     # 0x56683
movl %r13d, %esi
movl %r12d, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x38(%rsp), %rdi
movl %r13d, (%rdi,%r12,4)
movq 0x30(%rsp), %r9
movl 0x18(%rsp), %esi
movq 0x28(%rsp), %r10
movl 0x14(%rsp), %eax
incq %r12
jmp 0x386ac
movq 0x20(%rsp), %rdx
addl %esi, %edx
movl 0xc(%rsp), %ecx
cmpl %ecx, %edx
jne 0x387f3
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbl 0x68(%rbx), %esi
leaq 0x1de7b(%rip), %rdi     # 0x5665f
xorl %eax, %eax
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270
leaq 0x1dec5(%rip), %rdi     # 0x566bf
xorl %eax, %eax
callq 0x20f0
jmp 0x387eb