// File: ekg[P]fastahack/disorder.c
// Function: shannon_H(char*, long long)
float
shannon_H(char* buf, 
	  long long length)
{
  int i = 0;
  float bits = 0.0;
  char* itr=NULL; //values of itr should be zero to 255
  unsigned char token;
  int num_events = 0; //`length' parameter
  float freq = 0.0; //loop variable for holding freq from m_token_freq[]
  float entropy = 0.0; //running entropy sum

  if(NULL==buf || 0==length)
    return 0.0;

  if(0==LIBDISORDER_INITIALIZED)
    initialize_lib();

  itr = buf;
  m_maxent = 0.0;
  m_ratio = 0.0;
  num_events = length;
  get_token_frequencies(itr, num_events); //modifies m_token_freqs[]
  //set m_num_tokens by counting unique m_token_freqs entries
  count_num_tokens(); 

  if(m_num_tokens>LIBDO_MAX_BYTES)
  {
    //report error somehow?
    return 0.0;
  }

  //iterate through whole m_token_freq array, but only count
  //spots that have a registered token (i.e., freq>0)
  for(i=0;i<LIBDO_MAX_BYTES;i++)
  {
    if(0!=m_token_freqs[i])
    {
      token = i;
      freq = ((float)m_token_freqs[token]); 
      m_token_probs[token] = (freq / ((float)num_events));
      entropy += m_token_probs[token] * log2(m_token_probs[token]);
    }
  }

  bits = -1.0 * entropy;
  m_maxent = log2(m_num_tokens);
  m_ratio = bits / m_maxent;

  return bits;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x34(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x38(%rbp)
xorl %eax, %eax
cmpq -0x10(%rbp), %rax
je 0xd34e
xorl %eax, %eax
cmpq -0x18(%rbp), %rax
jne 0xd35b
xorps %xmm0, %xmm0
movss %xmm0, -0x4(%rbp)
jmp 0xd4cd
xorl %eax, %eax
cmpl 0x707d(%rip), %eax      # 0x143e0
jne 0xd36a
callq 0xd4e0
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, 0x7067(%rip)     # 0x143e4
xorps %xmm0, %xmm0
movss %xmm0, 0x7060(%rip)     # 0x143e8
movq -0x18(%rbp), %rax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x30(%rbp), %rsi
callq 0xd550
callq 0xd620
cmpl $0x100, 0x7041(%rip)    # imm = 0x100
jle 0xd3ba
xorps %xmm0, %xmm0
movss %xmm0, -0x4(%rbp)
jmp 0xd4cd
movl $0x0, -0x1c(%rbp)
cmpl $0x100, -0x1c(%rbp)     # imm = 0x100
jge 0xd479
movslq -0x1c(%rbp), %rdx
xorl %eax, %eax
leaq 0x7015(%rip), %rcx      # 0x143f0
cmpl (%rcx,%rdx,4), %eax
je 0xd469
movl -0x1c(%rbp), %eax
movb %al, -0x29(%rbp)
movzbl -0x29(%rbp), %eax
movl %eax, %ecx
leaq 0x6ff9(%rip), %rax      # 0x143f0
cvtsi2ssl (%rax,%rcx,4), %xmm0
movss %xmm0, -0x34(%rbp)
movss -0x34(%rbp), %xmm0
cvtsi2ssl -0x30(%rbp), %xmm1
divss %xmm1, %xmm0
movzbl -0x29(%rbp), %eax
movl %eax, %ecx
leaq 0x73d4(%rip), %rax      # 0x147f0
movss %xmm0, (%rax,%rcx,4)
movzbl -0x29(%rbp), %eax
movl %eax, %ecx
leaq 0x73c2(%rip), %rax      # 0x147f0
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x3c(%rbp)
movzbl -0x29(%rbp), %eax
movl %eax, %ecx
leaq 0x73ab(%rip), %rax      # 0x147f0
movss (%rax,%rcx,4), %xmm0
callq 0xd6b0
movaps %xmm0, %xmm2
movss -0x3c(%rbp), %xmm0
movss -0x38(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x38(%rbp)
jmp 0xd46b
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xd3c1
movss -0x38(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x11e6(%rip), %xmm0     # 0xe670
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x20(%rbp)
movl 0x6f4f(%rip), %edi      # 0x143ec
callq 0xd6d0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x6f36(%rip)     # 0x143e4
movss -0x20(%rbp), %xmm0
divss 0x6f29(%rip), %xmm0     # 0x143e4
movss %xmm0, 0x6f25(%rip)     # 0x143e8
movss -0x20(%rbp), %xmm0
movss %xmm0, -0x4(%rbp)
movss -0x4(%rbp), %xmm0
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)