#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 gini_impurity(struct sample const *, int, int);
double info_gain(struct sample const *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int *unique_values(struct sample const *, int, int *, int);
int value_count(struct sample const *, int, int, int);
void sample_stats(const struct sample *samples, int count) {
  for (int i = 0; i < 4; i++) {
    if (i == 3)
      continue;
    printf("--- field %i ---\n", i);
    printf("gini impurity: %g\n", gini_impurity(samples, count, i));
    printf("info gain:     %g\n", info_gain(samples, count, i));
    int unique = 0;
    int *vals = unique_values(samples, count, &unique, i);
    for (int j = 0; j < unique; j++) {
      int occur = value_count(samples, count, vals[j], i);
      printf("value %i occurs %i times\n", vals[j], occur);
    }
    free(vals);
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<sample_stats>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
jge    1277 <sample_stats+0x117>
cmpl   $0x3,-0x10(%rbp)
jne    118f <sample_stats+0x2f>
jmp    1269 <sample_stats+0x109>
mov    -0x10(%rbp),%esi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <gini_impurity@plt>
lea    0xe5c(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1080 <info_gain@plt>
lea    0xe52(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%ecx
lea    -0x14(%rbp),%rdx
call   1060 <unique_values@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1252 <sample_stats+0xf2>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x10(%rbp),%ecx
call   1070 <value_count@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
mov    -0x28(%rbp),%edx
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ff <sample_stats+0x9f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1176 <sample_stats+0x16>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sample_stats>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xeb1(%rip),%r14
xor    %r15d,%r15d
jmp    11b0 <sample_stats+0x40>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1030 <free@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
add    $0x1,%r15d
cmp    $0x4,%r15d
je     1264 <sample_stats+0xf4>
cmp    $0x3,%r15d
je     1264 <sample_stats+0xf4>
lea    0xe3f(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
call   1050 <gini_impurity@plt>
lea    0xe33(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
call   1090 <info_gain@plt>
lea    0xe2b(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
movl   $0x0,0x4(%rsp)
mov    %rbx,%rdi
mov    %ebp,%esi
lea    0x4(%rsp),%rdx
mov    %r15d,%ecx
call   1070 <unique_values@plt>
mov    %rax,%r12
cmpl   $0x0,0x4(%rsp)
jle    1190 <sample_stats+0x20>
xor    %r13d,%r13d
nopl   0x0(%rax)
mov    (%r12,%r13,4),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%ecx
call   1080 <value_count@plt>
mov    (%r12,%r13,4),%esi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%r13
movslq 0x4(%rsp),%rax
cmp    %rax,%r13
jl     1230 <sample_stats+0xc0>
jmp    1190 <sample_stats+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sample = type { i32 }
@.str = [18 x i8] c"--- field %i ---\0A\00"
@.str.1 = [19 x i8] c"gini impurity: %g\0A\00"
@.str.2 = [19 x i8] c"info gain: %g\0A\00"
@.str.3 = [26 x i8] c"value %i occurs %i times\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @sample_stats(%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 i32
store %struct.sample* %0, %struct.sample** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %10
10: ; preds = %62, %2
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 4
br i1 %12, label %13, label %65
13: ; preds = %10
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 3
br i1 %15, label %16, label %17
16: ; preds = %13
br label %62
17: ; preds = %13
%18 = load i32, i32* %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load %struct.sample*, %struct.sample** %3
%21 = load i32, i32* %4
%22 = load i32, i32* %5
%23 = call double @gini_impurity(%struct.sample* %20, i32 %21, i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), double %23)
%25 = load %struct.sample*, %struct.sample** %3
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = call double @info_gain(%struct.sample* %25, i32 %26, i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), double %28)
store i32 0, i32* %6
%30 = load %struct.sample*, %struct.sample** %3
%31 = load i32, i32* %4
%32 = load i32, i32* %5
%33 = call i32* @unique_values(%struct.sample* %30, i32 %31, i32* %6, i32 %32)
store i32* %33, i32** %7
store i32 0, i32* %8
br label %34
34: ; preds = %55, %17
%35 = load i32, i32* %8
%36 = load i32, i32* %6
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %58
38: ; preds = %34
%39 = load %struct.sample*, %struct.sample** %3
%40 = load i32, i32* %4
%41 = load i32*, i32** %7
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
%46 = load i32, i32* %5
%47 = call i32 @value_count(%struct.sample* %39, i32 %40, i32 %45, i32 %46)
store i32 %47, i32* %9
%48 = load i32*, i32** %7
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %48, i64 %50
%52 = load i32, i32* %51
%53 = load i32, i32* %9
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %52, i32 %53)
br label %55
55: ; preds = %38
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
br label %34
58: ; preds = %34
%59 = load i32*, i32** %7
%60 = call i32 @free(i32* %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %58, %16
%63 = load i32, i32* %5
%64 = add nsw i32 %63, 1
store i32 %64, i32* %5
br label %10
65: ; preds = %10
ret void
}

LLVM IR (-O2):
%struct.sample = type { i32 }
@.str = [18 x i8] c"--- field %i ---\0A\00"
@.str.1 = [19 x i8] c"gini impurity: %g\0A\00"
@.str.2 = [19 x i8] c"info gain: %g\0A\00"
@.str.3 = [26 x i8] c"value %i occurs %i times\0A\00"
define void @sample_stats(%struct.sample* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
br label %6
5: ; preds = %6, %29
ret void
6: ; preds = %2, %29
%7 = phi i32 [ 0, %2 ], [ %32, %29 ]
%8 = icmp eq i32 %7, 3
br i1 %8, label %5, label %9
9: ; preds = %6
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
%11 = call double @gini_impurity(%struct.sample* %0, i32 %1, i32 %7)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), double %11)
%13 = call double @info_gain(%struct.sample* %0, i32 %1, i32 %7)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), double %13)
store i32 0, i32* %3
%15 = call i32* @unique_values(%struct.sample* %0, i32 %1, i32* %3, i32 %7)
%16 = load i32, i32* %3
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %9, %18
%19 = phi i64 [ %25, %18 ], [ 0, %9 ]
%20 = getelementptr inbounds i32, i32* %15, i64 %19
%21 = load i32, i32* %20
%22 = call i32 @value_count(%struct.sample* %0, i32 %1, i32 %21, i32 %7)
%23 = load i32, i32* %20
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %22)
%25 = add nuw nsw i64 %19, 1
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = icmp slt i64 %25, %27
br i1 %28, label %18, label %29
29: ; preds = %18, %9
%30 = call i32 @free(i32* %15)
%31 = call i32 @putchar(i32 10)
%32 = add nuw nsw i32 %7, 1
%33 = icmp eq i32 %32, 4
br i1 %33, label %5, label %6
}