// File: /workspace/github_c_makefile_star2_O2/usgs[P]nshmp-haz-catalogs/src/agrid/adaptive/smooth_seis_rates.c
// Function: calc_seis_rate
float calc_seis_rate(struct smoothp* smpp, struct eqcat* eqcatp, struct compl_levels* cat_compp, 
	struct catalog* cat_parp, int eqno)
/*--------------------------------------------------------------------------*/
{
  int cnt;
  float num_fac, denom_fac, rate_fac, ratev, fac_exp;
//  float bvalue, beta, n_eff, mag_eq, denom_mod;
  float bvalue, beta, n_eff, denom_mod;
//  float bvalue, beta, n_eff, denom_mod;

// 
  bvalue=cat_parp->bval;
  beta=2.303*bvalue;
//  mag_eq=eqcatp->mag[eqno];

// check if earthquake occurred in a region with catalog gaps
// this feature allows for removing potentially induced earthquakes from catalog but 
// correctly calculating the rates with reduced time windows
  if ( (cat_compp->catalogGaps) == 1 ) {
    denom_mod=check_earthquake_catalog_gaps(eqcatp,cat_compp,eqno);
  }
  else {
    denom_mod=0;
  }

// convert from numbers to rates here
  if ( smpp->adj_compl == 1 ) {
    num_fac=0.0;
    denom_fac=0.0;
    for(cnt=0; cnt<cat_compp->nlevels; cnt++) {
      fac_exp=exp(-beta*(cat_compp->mag_comp[cnt]));
      num_fac=num_fac+fac_exp;
      denom_fac=denom_fac+fac_exp*(cat_parp->end_yr-cat_compp->yr_comp[cnt]+1);
    }
  }
  else {
    num_fac=1.0;
    if ( cat_compp->variableCompleteness == 1 ) {
      denom_fac=(cat_parp->end_yr-eqcatp->year_compl[eqno]+1);
    }
    else {
      denom_fac=(cat_parp->end_yr-cat_compp->yr_comp[0]+1);
    }
    denom_fac=denom_fac-denom_mod;
  }
  if (denom_fac<0) { 
    fprintf(stderr,"Problem with rate calculation.\n");
    exit(1);
  }
  rate_fac=num_fac/denom_fac;

// apply effective-number-count here, if appropriate 
  if ( (smpp->use_neff) == 1 ) {
    n_eff=eqcatp->neff[eqno];
  }
  else {
    n_eff=1.00;
  }
// ratev corresponds to the annual rate of earthquake (array element, eqno) in the catalog 
  ratev=rate_fac*n_eff;
    
  return ratev;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r8d, %ebp
movq %rcx, %r15
movq %rdx, %r12
movq %rsi, %rbx
cvtss2sd 0xc(%rcx), %xmm1
movq %rdi, %r14
xorps %xmm0, %xmm0
cmpl $0x1, 0x4(%rdx)
jne 0x3b89
movq %rbx, %rdi
movq %r12, %rsi
movl %ebp, %edx
movsd %xmm1, (%rsp)
callq 0x39ee
movsd (%rsp), %xmm1
cmpl $0x1, 0x4(%r14)
movslq %ebp, %r13
jne 0x3c1e
mulsd 0x3641(%rip), %xmm1     # 0x71e0
cvtsd2ss %xmm1, %xmm0
xorps 0x3556(%rip), %xmm0     # 0x7100
movaps %xmm0, 0x10(%rsp)
xorps %xmm1, %xmm1
xorl %ebp, %ebp
xorps %xmm2, %xmm2
movslq 0xc(%r12), %rax
cmpq %rax, %rbp
jge 0x3c4b
movss 0x3c(%r12,%rbp,4), %xmm0
mulss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss %xmm1, (%rsp)
movss %xmm2, 0xc(%rsp)
callq 0x11a0
movss 0xc(%rsp), %xmm2
cvtsd2ss %xmm0, %xmm0
movl (%r15), %eax
subl 0x14(%r12,%rbp,4), %eax
incl %eax
cvtsi2ss %eax, %xmm1
addss %xmm0, %xmm2
mulss %xmm0, %xmm1
movss (%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rsp)
movss (%rsp), %xmm1
incq %rbp
jmp 0x3bb7
leaq 0x14(%r12), %rax
cmpl $0x1, (%r12)
movl (%r15), %ecx
leaq 0x2bf24(%rbx,%r13,4), %rdx
cmovneq %rax, %rdx
subl (%rdx), %ecx
incl %ecx
cvtsi2ss %ecx, %xmm1
subss %xmm0, %xmm1
movss 0x358d(%rip), %xmm2     # 0x71d8
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
ja 0x3c88
cmpl $0x1, 0x14(%r14)
jne 0x3c66
movss 0x927c4(%rbx,%r13,4), %xmm0
jmp 0x3c6e
movss 0x356a(%rip), %xmm0     # 0x71d8
divss %xmm1, %xmm2
mulss %xmm0, %xmm2
movaps %xmm2, %xmm0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6369(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x35a4(%rip), %rdi      # 0x723d
pushq $0x1f
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
pushq $0x1
popq %rdi
callq 0x1180