// File: /workspace/github_c_makefile_star2_O2/usgs[P]nshmp-haz-catalogs/src/agrid/adaptive/convert_eq_nos_rates.c
// Function: convert_eq_nos_rates
void convert_eq_nos_rates(struct rates* seisr, struct compl_levels* compl, struct smoothp* smp, struct catalog* cat, struct boundaries* reg)
/*--------------------------------------------------------------------------*/
{
  int cnt;
  float isum=0.0;
  float fac, sum0=0.0, sum3=0.0, sum4=0.0, sum5=0.0, sum_noSm=0.0;
  float arg_pow;

// get factors for converting cumulative to incremental rates 
// Herrmann's formula for converting cumulative to incremental
// set factor to zero to retain cumulative rates
  if ( smp->cumul_rates == 1 ) {
    fac=0.0;
  }
  else {
// 0.5-factor gives dMag about a magnitude value
    arg_pow=(cat->bval*cat->dMag*0.5);
    fac=log10f( powf(10,arg_pow) - powf(10,-arg_pow) );
    fprintf(stdout,"bval/dMag/arg_pow/powf: %f %f %f %f \n", cat->bval, cat->dMag, arg_pow, powf(10,arg_pow));
  }
  fprintf(stdout,"Additive factor (log) converting cumulative to incremental (cumulative fac=0): %.4f\n", fac);

// loop through rates at all points to modify rates
// apply to smoothed and earthquake count results
  fprintf(stdout,"nsrc pts: %d\n", seisr->nv);
  for (cnt=0; cnt<seisr->nv; cnt++) {
    if ( seisr->asum[cnt] > 0 ) {
//if (cnt==29375) fprintf(stderr,"asum smoothed (M4+), %f \n", seisr->asum[cnt]);
// convert from (cumulative) counts per cell to rates
      isum=isum+seisr->asum[cnt];
// convert to log(rate)(M=0), cumulative
      seisr->asum[cnt]=log10f(seisr->asum[cnt]) + cat->bval*cat->minMag;
      seisr->asum_noSm[cnt]=log10f(seisr->asum_noSm[cnt]) + cat->bval*cat->minMag;
// convert cumulative to incremental rates, if specified
      seisr->asum[cnt]=seisr->asum[cnt]+fac;
      seisr->asum_noSm[cnt]=seisr->asum_noSm[cnt]+fac;
// convert back to rates, 10^a
      seisr->asum[cnt]=powf(10,seisr->asum[cnt]);
      seisr->asum_noSm[cnt]=powf(10,seisr->asum_noSm[cnt]);
      sum0=sum0+seisr->asum[cnt];
      sum3=sum3+seisr->asum[cnt]*powf(10,-(cat->bval)*3);
      sum4=sum4+seisr->asum[cnt]*powf(10,-(cat->bval)*4);
      sum5=sum5+seisr->asum[cnt]*powf(10,-(cat->bval)*5);
      sum_noSm=sum_noSm+seisr->asum_noSm[cnt];
    }
//if (cnt==29375) fprintf(stderr,"(rates, M0), final %f \n", seisr->asum[cnt]);
  }
/*  float maxr=0; int imaxr=0;
  for (cnt=0; cnt<seisr->nv; cnt++) {
    if (seisr->asum[cnt]>maxr) {
      maxr=seisr->asum[cnt];
      imaxr=cnt;
    }
  } 
 fprintf(stderr,"maxrate: %f %d\n", maxr, imaxr); */
  
// print summary rate info
//  fprintf(stdout,"%d eqs -> rates M0/3/4/5: %.4e %.4e %.4e %.4e\n", isum, sum0, sum3, sum4, sum5);
  fprintf(stdout,"%.3f eqs -> sum rates (no./yr), M0/3/4/5: %.1f %.1f %.1f %.1f\n", isum, sum0, sum3, sum4, sum5);

}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %rbx
movq %rdi, %r14
cmpl $0x1, (%rdx)
xorps %xmm0, %xmm0
je 0x3761
movss 0xc(%rbx), %xmm1
mulss 0x8(%rbx), %xmm1
mulss 0x3a39(%rip), %xmm1     # 0x70e8
movaps %xmm1, 0x20(%rsp)
movss 0x3a30(%rip), %xmm0     # 0x70ec
callq 0x1140
movss %xmm0, 0x8(%rsp)
movaps 0x3a32(%rip), %xmm1     # 0x7100
xorps 0x20(%rsp), %xmm1
movss 0x3a11(%rip), %xmm0     # 0x70ec
callq 0x1140
movss 0x8(%rsp), %xmm1
subss %xmm0, %xmm1
movaps %xmm1, %xmm0
callq 0x1100
movss %xmm0, 0x14(%rsp)
movq 0x68d9(%rip), %rax      # 0x9fd8
cvtss2sd 0xc(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movq (%rax), %r15
cvtss2sd 0x8(%rbx), %xmm0
movsd %xmm0, 0x18(%rsp)
movaps 0x20(%rsp), %xmm1
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movss 0x39bd(%rip), %xmm0     # 0x70ec
callq 0x1140
cvtss2sd %xmm0, %xmm3
leaq 0x39d1(%rip), %rsi      # 0x7110
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movb $0x4, %al
callq 0x10f0
movss 0x14(%rsp), %xmm0
movq 0x6870(%rip), %r15      # 0x9fd8
movq (%r15), %rdi
movss %xmm0, 0x14(%rsp)
cvtss2sd %xmm0, %xmm0
leaq 0x39ba(%rip), %rsi      # 0x7136
movb $0x1, %al
callq 0x10f0
movq (%r15), %rdi
movl 0x298100(%r14), %edx
leaq 0x39f7(%rip), %rsi      # 0x718b
xorl %r12d, %r12d
xorl %eax, %eax
callq 0x10f0
xorps %xmm4, %xmm4
xorps %xmm1, %xmm1
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movss %xmm0, 0x18(%rsp)
movslq 0x298100(%r14), %rax
cmpq %rax, %r12
jge 0x394f
movss 0x14c080(%r14,%r12,4), %xmm0
ucomiss 0x3928(%rip), %xmm0     # 0x70fc
jbe 0x3947
addss %xmm0, %xmm1
movss %xmm1, 0x3c(%rsp)
movaps %xmm4, 0x40(%rsp)
callq 0x1100
movss 0xc(%rbx), %xmm1
mulss 0x4(%rbx), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x14c080(%r14,%r12,4)
movss 0x1f20c0(%r14,%r12,4), %xmm0
callq 0x1100
movss 0xc(%rbx), %xmm2
mulss 0x4(%rbx), %xmm2
addss %xmm0, %xmm2
movss 0x14c080(%r14,%r12,4), %xmm1
movss 0x14(%rsp), %xmm0
addss %xmm0, %xmm1
addss %xmm0, %xmm2
movss %xmm2, 0x1f20c0(%r14,%r12,4)
movss 0x389f(%rip), %xmm0     # 0x70ec
callq 0x1140
movss %xmm0, 0x14c080(%r14,%r12,4)
movss 0x1f20c0(%r14,%r12,4), %xmm1
movss 0x387e(%rip), %xmm0     # 0x70ec
callq 0x1140
movss %xmm0, 0x1f20c0(%r14,%r12,4)
movss 0x14c080(%r14,%r12,4), %xmm1
movss %xmm1, 0x20(%rsp)
movss 0x8(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x8(%rsp)
movss 0xc(%rbx), %xmm1
mulss 0x3846(%rip), %xmm1     # 0x70f0
movss 0x383a(%rip), %xmm0     # 0x70ec
callq 0x1140
mulss 0x20(%rsp), %xmm0
movss 0x18(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x18(%rsp)
movss 0xc(%rbx), %xmm1
mulss 0x381a(%rip), %xmm1     # 0x70f4
movss 0x14c080(%r14,%r12,4), %xmm0
movaps %xmm0, 0x20(%rsp)
movss 0x37fb(%rip), %xmm0     # 0x70ec
callq 0x1140
movaps %xmm0, 0x50(%rsp)
movss 0xc(%rbx), %xmm1
mulss 0x37f0(%rip), %xmm1     # 0x70f8
movss 0x14c080(%r14,%r12,4), %xmm0
movaps 0x20(%rsp), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
movaps %xmm2, 0x20(%rsp)
movss 0x37c5(%rip), %xmm0     # 0x70ec
callq 0x1140
movss 0x3c(%rsp), %xmm1
movaps 0x40(%rsp), %xmm4
movaps 0x50(%rsp), %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
mulps 0x20(%rsp), %xmm2
addps %xmm2, %xmm4
incq %r12
jmp 0x37b3
movq (%r15), %rdi
cvtss2sd %xmm1, %xmm0
cvtss2sd 0x8(%rsp), %xmm1
cvtss2sd 0x18(%rsp), %xmm2
cvtss2sd %xmm4, %xmm3
shufps $0x55, %xmm4, %xmm4     # xmm4 = xmm4[1,1,1,1]
cvtss2sd %xmm4, %xmm4
leaq 0x3824(%rip), %rsi      # 0x7199
movb $0x5, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x10f0
nop