// File: /workspace/github_c_makefile_star2_O3/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
xorps %xmm0, %xmm0
cmpl $0x1, (%rdx)
je 0x3d82
movss 0xc(%rbx), %xmm1
mulss 0x8(%rbx), %xmm1
mulss 0x3421(%rip), %xmm1     # 0x70e8
movaps %xmm1, 0x20(%rsp)
movss 0x3418(%rip), %xmm0     # 0x70ec
callq 0x1150
movss %xmm0, 0x8(%rsp)
movaps 0x341a(%rip), %xmm1     # 0x7100
xorps 0x20(%rsp), %xmm1
movss 0x33f9(%rip), %xmm0     # 0x70ec
callq 0x1150
movss 0x8(%rsp), %xmm1
subss %xmm0, %xmm1
movaps %xmm1, %xmm0
callq 0x1110
movss %xmm0, 0x14(%rsp)
movq 0x62c1(%rip), %rax      # 0x9fd8
xorps %xmm0, %xmm0
cvtss2sd 0xc(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movq (%rax), %r15
xorps %xmm0, %xmm0
cvtss2sd 0x8(%rbx), %xmm0
movsd %xmm0, 0x18(%rsp)
movaps 0x20(%rsp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movss 0x339c(%rip), %xmm0     # 0x70ec
callq 0x1150
cvtss2sd %xmm0, %xmm3
leaq 0x33b0(%rip), %rsi      # 0x7110
movq %r15, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
movb $0x4, %al
callq 0x1100
movss 0x14(%rsp), %xmm0
movq 0x624f(%rip), %r15      # 0x9fd8
movq (%r15), %rdi
movss %xmm0, 0x14(%rsp)
cvtss2sd %xmm0, %xmm0
leaq 0x3399(%rip), %rsi      # 0x7136
movb $0x1, %al
callq 0x1100
movq (%r15), %rdi
movl 0x298100(%r14), %edx
leaq 0x33d6(%rip), %rsi      # 0x718b
xorl %eax, %eax
callq 0x1100
movl 0x298100(%r14), %eax
testl %eax, %eax
jle 0x3fa9
xorps %xmm4, %xmm4
xorps %xmm1, %xmm1
xorl %r12d, %r12d
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
movss %xmm0, 0x18(%rsp)
xorps %xmm2, %xmm2
movss 0x14c080(%r14,%r12,4), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x3f70
addss %xmm0, %xmm2
movss %xmm2, 0x3c(%rsp)
movaps %xmm4, 0x40(%rsp)
callq 0x1110
movss 0xc(%rbx), %xmm1
mulss 0x4(%rbx), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x14c080(%r14,%r12,4)
movss 0x1f20c0(%r14,%r12,4), %xmm0
callq 0x1110
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 0x3280(%rip), %xmm0     # 0x70ec
callq 0x1150
movss %xmm0, 0x14c080(%r14,%r12,4)
movss 0x1f20c0(%r14,%r12,4), %xmm1
movss 0x325f(%rip), %xmm0     # 0x70ec
callq 0x1150
movss %xmm0, 0x1f20c0(%r14,%r12,4)
movss 0x14c080(%r14,%r12,4), %xmm1
movss %xmm1, 0x20(%rsp)
movss 0x18(%rsp), %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x18(%rsp)
movss 0xc(%rbx), %xmm1
mulss 0x3227(%rip), %xmm1     # 0x70f0
movss 0x321b(%rip), %xmm0     # 0x70ec
callq 0x1150
mulss 0x20(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
addss %xmm0, %xmm1
movss %xmm1, 0x8(%rsp)
movss 0xc(%rbx), %xmm1
mulss 0x31fb(%rip), %xmm1     # 0x70f4
movss 0x14c080(%r14,%r12,4), %xmm0
movaps %xmm0, 0x20(%rsp)
movss 0x31dc(%rip), %xmm0     # 0x70ec
callq 0x1150
movaps %xmm0, 0x50(%rsp)
movss 0xc(%rbx), %xmm1
mulss 0x31d1(%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 0x31a6(%rip), %xmm0     # 0x70ec
callq 0x1150
movss 0x3c(%rsp), %xmm2
xorps %xmm1, %xmm1
movaps 0x40(%rsp), %xmm4
movaps 0x50(%rsp), %xmm3
unpcklps %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0],xmm3[1],xmm0[1]
mulps 0x20(%rsp), %xmm3
addps %xmm3, %xmm4
movl 0x298100(%r14), %eax
incq %r12
movslq %eax, %rcx
cmpq %rcx, %r12
jl 0x3de6
xorps %xmm0, %xmm0
cvtss2sd %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x18(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rsp), %xmm2
xorps %xmm3, %xmm3
cvtss2sd %xmm4, %xmm3
shufps $0x55, %xmm4, %xmm4     # xmm4 = xmm4[1,1,1,1]
cvtss2sd %xmm4, %xmm4
jmp 0x3fb8
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
xorps %xmm4, %xmm4
movq (%r15), %rdi
leaq 0x31d7(%rip), %rsi      # 0x7199
movb $0x5, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x1100