// File: /workspace/github_c_makefile_star2_O2/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
xorl %eax, %eax
cmpq $0x3a98, %rax           # imm = 0x3A98
je 0x2ca1
andl $0x0, 0x30(%rsp,%rax,4)
incq %rax
jmp 0x2c8f
leaq 0xbe6e4(%rbx), %r12
xorl %r13d, %r13d
leaq 0x2c(%rsp), %r14
movslq (%rbx), %rax
cmpq %rax, %r13
jge 0x2da2
movss 0x668a4(%rbx,%r13,4), %xmm0
movss %xmm0, 0x24(%rsp)
movss 0x75304(%rbx,%r13,4), %xmm0
movss %xmm0, 0x20(%rsp)
xorl %ebp, %ebp
xorl %ecx, %ecx
movslq %eax, %rdx
movslq %ecx, %r15
cmpq %rdx, %rbp
jge 0x2d52
movss 0x668a4(%rbx,%rbp,4), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x75304(%rbx,%rbp,4), %xmm0
movss %xmm0, 0x18(%rsp)
cmpq %rbp, %r13
je 0x2d4d
leaq 0x28(%rsp), %rax
movq %rax, (%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x24(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x14(%rsp), %r8
movq %r14, %r9
callq 0x2db4
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x30(%rsp,%r15,4)
incl %r15d
movl (%rbx), %eax
movl %r15d, %ecx
incq %rbp
jmp 0x2ce0
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
pushq $0x4
popq %rdx
leaq -0xf8(%rip), %rcx       # 0x2c6c
callq 0x1050
xorl %eax, %eax
cmpq $0xa, %rax
je 0x2d82
movss 0x30(%rsp,%rax,4), %xmm0
movss %xmm0, (%r12,%rax,4)
incq %rax
jmp 0x2d6b
xorl %eax, %eax
cmpq $0x3a98, %rax           # imm = 0x3A98
je 0x2d96
andl $0x0, 0x30(%rsp,%rax,4)
incq %rax
jmp 0x2d84
incq %r13
addq $0x28, %r12
jmp 0x2cb0
addq $0xea98, %rsp           # imm = 0xEA98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq