// File: /workspace/github_c_makefile_star2_O1/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
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r12
movl %edx, %r13d
testl %edx, %edx
movq %r8, 0x10(%rsp)
jle 0x3d71a
leaq (,%r13,4), %rdx
movq %r12, %rdi
movl $0xff, %esi
callq 0x2110
movq 0x10(%rsp), %r8
testl %r8d, %r8d
jle 0x3d734
movl %r8d, %eax
xorl %ecx, %ecx
movslq (%r14,%rcx,4), %rdx
movl %ecx, (%r12,%rdx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x3d724
movl %ebx, 0x1c(%rsp)
testl %ebx, %ebx
jle 0x3d89c
leal -0x1(%r8), %eax
movl %eax, 0x8(%rsp)
cltq
movq %rax, 0x30(%rsp)
xorl %ebp, %ebp
xorl %r10d, %r10d
xorl %esi, %esi
movq %r14, 0x28(%rsp)
movq %r12, 0x40(%rsp)
movq %r13, 0x38(%rsp)
cmpl $0x0, (%r12,%rbp,4)
jns 0x3d88e
imulq $0x70, %rbp, %r13
addq %r15, %r13
cmpl 0x8(%rsp), %r10d
movl %esi, 0x20(%rsp)
jge 0x3d7b8
movl 0x64(%r13), %eax
movslq %r10d, %rdx
leaq (%r14,%rdx,4), %rcx
movq 0x30(%rsp), %r9
subq %rdx, %r9
xorl %edx, %edx
movslq (%rcx,%rdx,4), %rdi
imulq $0x70, %rdi, %rdi
cmpl %eax, 0x64(%r15,%rdi)
jge 0x3d7b5
incq %rdx
cmpq %rdx, %r9
jne 0x3d797
movl 0x8(%rsp), %r10d
jmp 0x3d7b8
addl %edx, %r10d
movslq %r10d, %rax
movslq (%r14,%rax,4), %rcx
imulq $0x70, %rcx, %rcx
movl 0x64(%r13), %edx
cmpl %edx, 0x64(%r15,%rcx)
jne 0x3d8b9
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %r10d, 0xc(%rsp)
cmpl %r8d, %r10d
jge 0x3d84a
movq 0x28(%rsp), %rcx
leaq (%rcx,%rax,4), %r12
movl 0xc(%rsp), %ebx
movss 0x19ae9(%rip), %xmm0    # 0x572e0
movslq (%r12), %rax
imulq $0x70, %rax, %rsi
movl 0x64(%r15,%rsi), %eax
cmpl 0x64(%r13), %eax
jne 0x3d83f
addq %r15, %rsi
movq %r13, %rdi
movss %xmm0, 0x24(%rsp)
callq 0x144c2
movss 0x24(%rsp), %xmm1
ucomiss %xmm0, %xmm1
seta %al
testl %r14d, %r14d
sets %cl
orb %al, %cl
testb %cl, %cl
jne 0x3d836
movaps %xmm1, %xmm0
cmovnel %ebx, %r14d
movq 0x10(%rsp), %r8
addq $0x4, %r12
incl %ebx
cmpl %ebx, %r8d
jne 0x3d7f7
testl %r14d, %r14d
jns 0x3d870
movsbl 0x68(%r13), %ecx
movl 0x64(%r13), %r8d
leaq 0x1eefc(%rip), %rdi     # 0x5c75b
movl %r14d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %r8
movl 0x20(%rsp), %esi
incl %esi
movq 0x40(%rsp), %r12
movl %r14d, (%r12,%rbp,4)
movq 0x28(%rsp), %r14
movq 0x38(%rsp), %r13
movl 0xc(%rsp), %r10d
incq %rbp
cmpq %r13, %rbp
jne 0x3d765
jmp 0x3d89e
xorl %esi, %esi
addl %esi, %r8d
movl 0x1c(%rsp), %ecx
cmpl %ecx, %r8d
jne 0x3d8d6
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbl 0x68(%r13), %esi
leaq 0x1ee72(%rip), %rdi     # 0x5c737
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2270
leaq 0x1eeba(%rip), %rdi     # 0x5c797
movl %r8d, %edx
xorl %eax, %eax
callq 0x20f0
jmp 0x3d8cc