#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 log(double);
double calc_shanon(int nhap, int nsamp, int *freq, int cpop) {
  int chap;
  double shan, p;
  shan = 0.0;
  for (chap = 0; chap < nhap; ++chap) {
    if (freq[chap] != 0 && nsamp != 0) {
      p = freq[chap] / (double)nsamp;
      shan -= p * (log(p) / log(2.0));
    }
  }
  return (shan);
}


x86 assembly (clang, -O0):
<calc_shanon>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    11ea <calc_shanon+0xda>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     11d7 <calc_shanon+0xc7>
cmpl   $0x0,-0x8(%rbp)
je     11d7 <calc_shanon+0xc7>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
cvtsi2sdl (%rax,%rcx,4),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1030 <log@plt>
movsd  %xmm0,-0x38(%rbp)
movsd  0xe68(%rip),%xmm0
call   1030 <log@plt>
movsd  -0x38(%rbp),%xmm2
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
divsd  %xmm1,%xmm2
movsd  -0x20(%rbp),%xmm1
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
jmp    11dc <calc_shanon+0xcc>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1134 <calc_shanon+0x24>
movsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_shanon>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
jle    119a <calc_shanon+0x8a>
mov    %rdx,%r14
mov    %esi,%r15d
cvtsi2sd %esi,%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %edi,%ebx
xorpd  %xmm0,%xmm0
xor    %ebp,%ebp
jmp    1149 <calc_shanon+0x39>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %rbp,%rbx
je     119e <calc_shanon+0x8e>
test   %r15d,%r15d
je     1140 <calc_shanon+0x30>
mov    (%r14,%rbp,4),%eax
test   %eax,%eax
je     1140 <calc_shanon+0x30>
xorps  %xmm1,%xmm1
cvtsi2sd %eax,%xmm1
divsd  0x8(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,(%rsp)
movapd %xmm1,%xmm0
call   1030 <log@plt>
divsd  0xe81(%rip),%xmm0
mulsd  0x10(%rsp),%xmm0
movsd  (%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
movsd  (%rsp),%xmm0
jmp    1140 <calc_shanon+0x30>
xorpd  %xmm0,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @calc_shanon(i32 %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca double
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
store double 0.000000e+00, double* %10
store i32 0, i32* %9
br label %12
12: ; preds = %45, %4
%13 = load i32, i32* %9
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %48
16: ; preds = %12
%17 = load i32*, i32** %7
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32, i32* %20
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %44
23: ; preds = %16
%24 = load i32, i32* %6
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %44
26: ; preds = %23
%27 = load i32*, i32** %7
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = sitofp i32 %31 to double
%33 = load i32, i32* %6
%34 = sitofp i32 %33 to double
%35 = fdiv double %32, %34
store double %35, double* %11
%36 = load double, double* %11
%37 = load double, double* %11
%38 = call double @log(double %37)
%39 = call double @log(double 2.000000e+00)
%40 = fdiv double %38, %39
%41 = load double, double* %10
%42 = fneg double %36
%43 = call double @llvm.fmuladd.f64(double %42, double %40, double %41)
store double %43, double* %10
br label %44
44: ; preds = %26, %23, %16
br label %45
45: ; preds = %44
%46 = load i32, i32* %9
%47 = add nsw i32 %46, 1
store i32 %47, i32* %9
br label %12
48: ; preds = %12
%49 = load double, double* %10
ret double %49
}

LLVM IR (-O2):
define double @calc_shanon(i32 %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %28
6: ; preds = %4
%7 = icmp ne i32 %1, 0
%8 = sitofp i32 %1 to double
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %6, %24
%11 = phi i64 [ 0, %6 ], [ %26, %24 ]
%12 = phi double [ 0.000000e+00, %6 ], [ %25, %24 ]
%13 = getelementptr inbounds i32, i32* %2, i64 %11
%14 = load i32, i32* %13
%15 = icmp ne i32 %14, 0
%16 = and i1 %7, %15
br i1 %16, label %17, label %24
17: ; preds = %10
%18 = sitofp i32 %14 to double
%19 = fdiv double %18, %8
%20 = tail call double @log(double %19)
%21 = fdiv double %20, 0x3FE62E42FEFA39EF
%22 = fneg double %19
%23 = tail call double @llvm.fmuladd.f64(double %22, double %21, double %12)
br label %24
24: ; preds = %10, %17
%25 = phi double [ %23, %17 ], [ %12, %10 ]
%26 = add nuw nsw i64 %11, 1
%27 = icmp eq i64 %26, %9
br i1 %27, label %28, label %10
28: ; preds = %24, %4
%29 = phi double [ 0.000000e+00, %4 ], [ %25, %24 ]
ret double %29
}