// File: /workspace/github_cpp_makefile_star2_O1/Luthaf[P]zeopp/psd.cc
// Function: Histogram(std::ostream&, double, int, std::vector<double, std::allocator<double>>&, int, double, double, int)
void Histogram(ostream& output, const double binSize, const int maxBins, vector<double>& diam, int count, double nodefrac, double outfrac, int numSamples){
  assert(binSize > threshold);
  int bins[maxBins]; 
  double cumBins[maxBins];
  double derivBins[maxBins];
  for (int i=0; i<maxBins; i++){
    bins[i] = 0;
    cumBins[i] = 0;
    derivBins[i] = 0;
  }
 
  int bin;
  for (unsigned int i=0; i<diam.size(); i++){
    bin = diam.at(i)/binSize;
    if (bin >= maxBins){
      bin = maxBins - 1;
    }
    bins[bin]++;
    for (int j=0; j<bin+1; j++){
      cumBins[j]++;
    }
  }
//Cumulative distribution: 1 is added to the bin PLUS all preceding bins. 
//Each bin is divided by the value of the first bin.
  double maxC= cumBins[0];
  for (unsigned int b=0; b<maxBins; b++){
    cumBins[b] = (cumBins[b]/maxC);
  }
//Derivative of cumulative distribution: This is pore size distribution. 
  double deriv;
  for (unsigned int n=1; n<maxBins-1; n++){
    double fxh= cumBins[n+1];
    double fx = cumBins[n-1];
    deriv = (fxh-fx)/(2.0*binSize);
    if (deriv!=0) deriv= deriv*-1.0;
    if (deriv >= maxBins){
      deriv = maxBins - 1;
    }
    derivBins[n]=deriv;
  }
 
  double numBin;
  numBin = binSize * 1.0 * maxBins;

  //new format - easier to parse
  output << "Pore size distribution histogram\nBin size (A): "<<binSize<<"\nNumber of bins: "<<maxBins<<"\nFrom: 0\nTo: "<<numBin<<"\nTotal samples: "<<numSamples<<"\nAccessible samples: "<<count<<"\nFraction of sample points in node spheres: "<<nodefrac<<"\nFraction of sample points outside node spheres: "<<outfrac<<"\n\nBin Count Cumulative_dist Derivative_dist\n";
  for (int i=0; i<maxBins; i++){
    output<<binSize*1.0*i<<" "<<bins[i]<<" "<<cumBins[i]<<" "<<derivBins[i]<<"\n";
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r8d, -0x3c(%rbp)
movsd %xmm2, -0x60(%rbp)
movsd %xmm1, -0x58(%rbp)
movl %ecx, -0x40(%rbp)
movq %rdi, -0x68(%rbp)
ucomisd 0xc7b57(%rip), %xmm0    # 0x1541c8
jbe 0x8c9e4
movl %esi, %r13d
movl %esi, %r14d
movq %rsp, %rdi
leaq 0xf(,%r14,4), %rax
andq $-0x10, %rax
subq %rax, %rdi
movq %rdi, %rsp
movq %rsp, %r15
leaq 0xf(,%r14,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
testl %esi, %esi
movq %rdi, -0x48(%rbp)
movsd %xmm0, -0x38(%rbp)
jle 0x8c6ff
movq %rdx, -0x30(%rbp)
leaq (,%r14,4), %rdx
xorl %esi, %esi
callq 0x4250
leaq (,%r14,8), %r12
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4250
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x4250
movq -0x30(%rbp), %rdx
movq -0x48(%rbp), %rdi
movsd -0x38(%rbp), %xmm0
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
subq %rax, %rcx
je 0x8c763
sarq $0x3, %rcx
leal -0x1(%r13), %edx
xorl %esi, %esi
movsd 0xc293b(%rip), %xmm2    # 0x14f058
movsd (%rax,%rsi,8), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
cmpl %r13d, %r9d
cmovgel %edx, %r9d
movslq %r9d, %r8
incl (%rdi,%r8,4)
testl %r8d, %r8d
js 0x8c75c
incl %r9d
xorl %r8d, %r8d
movsd (%r15,%r8,8), %xmm1
addsd %xmm2, %xmm1
movsd %xmm1, (%r15,%r8,8)
incq %r8
cmpq %r8, %r9
jne 0x8c744
incl %esi
cmpq %rsi, %rcx
ja 0x8c71d
testl %r13d, %r13d
je 0x8c787
movsd (%r15), %xmm2
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, (%r15,%rax,8)
incq %rax
cmpq %rax, %r14
jne 0x8c76f
movq %r14, -0x70(%rbp)
leal -0x1(%r13), %eax
cvtsi2sd %r13d, %xmm7
cmpl $0x2, %eax
jb 0x8c80c
movapd %xmm0, %xmm6
addsd %xmm0, %xmm6
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movl %eax, %eax
decq %rax
xorl %ecx, %ecx
movapd 0xc6df9(%rip), %xmm2    # 0x1535b0
movsd 0x10(%r15,%rcx,8), %xmm3
subsd (%r15,%rcx,8), %xmm3
divsd %xmm6, %xmm3
movapd %xmm3, %xmm4
xorpd %xmm2, %xmm4
movapd %xmm3, %xmm5
cmpneqsd %xmm4, %xmm5
andpd %xmm5, %xmm4
andnpd %xmm3, %xmm5
orpd %xmm4, %xmm5
movapd %xmm7, %xmm3
cmpnlesd %xmm5, %xmm3
movapd %xmm3, %xmm4
andpd %xmm5, %xmm4
andnpd %xmm1, %xmm3
orpd %xmm4, %xmm3
movlpd %xmm3, 0x8(%rbx,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x8c7b7
mulsd %xmm0, %xmm7
movsd %xmm7, -0x50(%rbp)
leaq 0xcb1d9(%rip), %rsi     # 0x1579f5
movl $0x2f, %edx
movq -0x68(%rbp), %r14
movq %r14, %rdi
movq %rbx, -0x30(%rbp)
callq 0x44e0
movq %r14, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x46b0
movq %rax, %r12
leaq 0xcb1dd(%rip), %rsi     # 0x157a25
movl $0x11, %edx
movq %rax, %rdi
callq 0x44e0
movq %r12, %rdi
movl %r13d, %esi
callq 0x4750
movq %rax, %r12
leaq 0xcb1cd(%rip), %rsi     # 0x157a37
movl $0xd, %edx
movq %rax, %rdi
callq 0x44e0
movq %r12, %rdi
movsd -0x50(%rbp), %xmm0
callq 0x46b0
movq %rax, %r12
leaq 0xcb1b7(%rip), %rsi     # 0x157a45
movl $0x10, %edx
movq %rax, %rdi
callq 0x44e0
movq %r12, %rdi
movl -0x3c(%rbp), %esi
callq 0x4750
movq %rax, %r12
leaq 0xcb1a6(%rip), %rsi     # 0x157a56
movl $0x15, %edx
movq %rax, %rdi
callq 0x44e0
movq %r12, %rdi
movl -0x40(%rbp), %esi
callq 0x4750
movq %rax, %r12
leaq 0xcb19a(%rip), %rsi     # 0x157a6c
movl $0x2c, %edx
movq %rax, %rdi
callq 0x44e0
movq %r12, %rdi
movsd -0x58(%rbp), %xmm0
callq 0x46b0
movq %rax, %r12
leaq 0xcb1a3(%rip), %rsi     # 0x157a99
movl $0x31, %edx
movq %rax, %rdi
callq 0x44e0
movq %r12, %rdi
movsd -0x60(%rbp), %xmm0
callq 0x46b0
leaq 0xcb1b4(%rip), %rsi     # 0x157acb
movl $0x2c, %edx
movq %rax, %rdi
callq 0x44e0
testl %r13d, %r13d
jle 0x8c9d5
xorl %ebx, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd -0x38(%rbp), %xmm0
movq %r14, %rdi
callq 0x46b0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
leaq 0xcab31(%rip), %r13     # 0x157486
movq %r13, %rsi
callq 0x44e0
movq -0x48(%rbp), %rax
movl (%rax,%rbx,4), %esi
movq %r12, %rdi
callq 0x4750
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x44e0
movsd (%r15,%rbx,8), %xmm0
movq %r12, %rdi
callq 0x46b0
movq %rax, %r12
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x44e0
movq -0x30(%rbp), %rax
movsd (%rax,%rbx,8), %xmm0
movq %r12, %rdi
callq 0x46b0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc8b6e(%rip), %rsi     # 0x15552e
callq 0x44e0
movq -0x70(%rbp), %rax
incq %rbx
cmpq %rbx, %rax
jne 0x8c92f
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc7baa(%rip), %rdi     # 0x154595
leaq 0xcaf9d(%rip), %rsi     # 0x15798f
leaq 0xcaf9d(%rip), %rcx     # 0x157996
movl $0x2ef, %edx            # imm = 0x2EF
callq 0x4290