// File: /workspace/github_c_makefile_star2_O3/usgs[P]nshmp-haz-catalogs/src/agrid/adaptive/get_eq_neighbor_dists.c
// Function: get_eq_neighbor_dists
void get_eq_neighbor_dists(struct eqcat* catn)
/*--------------------------------------------------------------------------*/
{
//  int cnt, cnt1, cnt2, ncnt, narr;
  int cnt, cnt1, cnt2, ncnt;
//  int ord[NEQ];
  float lon1, lat1, lon2, lat2, dist, az, baz;
  float distarr[NEQ];

// sorting array has one less element than earthquake list array
//  narr=catn->nv-1;
  for(cnt=0; cnt<NEQ; cnt++) distarr[cnt]=0.0;

// loop over all pairs of stations, get distances and sort for the 10-nearest events
//  fprintf(stderr,"nv=%d\n", catn->nv);
  for(cnt1=0; cnt1<catn->nv; cnt1++) {
    lon1=catn->lon[cnt1];
    lat1=catn->lat[cnt1];
    ncnt=0;
    for(cnt2=0; cnt2<catn->nv; cnt2++) {
      lon2=catn->lon[cnt2];
      lat2=catn->lat[cnt2];
      if ( (cnt1>cnt2) || (cnt1<cnt2) ) {
        delaz_(&lat1,&lon1,&lat2,&lon2,&dist,&az,&baz);
        distarr[ncnt]=dist;
//        fprintf(stderr,"dist/coords: %f %.2f %.2f %.2f %.2f %d %d\n", dist, lon1, lat1, lon2, lat2, cnt1, cnt2);
        ncnt++;
      }
    }
//    for(cnt2=0; cnt2<ncnt; cnt2++) fprintf(stderr,"dist: %f\n", distarr[cnt2]);

// sort distances in distarr array
    qsort(distarr,ncnt,sizeof(float),compare_floats);
//    for(cnt2=0; cnt2<ncnt; cnt2++) fprintf(stderr,"dist_sort: %f\n", distarr[cnt2]);

    for(cnt=0;cnt<(int)NNUM; cnt++) {
      catn->dist_nbr[cnt1][cnt]=distarr[cnt];
//      fprintf(stderr,"dist_nbr: %f %f\n", distarr[cnt], catn->dist_nbr[cnt1][cnt]);
    }
//    fprintf(stderr,"REMOVE LATER\n"); exit(1);
    for(cnt=0; cnt<NEQ; cnt++) distarr[cnt]=0.0;
  }

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xea98, %rsp           # imm = 0xEA98
movq %rdi, %rbx
leaq 0x30(%rsp), %rdi
movl $0xea60, %edx           # imm = 0xEA60
xorl %esi, %esi
callq 0x10b0
movl (%rbx), %eax
testl %eax, %eax
jle 0x3562
leaq 0xbe6e4(%rbx), %rcx
movq %rcx, 0x20(%rsp)
xorl %r15d, %r15d
leaq 0xc(%rsp), %rbp
leaq 0x2c(%rsp), %r14
movss 0x668a4(%rbx,%r15,4), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x75304(%rbx,%r15,4), %xmm0
movss %xmm0, 0x18(%rsp)
testl %eax, %eax
jle 0x34fd
xorl %r12d, %r12d
xorl %r13d, %r13d
movss 0x668a4(%rbx,%r12,4), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x75304(%rbx,%r12,4), %xmm0
movss %xmm0, 0x10(%rsp)
cmpq %r12, %r15
je 0x34ed
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
leaq 0x18(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x10(%rsp), %rdx
leaq 0x14(%rsp), %rcx
movq %rbp, %r8
movq %r14, %r9
callq 0x3574
movss 0xc(%rsp), %xmm0
movslq %r13d, %r13
movss %xmm0, 0x30(%rsp,%r13,4)
incl %r13d
movl (%rbx), %eax
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x348b
movslq %r13d, %rsi
jmp 0x34ff
xorl %esi, %esi
leaq (%r15,%r15,4), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
movl $0x4, %edx
leaq 0x30(%rsp), %r13
movq %r13, %rdi
leaq -0x114(%rip), %rcx      # 0x340c
callq 0x1050
movq 0x50(%rsp), %rax
movq %rax, 0x20(%r12)
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movups %xmm1, 0x10(%r12)
movups %xmm0, (%r12)
movl $0xea60, %edx           # imm = 0xEA60
movq %r13, %rdi
xorl %esi, %esi
callq 0x10b0
incq %r15
movslq (%rbx), %rax
cmpq %rax, %r15
jl 0x3461
addq $0xea98, %rsp           # imm = 0xEA98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq