// File: 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
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movsd %xmm1, -0x30(%rbp)
movsd %xmm2, -0x38(%rbp)
movl %r8d, -0x3c(%rbp)
movsd -0x10(%rbp), %xmm0
movsd 0x102a9f(%rip), %xmm1   # 0x1f7398
ucomisd %xmm1, %xmm0
jbe 0xf4901
jmp 0xf4920
leaq 0x102e4d(%rip), %rdi    # 0x1f7755
leaq 0x107e8b(%rip), %rsi    # 0x1fc79a
movl $0x2ef, %edx            # imm = 0x2EF
leaq 0x107e86(%rip), %rcx    # 0x1fc7a1
callq 0x5320
movl -0x14(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x48(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, %rsp
movq %rax, -0x50(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb8(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xf49e2
movq -0xb8(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq -0xc8(%rbp), %rdx
movslq -0x64(%rbp), %rsi
movl $0x0, (%rdx,%rsi,4)
movslq -0x64(%rbp), %rdx
xorps %xmm0, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movslq -0x64(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0xf4997
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq %rax, -0xd0(%rbp)
movq -0x20(%rbp), %rdi
callq 0xf990
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0xf4a9e
movq -0x20(%rbp), %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0xfc80
movsd (%rax), %xmm0
divsd -0x10(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x14(%rbp), %eax
jl 0xf4a3e
movl -0x14(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x68(%rbp)
movq -0xc8(%rbp), %rax
movslq -0x68(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl -0x68(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jge 0xf4a8e
movq -0xc0(%rbp), %rax
movslq -0x70(%rbp), %rcx
movsd 0xfd5d7(%rip), %xmm0    # 0x1f2050
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xf4a59
jmp 0xf4a90
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xf49e9
movq -0xc0(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jae 0xf4ae4
movq -0xc0(%rbp), %rax
movl -0x7c(%rbp), %ecx
movsd (%rax,%rcx,8), %xmm0
divsd -0x78(%rbp), %xmm0
movl -0x7c(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0xf4ab5
movl $0x1, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0xf4be2
movq -0xc0(%rbp), %rax
movl -0x8c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x98(%rbp)
movl -0x8c(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0xa0(%rbp)
movsd -0x98(%rbp), %xmm0
subsd -0xa0(%rbp), %xmm0
movsd 0xfd4f7(%rip), %xmm1    # 0x1f2048
mulsd -0x10(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xf4b77
jp 0xf4b77
jmp 0xf4b8f
movsd 0xfdbf9(%rip), %xmm0    # 0x1f2778
mulsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x88(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0xf4bb4
movl -0x14(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0xb8(%rbp), %rax
movsd -0x88(%rbp), %xmm0
movl -0x8c(%rbp), %ecx
movsd %xmm0, (%rax,%rcx,8)
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xf4aee
movsd 0xfd466(%rip), %xmm0    # 0x1f2050
mulsd -0x10(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x107bf5(%rip), %rsi    # 0x1fc800
callq 0x5540
movq %rax, %rdi
movsd -0x10(%rbp), %xmm0
callq 0x5980
movq %rax, %rdi
leaq 0x107c09(%rip), %rsi    # 0x1fc830
callq 0x5540
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x58d0
movq %rax, %rdi
leaq 0x107c01(%rip), %rsi    # 0x1fc842
callq 0x5540
movq %rax, %rdi
movsd -0xa8(%rbp), %xmm0
callq 0x5980
movq %rax, %rdi
leaq 0x107bf0(%rip), %rsi    # 0x1fc850
callq 0x5540
movq %rax, %rdi
movl -0x3c(%rbp), %esi
callq 0x58d0
movq %rax, %rdi
leaq 0x107be7(%rip), %rsi    # 0x1fc861
callq 0x5540
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x58d0
movq %rax, %rdi
leaq 0x107be3(%rip), %rsi    # 0x1fc877
callq 0x5540
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x5980
movq %rax, %rdi
leaq 0x107bf4(%rip), %rsi    # 0x1fc8a4
callq 0x5540
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x5980
movq %rax, %rdi
leaq 0x107c0a(%rip), %rsi    # 0x1fc8d6
callq 0x5540
movl $0x0, -0xac(%rbp)
movl -0xac(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0xf4dab
movq -0x8(%rbp), %rdi
movsd 0xfd35a(%rip), %xmm0    # 0x1f2050
mulsd -0x10(%rbp), %xmm0
cvtsi2sdl -0xac(%rbp), %xmm1
mulsd %xmm1, %xmm0
callq 0x5980
movq %rax, %rdi
leaq 0x107598(%rip), %rsi    # 0x1fc2ae
callq 0x5540
movq %rax, %rdi
movq -0xc8(%rbp), %rax
movslq -0xac(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x58d0
movq %rax, %rdi
leaq 0x107570(%rip), %rsi    # 0x1fc2ae
callq 0x5540
movq %rax, %rdi
movq -0xc0(%rbp), %rax
movslq -0xac(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x5980
movq %rax, %rdi
leaq 0x107546(%rip), %rsi    # 0x1fc2ae
callq 0x5540
movq %rax, %rdi
movq -0xb8(%rbp), %rax
movslq -0xac(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x5980
movq %rax, %rdi
leaq 0x104893(%rip), %rsi    # 0x1f9625
callq 0x5540
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xf4cdb
movq -0x48(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
nopw (%rax,%rax)