// File: /workspace/github_c_makefile_star2_O3/usgs[P]nshmp-haz-catalogs/src/agrid/adaptive/get_eqs_fm_catalog.c
// Function: get_eqs_fm_catalog
int get_eqs_fm_catalog(char *fnm, struct eqcat* catn, struct compl_levels* compl, struct boundaries* reg, struct rates* seisr)
/*--------------------------------------------------------------------------*/
{
  FILE *fpcat;
  int cnt=0, cnt1, lineno=1;
//  int ix, iy, n;
  int yr, mon, day, hr, min;
  float mag, lon, lat, dep, sec, mag_adj, msig, neff,mconv;
  char buff[BUFFLEN];

// open file
  if( (fpcat = fopen(fnm, "r"))==NULL) {
    fprintf(stderr,"Earthquake catalog file not found, %s\n", fnm);
    return 0;
  }
  
// loop through catalog and select earthquakes that meet magnitude completeness thresholds and
// occur within the region
  fprintf(stderr,"Regional boundaries for eqs: min/max lon, lat: %d %d\n", reg->nsy, reg->nsx);
  while( fgets(buff,BUFFLEN,fpcat) ) {
//    sscanf(buff,"%f %f %f %f %d %d %d %d %d %f %f %f", &mag, &lon, &lat, &dep, &yr, &mon, &day, &hr, &min, &sec, &msig, &neff);
    sscanf(buff,"%f %f %f %f %d %d %d %d %d %f %f %f %f", &mconv, &lon, &lat, &dep, &yr, &mon, &day, &hr, &min, &sec, &msig, &mag, &neff);
    if ( (lon>=reg->minlon) && (lon<=reg->maxlon) &&  (lat>=reg->minlat) && (lat<=reg->maxlat) ) {
      for(cnt1=0; cnt1<compl->nlevels; cnt1++) {
//        fprintf(stderr,"mag/yr completeness level: %f %d\n", compl->mag_comp[cnt1], compl->yr_comp[cnt1]);
//        fprintf(stderr,"mag/yr completeness level: %f %d %d\n", compl->mag_comp[cnt1], compl->yr_comp[cnt1], compl->end_yr);
        if (compl->nlevels>1) fprintf(stderr,"There may be a problem with selection of earthquakes from catalog for multiple completeness levels.\n");
        mag_adj=compl->mag_comp[cnt1]-compl->dMag/2;
        if ( (mag>=mag_adj) && (yr>=compl->yr_comp[cnt1]) && (yr<=compl->end_yr) ) {
          catn->mag[cnt]=mag;
          catn->lon[cnt]=lon;
          catn->lat[cnt]=lat;
          catn->depth[cnt]=dep;
          catn->year[cnt]=yr;
          catn->month[cnt]=mon;
          catn->day[cnt]=day;
          catn->lineno[cnt]=lineno;
          catn->mag_sigma[cnt]=msig;
          catn->neff[cnt]=neff;
//          fprintf(stderr,"sel: %f %f %f %d %d %d %d\n", mag, lon, lat, yr, mon, day, lineno);
//          fprintf(stderr,"sel: %f %f %f %d %d %d %d\n", catn->mag[cnt], catn->lon[cnt], catn->lat[cnt], catn->year[cnt], catn->month[cnt], catn->day[cnt], catn->lineno[cnt]);
//          iy=(int)((reg->maxlat-lat)/reg->dlat);
//          ix=(int)((lon-reg->minlon)/reg->dlon);
//          n=iy*reg->nsx+ix+1;
//          seisr->asum_cnt[n]++;
          cnt++;
        }
      }
    }
    lineno++;
  }
  catn->nv=cnt;
  fclose(fpcat);

// check earthquake numbers fit within arrays
  if (catn->nv>NEQ) {
    fprintf(stderr,"Number of earthquakes in catalog (%d) - %s - greater than array size, NEQ (%d).", catn->nv, fnm, (int)NEQ);
    fprintf(stderr,"Modify NEQ and re-start code.\n");
    exit(1);
  }
  else {
    fprintf(stderr,"Selected %d earthquakes from catalog, %s (%d events, total)\n", catn->nv, fnm, lineno);
  }
  
  if ( (compl->variableCompleteness) == 1 ) {
    if ( (compl->nlevels) > 1 ) {
      fprintf(stderr,"ERROR: Code doesn't currently allow for variable completeness and multiple completeness levels.\n");
      exit(1);
    }
    assign_variable_completeness_eqs(catn, compl);
  }

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
leaq 0x377b(%rip), %rsi      # 0x68cb
callq 0x1160
movq %rax, %rbp
movq 0x6e99(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
testq %rbp, %rbp
jne 0x317f
leaq 0x3c4b(%rip), %rsi      # 0x6db9
xorl %ebp, %ebp
movq %r15, %rdx
xorl %eax, %eax
callq 0x1100
jmp 0x33d2
movq %r15, 0x30(%rsp)
movl (%r12), %ecx
movl 0x4(%r12), %edx
leaq 0x3c4c(%rip), %rsi      # 0x6de0
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x1100
leaq 0x50(%rsp), %rdi
movl $0x12c, %esi            # imm = 0x12C
movq %rbp, 0x38(%rsp)
movq %rbp, %rdx
callq 0x10e0
movl $0x1, %ebp
testq %rax, %rax
je 0x3390
leaq 0x50(%rsp), %r15
xorl %r13d, %r13d
subq $0x8, %rsp
movq %r15, %rdi
leaq 0x3c3d(%rip), %rsi      # 0x6e16
leaq 0x48(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x24(%rsp), %r9
xorl %eax, %eax
leaq 0x1c(%rsp), %r10
pushq %r10
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x64(%rsp), %r10
pushq %r10
leaq 0x70(%rsp), %r10
pushq %r10
leaq 0x7c(%rsp), %r10
pushq %r10
leaq 0x5c(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
leaq 0x74(%rsp), %r10
pushq %r10
callq 0x1130
addq $0x50, %rsp
movss 0x10(%rsp), %xmm0
ucomiss 0xc(%r12), %xmm0
jb 0x336e
movss 0x10(%r12), %xmm1
ucomiss %xmm0, %xmm1
jb 0x336e
movss 0xc(%rsp), %xmm0
ucomiss 0x14(%r12), %xmm0
jb 0x336e
movss 0x18(%r12), %xmm1
ucomiss %xmm0, %xmm1
jb 0x336e
movl 0xc(%rbx), %eax
testl %eax, %eax
jle 0x336e
xorl %r15d, %r15d
cmpl $0x2, %eax
jge 0x3364
movss 0x64(%rbx), %xmm1
mulss 0x3a85(%rip), %xmm1     # 0x6d24
addss 0x3c(%rbx,%r15,4), %xmm1
movss 0x20(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x3352
movl 0x2c(%rsp), %eax
cmpl 0x14(%rbx,%r15,4), %eax
jl 0x3352
cmpl 0x10(%rbx), %eax
jg 0x3352
movslq %r13d, %r13
movss %xmm0, 0x57e44(%r14,%r13,4)
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x668a4(%r14,%r13,4)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x75304(%r14,%r13,4)
movss 0x1c(%rsp), %xmm0
movss %xmm0, 0x83d64(%r14,%r13,4)
movl %eax, 0x4(%r14,%r13,4)
movl 0x28(%rsp), %eax
movl %eax, 0xea64(%r14,%r13,4)
movl 0x24(%rsp), %eax
movl %eax, 0x1d4c4(%r14,%r13,4)
movl %ebp, 0x3a984(%r14,%r13,4)
movss 0x18(%rsp), %xmm0
movss %xmm0, 0xa1224(%r14,%r13,4)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x927c4(%r14,%r13,4)
incl %r13d
incq %r15
movslq 0xc(%rbx), %rax
cmpq %rax, %r15
jl 0x3289
jmp 0x336e
callq 0x17a0
jmp 0x3292
incl %ebp
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movl $0x12c, %esi            # imm = 0x12C
movq 0x38(%rsp), %rdx
callq 0x10e0
testq %rax, %rax
jne 0x31cb
movl %r13d, (%r14)
movq 0x38(%rsp), %rdi
callq 0x1080
movl (%r14), %edx
movq 0x6c51(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
cmpl $0x3a99, %edx           # imm = 0x3A99
jge 0x33f9
leaq 0x3b58(%rip), %rsi      # 0x6f11
movq 0x30(%rsp), %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x1100
movl $0x1, %ebp
cmpl $0x1, (%rbx)
je 0x33e6
movl %ebp, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x2, 0xc(%rbx)
jge 0x3405
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14fd
jmp 0x33d2
movl %edx, %esi
movq 0x30(%rsp), %rdx
callq 0x17e7
callq 0x17c2
nop