// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x1, -0x6c(%rbp)
movq -0x38(%rbp), %rdi
leaq 0x57a6(%rip), %rsi      # 0x88b7
callq 0x1130
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
jne 0x3148
movq 0x8ed1(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x38(%rbp), %rdx
leaq 0x5c70(%rip), %rsi      # 0x8da5
movb $0x0, %al
callq 0x10e0
movl $0x0, -0x2c(%rbp)
jmp 0x3512
movq 0x8ea9(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x50(%rbp), %rax
movl (%rax), %ecx
leaq 0x5c66(%rip), %rsi      # 0x8dcc
movb $0x0, %al
callq 0x10e0
leaq -0x1d0(%rbp), %rdi
movq -0x60(%rbp), %rdx
movl $0x12c, %esi            # imm = 0x12C
callq 0x10c0
cmpq $0x0, %rax
je 0x343b
leaq -0x1d0(%rbp), %rdi
leaq 0x5c68(%rip), %rsi      # 0x8e02
leaq -0xa4(%rbp), %rdx
leaq -0x88(%rbp), %rcx
leaq -0x8c(%rbp), %r8
leaq -0x90(%rbp), %r9
leaq -0x70(%rbp), %rax
movq %rax, -0x1e0(%rbp)
leaq -0x74(%rbp), %r13
leaq -0x78(%rbp), %r12
leaq -0x7c(%rbp), %r15
leaq -0x80(%rbp), %r14
leaq -0x94(%rbp), %rbx
leaq -0x9c(%rbp), %r11
leaq -0x84(%rbp), %r10
leaq -0xa0(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x1e0(%rbp), %rax
movq %rax, (%rsp)
movq -0x1d8(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movb $0x0, %al
callq 0x1100
movss -0x88(%rbp), %xmm0
movq -0x50(%rbp), %rax
ucomiss 0xc(%rax), %xmm0
jb 0x342d
movss -0x88(%rbp), %xmm1
movq -0x50(%rbp), %rax
movss 0x10(%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x342d
movss -0x8c(%rbp), %xmm0
movq -0x50(%rbp), %rax
ucomiss 0x14(%rax), %xmm0
jb 0x342d
movss -0x8c(%rbp), %xmm1
movq -0x50(%rbp), %rax
movss 0x18(%rax), %xmm0
ucomiss %xmm1, %xmm0
jb 0x342d
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x342b
movq -0x48(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jle 0x32ce
movq 0x8d3b(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x5b62(%rip), %rsi      # 0x8e29
movb $0x0, %al
callq 0x10e0
movq -0x48(%rbp), %rax
movslq -0x68(%rbp), %rcx
movss 0x3c(%rax,%rcx,4), %xmm0
movq -0x48(%rbp), %rax
movss 0x64(%rax), %xmm1
movss 0x5a23(%rip), %xmm2     # 0x8d10
divss %xmm2, %xmm1
subss %xmm1, %xmm0
movss %xmm0, -0x98(%rbp)
movss -0x84(%rbp), %xmm0
ucomiss -0x98(%rbp), %xmm0
jb 0x341b
movl -0x70(%rbp), %eax
movq -0x48(%rbp), %rcx
movslq -0x68(%rbp), %rdx
cmpl 0x14(%rcx,%rdx,4), %eax
jl 0x341b
movl -0x70(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jg 0x341b
movss -0x84(%rbp), %xmm0
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movss %xmm0, 0x57e44(%rax,%rcx,4)
movss -0x88(%rbp), %xmm0
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movss %xmm0, 0x668a4(%rax,%rcx,4)
movss -0x8c(%rbp), %xmm0
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movss %xmm0, 0x75304(%rax,%rcx,4)
movss -0x90(%rbp), %xmm0
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movss %xmm0, 0x83d64(%rax,%rcx,4)
movl -0x70(%rbp), %edx
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl %edx, 0x4(%rax,%rcx,4)
movl -0x74(%rbp), %edx
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl %edx, 0xea64(%rax,%rcx,4)
movl -0x78(%rbp), %edx
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl %edx, 0x1d4c4(%rax,%rcx,4)
movl -0x6c(%rbp), %edx
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl %edx, 0x3a984(%rax,%rcx,4)
movss -0x9c(%rbp), %xmm0
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movss %xmm0, 0xa1224(%rax,%rcx,4)
movss -0xa0(%rbp), %xmm0
movq -0x40(%rbp), %rax
movslq -0x64(%rbp), %rcx
movss %xmm0, 0x927c4(%rax,%rcx,4)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x341d
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x329c
jmp 0x342d
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x316d
movl -0x64(%rbp), %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movq -0x60(%rbp), %rdi
callq 0x1080
movq -0x40(%rbp), %rax
cmpl $0x3a98, (%rax)         # imm = 0x3A98
jle 0x34a3
movq 0x8b98(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movl (%rax), %edx
movq -0x38(%rbp), %rcx
leaq 0x5a1a(%rip), %rsi      # 0x8e8e
movl $0x3a98, %r8d           # imm = 0x3A98
movb $0x0, %al
callq 0x10e0
movq 0x8b70(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x5a4c(%rip), %rsi      # 0x8ede
movb $0x0, %al
callq 0x10e0
movl $0x1, %edi
callq 0x1160
movq 0x8b4e(%rip), %rax      # 0xbff8
movq (%rax), %rdi
movq -0x40(%rbp), %rax
movl (%rax), %edx
movq -0x38(%rbp), %rcx
movl -0x6c(%rbp), %r8d
leaq 0x5a3b(%rip), %rsi      # 0x8efd
movb $0x0, %al
callq 0x10e0
movq -0x48(%rbp), %rax
cmpl $0x1, (%rax)
jne 0x350b
movq -0x48(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jle 0x34fe
movq 0x8b15(%rip), %rax      # 0xbff8
movq (%rax), %rdi
leaq 0x5a4d(%rip), %rsi      # 0x8f3a
movb $0x0, %al
callq 0x10e0
movl $0x1, %edi
callq 0x1160
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x2d20
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)