// File: /workspace/github_c_makefile_star2_O3/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, %r12
movl %edx, %ebx
movq %rsi, %r15
movq %rdi, %r14
movl %edx, %r13d
testl %edx, %edx
movq %r8, 0x10(%rsp)
jle 0x3d6e4
leaq (,%r13,4), %rdx
movq %r14, %rdi
movl $0xff, %esi
callq 0x2110
movq 0x10(%rsp), %r8
movq %r13, 0x28(%rsp)
testl %r8d, %r8d
jle 0x3d703
movl %r8d, %eax
xorl %ecx, %ecx
movslq (%r12,%rcx,4), %rdx
movl %ecx, (%r14,%rdx,4)
incq %rcx
cmpq %rcx, %rax
jne 0x3d6f3
movl %ebx, 0x1c(%rsp)
testl %ebx, %ebx
jle 0x3d86a
leal -0x1(%r8), %eax
movl %eax, 0xc(%rsp)
cltq
movq %rax, 0x38(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x8(%rsp)
xorl %esi, %esi
movq 0x28(%rsp), %rbx
movq %r12, 0x30(%rsp)
movq %r14, 0x40(%rsp)
cmpl $0x0, (%r14,%rbp,4)
jns 0x3d85c
movl %esi, 0x20(%rsp)
imulq $0x70, %rbp, %r13
movl 0x64(%r15,%r13), %edx
movl 0x8(%rsp), %esi
cmpl 0xc(%rsp), %esi
jge 0x3d78f
movslq %esi, %rcx
leaq (%r12,%rcx,4), %rax
movq 0x38(%rsp), %rsi
subq %rcx, %rsi
xorl %ecx, %ecx
movslq (%rax,%rcx,4), %rdi
imulq $0x70, %rdi, %rdi
cmpl %edx, 0x64(%r15,%rdi)
jge 0x3d789
incq %rcx
cmpq %rcx, %rsi
jne 0x3d76c
movl 0xc(%rsp), %esi
jmp 0x3d78f
movl 0x8(%rsp), %esi
addl %ecx, %esi
addq %r15, %r13
movslq %esi, %rax
movslq (%r12,%rax,4), %rcx
imulq $0x70, %rcx, %rcx
cmpl %edx, 0x64(%r15,%rcx)
jne 0x3d887
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movl %esi, 0x8(%rsp)
cmpl %r8d, %esi
jge 0x3d829
leaq (%r12,%rax,4), %r12
movl 0x8(%rsp), %ebx
movss 0x19ad9(%rip), %xmm0    # 0x572a0
movslq (%r12), %rax
imulq $0x70, %rax, %rsi
movl 0x64(%r15,%rsi), %eax
cmpl 0x64(%r13), %eax
jne 0x3d80f
addq %r15, %rsi
movq %r13, %rdi
movss %xmm0, 0x24(%rsp)
callq 0x141ea
movss 0x24(%rsp), %xmm1
ucomiss %xmm0, %xmm1
seta %al
testl %r14d, %r14d
sets %cl
orb %al, %cl
testb %cl, %cl
jne 0x3d806
movaps %xmm1, %xmm0
cmovnel %ebx, %r14d
movq 0x10(%rsp), %r8
addq $0x4, %r12
incl %ebx
cmpl %ebx, %r8d
jne 0x3d7c7
testl %r14d, %r14d
movq 0x30(%rsp), %r12
movq 0x28(%rsp), %rbx
jns 0x3d84a
movsbl 0x68(%r13), %ecx
movl 0x64(%r13), %r8d
leaq 0x1eee2(%rip), %rdi     # 0x5c71b
movl %r14d, %esi
movl %ebp, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %r8
movl 0x20(%rsp), %esi
incl %esi
movq 0x40(%rsp), %rax
movl %r14d, (%rax,%rbp,4)
movq %rax, %r14
incq %rbp
cmpq %rbx, %rbp
jne 0x3d739
jmp 0x3d86c
xorl %esi, %esi
addl %esi, %r8d
movl 0x1c(%rsp), %ecx
cmpl %ecx, %r8d
jne 0x3d8a4
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsbl 0x68(%r13), %esi
leaq 0x1ee64(%rip), %rdi     # 0x5c6f7
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2280
leaq 0x1eeac(%rip), %rdi     # 0x5c757
movl %r8d, %edx
xorl %eax, %eax
callq 0x20f0
jmp 0x3d89a