#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 */
float log2f(float);
float strlen(char *);
float shannon(char *str) {
  float count[128] = {0};
  float sum = 0;
  for (int i = 0; i < strlen(str); i++)
    count[str[i]]++;
  for (int i = 0; i < 128; i++)
    sum +=
        count[i] ? (count[i] / strlen(str)) * log2f(count[i] / strlen(str)) : 0;
  return -1 * sum;
}


x86 assembly (clang, -O0):
<shannon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1040 <memset@plt>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x214(%rbp)
ff
movl   $0x0,-0x218(%rbp)
cvtsi2ssl -0x218(%rbp),%xmm0
ff
movss  %xmm0,-0x220(%rbp)
ff
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
movss  -0x220(%rbp),%xmm1
ff
ucomiss %xmm1,%xmm0
jbe    11cf <shannon+0x9f>
mov    -0x8(%rbp),%rax
movslq -0x218(%rbp),%rcx
movsbq (%rax,%rcx,1),%rax
movss  0xe5b(%rip),%xmm0
addss  -0x210(%rbp,%rax,4),%xmm0
ff ff
movss  %xmm0,-0x210(%rbp,%rax,4)
ff ff
mov    -0x218(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x218(%rbp)
jmp    1167 <shannon+0x37>
movl   $0x0,-0x21c(%rbp)
cmpl   $0x80,-0x21c(%rbp)
jge    12d5 <shannon+0x1a5>
movslq -0x21c(%rbp),%rax
movss  -0x210(%rbp,%rax,4),%xmm0
ff ff
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jne    1210 <shannon+0xe0>
jp     1210 <shannon+0xe0>
jmp    1299 <shannon+0x169>
movslq -0x21c(%rbp),%rax
movss  -0x210(%rbp,%rax,4),%xmm0
ff ff
movss  %xmm0,-0x230(%rbp)
ff
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
movaps %xmm0,%xmm1
movss  -0x230(%rbp),%xmm0
ff
divss  %xmm1,%xmm0
movss  %xmm0,-0x228(%rbp)
ff
movslq -0x21c(%rbp),%rax
movss  -0x210(%rbp,%rax,4),%xmm0
ff ff
movss  %xmm0,-0x22c(%rbp)
ff
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
movaps %xmm0,%xmm1
movss  -0x22c(%rbp),%xmm0
ff
divss  %xmm1,%xmm0
call   1050 <log2f@plt>
movaps %xmm0,%xmm1
movss  -0x228(%rbp),%xmm0
ff
mulss  %xmm1,%xmm0
movss  %xmm0,-0x224(%rbp)
ff
jmp    12a9 <shannon+0x179>
xorps  %xmm0,%xmm0
movss  %xmm0,-0x224(%rbp)
ff
jmp    12a9 <shannon+0x179>
movss  -0x224(%rbp),%xmm0
ff
addss  -0x214(%rbp),%xmm0
ff
movss  %xmm0,-0x214(%rbp)
ff
mov    -0x21c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x21c(%rbp)
jmp    11d9 <shannon+0xa9>
movss  0xd23(%rip),%xmm0
mulss  -0x214(%rbp),%xmm0
ff
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shannon>:
push   %r14
push   %rbx
sub    $0x228,%rsp
mov    %rdi,%r14
lea    0x20(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jbe    1199 <shannon+0x69>
xor    %ebx,%ebx
movsbq (%r14,%rbx,1),%rax
movss  0x20(%rsp,%rax,4),%xmm0
addss  0xe8d(%rip),%xmm0
movss  %xmm0,0x20(%rsp,%rax,4)
add    $0x1,%rbx
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
movss  %xmm0,0x8(%rsp)
mov    %r14,%rdi
call   1030 <strlen@plt>
ucomiss 0x8(%rsp),%xmm0
ja     1160 <shannon+0x30>
xor    %ebx,%ebx
xorps  %xmm1,%xmm1
jmp    11b1 <shannon+0x81>
addss  %xmm0,%xmm1
add    $0x1,%rbx
cmp    $0x80,%rbx
je     120b <shannon+0xdb>
movss  0x20(%rsp,%rbx,4),%xmm2
xorps  %xmm0,%xmm0
ucomiss %xmm0,%xmm2
jne    11c1 <shannon+0x91>
jnp    11a0 <shannon+0x70>
mov    %r14,%rdi
movaps %xmm1,0x10(%rsp)
movss  %xmm2,0x8(%rsp)
call   1030 <strlen@plt>
movss  0x8(%rsp),%xmm1
divss  %xmm0,%xmm1
movss  %xmm1,0xc(%rsp)
mov    %r14,%rdi
call   1030 <strlen@plt>
movss  0x8(%rsp),%xmm1
divss  %xmm0,%xmm1
movaps %xmm1,%xmm0
call   1050 <log2f@plt>
movaps 0x10(%rsp),%xmm1
mulss  0xc(%rsp),%xmm0
jmp    11a0 <shannon+0x70>
xorps  0xdfe(%rip),%xmm1
movaps %xmm1,%xmm0
add    $0x228,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define float @shannon(i8* %0) {
%2 = alloca i8*
%3 = alloca [128 x float]
%4 = alloca float
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
%7 = bitcast [128 x float]* %3 to i8*
store float 0.000000e+00, float* %4
store i32 0, i32* %5
br label %8
8: ; preds = %24, %1
%9 = load i32, i32* %5
%10 = sitofp i32 %9 to float
%11 = load i8*, i8** %2
%12 = call float @strlen(i8* %11)
%13 = fcmp olt float %10, %12
br i1 %13, label %14, label %27
14: ; preds = %8
%15 = load i8*, i8** %2
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i64
%21 = getelementptr inbounds [128 x float], [128 x float]* %3, i64 0, i64 %20
%22 = load float, float* %21
%23 = fadd float %22, 1.000000e+00
store float %23, float* %21
br label %24
24: ; preds = %14
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %8
27: ; preds = %8
store i32 0, i32* %6
br label %28
28: ; preds = %59, %27
%29 = load i32, i32* %6
%30 = icmp slt i32 %29, 128
br i1 %30, label %31, label %62
31: ; preds = %28
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [128 x float], [128 x float]* %3, i64 0, i64 %33
%35 = load float, float* %34
%36 = fcmp une float %35, 0.000000e+00
br i1 %36, label %37, label %54
37: ; preds = %31
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [128 x float], [128 x float]* %3, i64 0, i64 %39
%41 = load float, float* %40
%42 = load i8*, i8** %2
%43 = call float @strlen(i8* %42)
%44 = fdiv float %41, %43
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [128 x float], [128 x float]* %3, i64 0, i64 %46
%48 = load float, float* %47
%49 = load i8*, i8** %2
%50 = call float @strlen(i8* %49)
%51 = fdiv float %48, %50
%52 = call float @log2f(float %51)
%53 = fmul float %44, %52
br label %55
54: ; preds = %31
br label %55
55: ; preds = %54, %37
%56 = phi float [ %53, %37 ], [ 0.000000e+00, %54 ]
%57 = load float, float* %4
%58 = fadd float %57, %56
store float %58, float* %4
br label %59
59: ; preds = %55
%60 = load i32, i32* %6
%61 = add nsw i32 %60, 1
store i32 %61, i32* %6
br label %28
62: ; preds = %28
%63 = load float, float* %4
%64 = fmul float -1.000000e+00, %63
ret float %64
}

LLVM IR (-O2):
define float @shannon(i8* %0) {
%2 = alloca [128 x float]
%3 = bitcast [128 x float]* %2 to i8*
%4 = tail call float @strlen(i8* %0)
%5 = fcmp ogt float %4, 0.000000e+00
br i1 %5, label %7, label %6
6: ; preds = %7, %1
br label %22
7: ; preds = %1, %7
%8 = phi i64 [ %15, %7 ], [ 0, %1 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i64
%12 = getelementptr inbounds [128 x float], [128 x float]* %2, i64 0, i64 %11
%13 = load float, float* %12
%14 = fadd float %13, 1.000000e+00
store float %14, float* %12
%15 = add nuw i64 %8, 1
%16 = trunc i64 %15 to i32
%17 = sitofp i32 %16 to float
%18 = tail call float @strlen(i8* %0)
%19 = fcmp ogt float %18, %17
br i1 %19, label %7, label %6
20: ; preds = %35
%21 = fneg float %37
ret float %21
22: ; preds = %6, %35
%23 = phi i64 [ %38, %35 ], [ 0, %6 ]
%24 = phi float [ %37, %35 ], [ 0.000000e+00, %6 ]
%25 = getelementptr inbounds [128 x float], [128 x float]* %2, i64 0, i64 %23
%26 = load float, float* %25
%27 = fcmp une float %26, 0.000000e+00
br i1 %27, label %28, label %35
28: ; preds = %22
%29 = tail call float @strlen(i8* %0)
%30 = fdiv float %26, %29
%31 = tail call float @strlen(i8* %0)
%32 = fdiv float %26, %31
%33 = tail call float @log2f(float %32)
%34 = fmul float %30, %33
br label %35
35: ; preds = %22, %28
%36 = phi float [ %34, %28 ], [ 0.000000e+00, %22 ]
%37 = fadd float %24, %36
%38 = add nuw nsw i64 %23, 1
%39 = icmp eq i64 %38, 128
br i1 %39, label %20, label %22
}