#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 *);
scalar_t__ malloc(int);
int *unique_values(struct sample const *, int, int *, unsigned int);
int value_count(struct sample const *, int, int, unsigned int);
double gini_impurity(const struct sample *samples, int count, unsigned field) {
  double sum = 0.0;
  int unique = 0;
  int *vals = unique_values(samples, count, &unique, field);
  int *occurs = (int *)malloc(sizeof(int) * unique);
  for (int i = 0; i < unique; i++) {
    occurs[i] = value_count(samples, count, vals[i], field);
    double fi = (double)occurs[i] / (double)count;
    sum += fi - (fi * fi);
  }
  free(vals);
  free(occurs);
  return sum;
}


x86 assembly (clang, -O0):
<gini_impurity>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%ecx
lea    -0x1c(%rbp),%rdx
call   1040 <unique_values@plt>
mov    %rax,-0x28(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1226 <gini_impurity+0xe6>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%ecx
call   1050 <value_count@plt>
mov    %eax,%edx
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%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),%xmm1
movsd  -0x40(%rbp),%xmm0
movsd  -0x40(%rbp),%xmm2
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1192 <gini_impurity+0x52>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movsd  -0x18(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gini_impurity>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%r15
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %r14d,%ecx
call   1040 <unique_values@plt>
mov    %rax,%r13
mov    0x4(%rsp),%edi
shl    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
cmpl   $0x0,0x4(%rsp)
jle    11f0 <gini_impurity+0xb0>
cvtsi2sd %r12d,%xmm0
movsd  %xmm0,0x10(%rsp)
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    0x0(%r13,%rbx,4),%edx
mov    %r15,%rdi
mov    %r12d,%esi
mov    %r14d,%ecx
call   1050 <value_count@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mov    %eax,0x0(%rbp,%rbx,4)
divsd  0x10(%rsp),%xmm0
movapd %xmm0,%xmm1
mulsd  %xmm0,%xmm1
subsd  %xmm1,%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
add    $0x1,%rbx
movslq 0x4(%rsp),%rax
cmp    %rax,%rbx
jl     11a0 <gini_impurity+0x60>
jmp    11fa <gini_impurity+0xba>
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
movsd  0x8(%rsp),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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