// File: /workspace/github_cpp_makefile_star2_O3/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
pxor %xmm2, %xmm2
jne 0x8e83
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %r15
cmpb $0x0, 0x461c(%rip)      # 0xd360
jne 0x8d4d
movb $0x1, 0x4613(%rip)      # 0xd360
xorl %eax, %eax
movl %eax, 0x460f(%rip)      # 0xd364
movl %eax, 0x460d(%rip)      # 0xd368
movslq %r14d, %r12
leaq 0x460b(%rip), %rbx      # 0xd370
movl $0x400, %edx            # imm = 0x400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3180
testq %r12, %r12
jle 0x8d8b
xorl %eax, %eax
movzbl (%r15,%rax), %ecx
incl (%rbx,%rcx,4)
incq %rax
cmpq %rax, %r12
jne 0x8d7b
pxor %xmm0, %xmm0
xorl %eax, %eax
pcmpeqd %xmm2, %xmm2
pxor %xmm1, %xmm1
movdqa (%rax,%rbx), %xmm3
pcmpeqd %xmm0, %xmm3
paddd %xmm3, %xmm1
psubd %xmm2, %xmm1
addq $0x10, %rax
cmpq $0x400, %rax            # imm = 0x400
jne 0x8d99
pshufd $0xee, %xmm1, %xmm0     # xmm0 = xmm1[2,3,2,3]
paddd %xmm1, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
paddd %xmm0, %xmm1
movd %xmm1, %eax
movd %xmm1, 0x4598(%rip)     # 0xd36c
cmpl $0x100, %eax            # imm = 0x100
pxor %xmm2, %xmm2
ja 0x8e78
xorps %xmm0, %xmm0
cvtsi2ss %r14d, %xmm0
movss %xmm0, 0x8(%rsp)
pxor %xmm1, %xmm1
xorl %r14d, %r14d
movl (%r14,%rbx), %eax
testl %eax, %eax
je 0x8e2d
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
divss 0x8(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movdqa %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 0x8df8
pxor 0x83e(%rip), %xmm1      # 0x9680
movdqa %xmm1, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x4519(%rip), %xmm0 # 0xd36c
callq 0x34a0
movaps 0x10(%rsp), %xmm2
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x44fb(%rip)     # 0xd364
movaps %xmm2, %xmm1
divss %xmm0, %xmm1
movss %xmm1, 0x44f0(%rip)     # 0xd368
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
movdqa %xmm2, %xmm0
retq