#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double log2(double);
int /*<<< orphan*/ memset(int **, char, int);
double shannon_entropy(const unsigned char *p, size_t size) {
  double entropy = 0.0;
  int histogram[255 + 1];
  unsigned int i;
  memset((&histogram), '\0', sizeof(*(&histogram)));
  for (i = 0; i < size; ++i)
    ++histogram[p[i]];
  for (i = 0; i <= 255; ++i) {
    if (histogram[i] != 0) {
      double p = (double)histogram[i] / size;
      entropy -= p * log2(p);
    }
  }
  return entropy;
}


x86 assembly (clang, -O0):
<shannon_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movl   $0x0,-0x424(%rbp)
mov    -0x424(%rbp),%eax
cmp    -0x10(%rbp),%rax
jae    119b <shannon_entropy+0x7b>
mov    -0x8(%rbp),%rax
mov    -0x424(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
mov    -0x420(%rbp,%rax,4),%ecx
add    $0x1,%ecx
mov    %ecx,-0x420(%rbp,%rax,4)
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    1158 <shannon_entropy+0x38>
movl   $0x0,-0x424(%rbp)
cmpl   $0xff,-0x424(%rbp)
ja     1275 <shannon_entropy+0x155>
mov    -0x424(%rbp),%eax
cmpl   $0x0,-0x420(%rbp,%rax,4)
je     125c <shannon_entropy+0x13c>
mov    -0x424(%rbp),%eax
cvtsi2sdl -0x420(%rbp,%rax,4),%xmm0
ff ff
movaps 0xe21(%rip),%xmm2
movq   -0x10(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xe20(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x430(%rbp)
ff
movsd  -0x430(%rbp),%xmm0
ff
movsd  %xmm0,-0x438(%rbp)
ff
movsd  -0x430(%rbp),%xmm0
ff
call   1030 <log2@plt>
movaps %xmm0,%xmm2
movsd  -0x438(%rbp),%xmm0
ff
movsd  -0x18(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    1261 <shannon_entropy+0x141>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    11a5 <shannon_entropy+0x85>
movsd  -0x18(%rbp),%xmm0
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shannon_entropy>:
push   %r14
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
test   %rbx,%rbx
je     1162 <shannon_entropy+0x42>
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
movzbl (%r14,%rax,1),%ecx
addl   $0x1,0x20(%rsp,%rcx,4)
add    $0x1,%eax
cmp    %rbx,%rax
jb     1150 <shannon_entropy+0x30>
movq   %rbx,%xmm0
punpckldq 0xe91(%rip),%xmm0
subpd  0xe99(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movapd %xmm1,0x10(%rsp)
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
jmp    11ad <shannon_entropy+0x8d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    $0x100,%rbx
je     11f1 <shannon_entropy+0xd1>
mov    0x20(%rsp,%rbx,4),%eax
test   %eax,%eax
je     11a0 <shannon_entropy+0x80>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
divsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  %xmm0,(%rsp)
movapd %xmm1,%xmm0
call   1030 <log2@plt>
mulsd  0x8(%rsp),%xmm0
movsd  (%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
jmp    11a0 <shannon_entropy+0x80>
add    $0x428,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define double @shannon_entropy(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca double
%6 = alloca [256 x i32]
%7 = alloca i32
%8 = alloca double
store i8* %0, i8** %3
store i64 %1, i64* %4
store double 0.000000e+00, double* %5
%9 = bitcast [256 x i32]* %6 to i32**
%10 = call i32 @memset(i32** %9, i8 signext 0, i32 1024)
store i32 0, i32* %7
br label %11
11: ; preds = %26, %2
%12 = load i32, i32* %7
%13 = zext i32 %12 to i64
%14 = load i64, i64* %4
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %29
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = load i32, i32* %7
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = zext i8 %21 to i64
%23 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = add nsw i32 %24, 1
store i32 %25, i32* %23
br label %26
26: ; preds = %16
%27 = load i32, i32* %7
%28 = add i32 %27, 1
store i32 %28, i32* %7
br label %11
29: ; preds = %11
store i32 0, i32* %7
br label %30
30: ; preds = %55, %29
%31 = load i32, i32* %7
%32 = icmp ule i32 %31, 255
br i1 %32, label %33, label %58
33: ; preds = %30
%34 = load i32, i32* %7
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %33
%40 = load i32, i32* %7
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %41
%43 = load i32, i32* %42
%44 = sitofp i32 %43 to double
%45 = load i64, i64* %4
%46 = uitofp i64 %45 to double
%47 = fdiv double %44, %46
store double %47, double* %8
%48 = load double, double* %8
%49 = load double, double* %8
%50 = call double @log2(double %49)
%51 = load double, double* %5
%52 = fneg double %48
%53 = call double @llvm.fmuladd.f64(double %52, double %50, double %51)
store double %53, double* %5
br label %54
54: ; preds = %39, %33
br label %55
55: ; preds = %54
%56 = load i32, i32* %7
%57 = add i32 %56, 1
store i32 %57, i32* %7
br label %30
58: ; preds = %30
%59 = load double, double* %5
ret double %59
}

LLVM IR (-O2):
define double @shannon_entropy(i8* nocapture %0, i64 %1) {
%3 = alloca [256 x i32]
%4 = bitcast [256 x i32]* %3 to i8*
%5 = bitcast [256 x i32]* %3 to i32**
%6 = call i32 @memset(i32** %5, i8 signext 0, i32 1024)
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %10
8: ; preds = %10, %2
%9 = uitofp i64 %1 to double
br label %21
10: ; preds = %2, %10
%11 = phi i64 [ %19, %10 ], [ 0, %2 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = zext i8 %13 to i64
%15 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 %14
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 1
store i32 %17, i32* %15
%18 = add nuw nsw i64 %11, 1
%19 = and i64 %18, 4294967295
%20 = icmp ult i64 %19, %1
br i1 %20, label %10, label %8
21: ; preds = %8, %33
%22 = phi i64 [ 0, %8 ], [ %35, %33 ]
%23 = phi double [ 0.000000e+00, %8 ], [ %34, %33 ]
%24 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 %22
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = sitofp i32 %25 to double
%29 = fdiv double %28, %9
%30 = call double @log2(double %29)
%31 = fneg double %29
%32 = call double @llvm.fmuladd.f64(double %31, double %30, double %23)
br label %33
33: ; preds = %21, %27
%34 = phi double [ %32, %27 ], [ %23, %21 ]
%35 = add nuw nsw i64 %22, 1
%36 = icmp eq i64 %35, 256
br i1 %36, label %37, label %21
37: ; preds = %33
ret double %34
}