// File: /workspace/github_cpp_makefile_star2_O1/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:
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
xorps %xmm2, %xmm2
jne 0x8ea7
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rsi, %r14
movq %rdi, %r15
cmpb $0x0, 0x45ef(%rip)      # 0xd360
jne 0x8d97
movl $0x0, 0x45ef(%rip)      # 0xd36c
leaq 0x45ec(%rip), %rdi      # 0xd370
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x3180
movb $0x1, 0x45c9(%rip)      # 0xd360
xorl %r12d, %r12d
movl %r12d, 0x45c3(%rip)     # 0xd364
movl %r12d, 0x45c0(%rip)     # 0xd368
movslq %r14d, %r13
movl %r12d, 0x45ba(%rip)     # 0xd36c
leaq 0x45b7(%rip), %rbx      # 0xd370
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3180
testq %r13, %r13
jle 0x8ddf
xorl %eax, %eax
movzbl (%r15,%rax), %ecx
incl (%rbx,%rcx,4)
incq %rax
cmpq %rax, %r13
jne 0x8dcf
xorl %eax, %eax
xorps %xmm2, %xmm2
cmpl $0x1, (%rax,%rbx)
sbbl $-0x1, %r12d
addq $0x4, %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x8de4
movl %r12d, 0x456d(%rip)     # 0xd36c
cmpl $0x100, %r12d           # imm = 0x100
ja 0x8e9a
cvtsi2ss %r14d, %xmm0
movss %xmm0, 0x8(%rsp)
xorps %xmm1, %xmm1
xorl %r14d, %r14d
movl (%r14,%rbx), %eax
testl %eax, %eax
je 0x8e51
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss 0x8(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movaps %xmm1, 0x10(%rsp)
callq 0x3380
movaps 0x10(%rsp), %xmm1
mulss 0xc(%rsp), %xmm0
addss %xmm0, %xmm1
addq $0x4, %r14
cmpq $0x400, %r14            # imm = 0x400
jne 0x8e1d
xorps 0x81b(%rip), %xmm1      # 0x9680
movaps %xmm1, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x44f7(%rip), %xmm0 # 0xd36c
callq 0x34a0
movaps 0x10(%rsp), %xmm2
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x44d9(%rip)     # 0xd364
movaps %xmm2, %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0x44ce(%rip)     # 0xd368
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
movaps %xmm2, %xmm0
retq