// File: /workspace/github_cpp_makefile_star2_O3/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, -0x44(%rbp)
movsd %xmm2, -0x58(%rbp)
movsd %xmm1, -0x50(%rbp)
movl %ecx, -0x48(%rbp)
movq %rdi, -0x60(%rbp)
ucomisd 0xc7be1(%rip), %xmm0    # 0x152278
jbe 0x8aa28
movl %esi, %r13d
movl %esi, %r12d
movq %rsp, %r14
leaq 0xf(,%r12,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %r15
leaq 0xf(,%r12,8), %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq %rsp, %r9
subq %rax, %r9
movq %r9, %rsp
testl %esi, %esi
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
jle 0x8a729
movq %rdx, -0x30(%rbp)
leaq (,%r12,4), %rdx
movq %r14, %rdi
xorl %esi, %esi
callq 0x4250
leaq (,%r12,8), %rbx
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x4250
movq -0x38(%rbp), %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x4250
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %r9
movsd -0x40(%rbp), %xmm0
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
subq %rax, %rcx
je 0x8a78a
sarq $0x3, %rcx
leal -0x1(%r13), %edx
xorl %esi, %esi
movsd 0xc2911(%rip), %xmm2    # 0x14d058
movsd (%rax,%rsi,8), %xmm1
divsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
cmpl %r13d, %edi
cmovgel %edx, %edi
movslq %edi, %r8
incl (%r14,%r8,4)
testl %r8d, %r8d
js 0x8a783
incl %edi
xorl %r8d, %r8d
movsd (%r15,%r8,8), %xmm1
addsd %xmm2, %xmm1
movsd %xmm1, (%r15,%r8,8)
incq %r8
cmpq %r8, %rdi
jne 0x8a76b
incl %esi
cmpq %rsi, %rcx
ja 0x8a747
testl %r13d, %r13d
movq %r14, -0x70(%rbp)
je 0x8a7c8
movsd (%r15), %xmm2
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, (%r15,%rax,8)
incq %rax
cmpq %rax, %r12
jne 0x8a79a
leal -0x1(%r13), %eax
cmpl $0x1, %eax
ja 0x8a7cd
movq %r12, %r14
cvtsi2sd %r13d, %xmm7
jmp 0x8a849
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movapd %xmm0, %xmm6
addsd %xmm0, %xmm6
cvtsi2sd %r13d, %xmm7
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movl %eax, %eax
decq %rax
xorl %ecx, %ecx
movapd 0xc6e50(%rip), %xmm2    # 0x151640
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(%r9,%rcx,8)
incq %rcx
cmpq %rcx, %rax
jne 0x8a7f0
movq %r12, %r14
mulsd %xmm0, %xmm7
movsd %xmm7, -0x30(%rbp)
leaq 0xcb2fc(%rip), %rsi     # 0x155b55
movl $0x2f, %edx
movq -0x60(%rbp), %r12
movq %r12, %rdi
callq 0x44e0
movq %r12, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x46b0
movq %rax, %rbx
leaq 0xcb304(%rip), %rsi     # 0x155b85
movl $0x11, %edx
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
movl %r13d, %esi
callq 0x4750
movq %rax, %rbx
leaq 0xcb2f4(%rip), %rsi     # 0x155b97
movl $0xd, %edx
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x46b0
movq %rax, %rbx
leaq 0xcb2de(%rip), %rsi     # 0x155ba5
movl $0x10, %edx
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
movl -0x44(%rbp), %esi
callq 0x4750
movq %rax, %rbx
leaq 0xcb2cd(%rip), %rsi     # 0x155bb6
movl $0x15, %edx
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
movl -0x48(%rbp), %esi
callq 0x4750
movq %rax, %rbx
leaq 0xcb2c1(%rip), %rsi     # 0x155bcc
movl $0x2c, %edx
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
movsd -0x50(%rbp), %xmm0
callq 0x46b0
movq %rax, %rbx
leaq 0xcb2ca(%rip), %rsi     # 0x155bf9
movl $0x31, %edx
movq %rax, %rdi
callq 0x44e0
movq %rbx, %rdi
movsd -0x58(%rbp), %xmm0
callq 0x46b0
leaq 0xcb2db(%rip), %rsi     # 0x155c2b
movl $0x2c, %edx
movq %rax, %rdi
callq 0x44e0
testl %r13d, %r13d
jle 0x8aa19
movq %r14, %rax
xorl %r14d, %r14d
movq %rax, -0x68(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd -0x40(%rbp), %xmm0
movq %r12, %rdi
callq 0x46b0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
leaq 0xcac3f(%rip), %r13     # 0x1555d6
movq %r13, %rsi
callq 0x44e0
movq -0x70(%rbp), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
callq 0x4750
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x44e0
movsd (%r15,%r14,8), %xmm0
movq %rbx, %rdi
callq 0x46b0
movq %rax, %rbx
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x44e0
movq -0x38(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
movq %rbx, %rdi
callq 0x46b0
movl $0x1, %edx
movq %rax, %rdi
leaq 0xc8c1a(%rip), %rsi     # 0x15361e
callq 0x44e0
movq -0x68(%rbp), %rax
incq %r14
cmpq %r14, %rax
jne 0x8a970
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc7c16(%rip), %rdi     # 0x152645
leaq 0xcb0b9(%rip), %rsi     # 0x155aef
leaq 0xcb0b9(%rip), %rcx     # 0x155af6
movl $0x2ef, %edx            # imm = 0x2EF
callq 0x4290