// File: /workspace/github_c_makefile_star2_O1/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, %ebx
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, %r14
cvtss2sd 0xc(%rcx), %xmm1
movq %rdi, %r15
xorps %xmm0, %xmm0
cmpl $0x1, 0x4(%rdx)
jne 0x41f2
movq %r14, %rdi
movq %r13, %rsi
movl %ebx, %edx
movsd %xmm1, (%rsp)
callq 0x4044
movsd (%rsp), %xmm1
cmpl $0x1, 0x4(%r15)
jne 0x4291
cmpl $0x0, 0xc(%r13)
jle 0x42c6
mulsd 0x2fc0(%rip), %xmm1     # 0x71d0
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
xorps 0x2ed2(%rip), %xmm0     # 0x70f0
movaps %xmm0, 0x10(%rsp)
xorps %xmm2, %xmm2
xorl %ebp, %ebp
xorps %xmm1, %xmm1
movss %xmm1, (%rsp)
movss %xmm2, 0xc(%rsp)
movss 0x3c(%r13,%rbp,4), %xmm0
mulss 0x10(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x11b0
movss 0xc(%rsp), %xmm2
cvtsd2ss %xmm0, %xmm0
addss %xmm0, %xmm2
movl (%r12), %eax
subl 0x14(%r13,%rbp,4), %eax
incl %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
mulss %xmm0, %xmm1
movss (%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rsp)
movss (%rsp), %xmm1
incq %rbp
movslq 0xc(%r13), %rax
cmpq %rax, %rbp
jl 0x422b
jmp 0x42cc
cmpl $0x1, (%r13)
leaq 0x14(%r13), %rax
movl (%r12), %ecx
movslq %ebx, %rdx
leaq 0x2bf24(%r14,%rdx,4), %rdx
cmovneq %rax, %rdx
subl (%rdx), %ecx
incl %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
subss %xmm0, %xmm1
movss 0x2054(%rip), %xmm2     # 0x6318
jmp 0x42cc
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
ja 0x430c
cmpl $0x1, 0x14(%r15)
jne 0x42ea
movslq %ebx, %rax
movss 0x927c4(%r14,%rax,4), %xmm0
jmp 0x42f2
movss 0x2026(%rip), %xmm0     # 0x6318
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
callq 0x19bf