#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 */
struct sample {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(int *);
double log2(double);
scalar_t__ malloc(int);
int *unique_values(struct sample const *, int, int *, int);
int value_count(struct sample const *, int, int, int);
double set_entropy(const struct sample *samples, int count) {
  int unique = 0;
  int *vals = unique_values(samples, count, &unique, 3);
  int *occurs = (int *)malloc(sizeof(int) * unique);
  for (int i = 0; i < unique; i++) {
    occurs[i] = value_count(samples, count, vals[i], 3);
  }
  double e = 0.0;
  for (int i = 0; i < unique; i++) {
    double f = (double)occurs[i] / (double)count;
    e -= f * log2(f);
  }
  free(occurs);
  free(vals);
  return e;
}


x86 assembly (clang, -O0):
<set_entropy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x10(%rbp),%rdx
mov    $0x3,%ecx
call   1050 <unique_values@plt>
mov    %rax,-0x18(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11dc <set_entropy+0x8c>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    $0x3,%ecx
call   1060 <value_count@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1199 <set_entropy+0x49>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1265 <set_entropy+0x115>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x40(%rbp),%xmm0
call   1040 <log2@plt>
movaps %xmm0,%xmm2
movsd  -0x48(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11eb <set_entropy+0x9b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x30(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_entropy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r15d
mov    %rdi,%r12
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    $0x3,%ecx
call   1050 <unique_values@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edi
shl    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
cmpl   $0x0,0x4(%rsp)
jle    123b <set_entropy+0xeb>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbp,4),%edx
mov    %r12,%rdi
mov    %r15d,%esi
mov    $0x3,%ecx
call   1060 <value_count@plt>
mov    %eax,(%rbx,%rbp,4)
add    $0x1,%rbp
movslq 0x4(%rsp),%rax
cmp    %rax,%rbp
jl     11a0 <set_entropy+0x50>
test   %eax,%eax
xorpd  %xmm0,%xmm0
jle    123f <set_entropy+0xef>
xorps  %xmm0,%xmm0
cvtsi2sd %r15d,%xmm0
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm0,%xmm0
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sdl (%rbx,%rbp,4),%xmm0
divsd  0x10(%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
call   1040 <log2@plt>
mulsd  0x18(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
subsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x1,%rbp
movslq 0x4(%rsp),%rax
cmp    %rax,%rbp
jl     11f0 <set_entropy+0xa0>
jmp    123f <set_entropy+0xef>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sample = type { i32 }
define double @set_entropy(%struct.sample* %0, i32 %1) {
%3 = alloca %struct.sample*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca double
%10 = alloca i32
%11 = alloca double
store %struct.sample* %0, %struct.sample** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%12 = load %struct.sample*, %struct.sample** %3
%13 = load i32, i32* %4
%14 = call i32* @unique_values(%struct.sample* %12, i32 %13, i32* %5, i32 3)
store i32* %14, i32** %6
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = mul i64 4, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** %7
store i32 0, i32* %8
br label %21
21: ; preds = %38, %2
%22 = load i32, i32* %8
%23 = load i32, i32* %5
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %41
25: ; preds = %21
%26 = load %struct.sample*, %struct.sample** %3
%27 = load i32, i32* %4
%28 = load i32*, i32** %6
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %28, i64 %30
%32 = load i32, i32* %31
%33 = call i32 @value_count(%struct.sample* %26, i32 %27, i32 %32, i32 3)
%34 = load i32*, i32** %7
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i32, i32* %34, i64 %36
store i32 %33, i32* %37
br label %38
38: ; preds = %25
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %21
41: ; preds = %21
store double 0.000000e+00, double* %9
store i32 0, i32* %10
br label %42
42: ; preds = %62, %41
%43 = load i32, i32* %10
%44 = load i32, i32* %5
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %65
46: ; preds = %42
%47 = load i32*, i32** %7
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %47, i64 %49
%51 = load i32, i32* %50
%52 = sitofp i32 %51 to double
%53 = load i32, i32* %4
%54 = sitofp i32 %53 to double
%55 = fdiv double %52, %54
store double %55, double* %11
%56 = load double, double* %11
%57 = load double, double* %11
%58 = call double @log2(double %57)
%59 = load double, double* %9
%60 = fneg double %56
%61 = call double @llvm.fmuladd.f64(double %60, double %58, double %59)
store double %61, double* %9
br label %62
62: ; preds = %46
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %42
65: ; preds = %42
%66 = load i32*, i32** %7
%67 = call i32 @free(i32* %66)
%68 = load i32*, i32** %6
%69 = call i32 @free(i32* %68)
%70 = load double, double* %9
ret double %70
}

LLVM IR (-O2):
%struct.sample = type { i32 }
define double @set_entropy(%struct.sample* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = call i32* @unique_values(%struct.sample* %0, i32 %1, i32* %3, i32 3)
%6 = load i32, i32* %3
%7 = shl i32 %6, 2
%8 = call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i32*
%10 = load i32, i32* %3
%11 = icmp sgt i32 %10, 0
br i1 %11, label %16, label %26
12: ; preds = %16
%13 = icmp sgt i32 %23, 0
br i1 %13, label %14, label %26
14: ; preds = %12
%15 = sitofp i32 %1 to double
br label %30
16: ; preds = %2, %16
%17 = phi i64 [ %22, %16 ], [ 0, %2 ]
%18 = getelementptr inbounds i32, i32* %5, i64 %17
%19 = load i32, i32* %18
%20 = call i32 @value_count(%struct.sample* %0, i32 %1, i32 %19, i32 3)
%21 = getelementptr inbounds i32, i32* %9, i64 %17
store i32 %20, i32* %21
%22 = add nuw nsw i64 %17, 1
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = icmp slt i64 %22, %24
br i1 %25, label %16, label %12
26: ; preds = %30, %2, %12
%27 = phi double [ 0.000000e+00, %12 ], [ 0.000000e+00, %2 ], [ %39, %30 ]
%28 = call i32 @free(i32* %9)
%29 = call i32 @free(i32* %5)
ret double %27
30: ; preds = %14, %30
%31 = phi i64 [ 0, %14 ], [ %40, %30 ]
%32 = phi double [ 0.000000e+00, %14 ], [ %39, %30 ]
%33 = getelementptr inbounds i32, i32* %9, i64 %31
%34 = load i32, i32* %33
%35 = sitofp i32 %34 to double
%36 = fdiv double %35, %15
%37 = call double @log2(double %36)
%38 = fneg double %36
%39 = call double @llvm.fmuladd.f64(double %38, double %37, double %32)
%40 = add nuw nsw i64 %31, 1
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = icmp slt i64 %40, %42
br i1 %43, label %30, label %26
}