#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 KLdivergence(double *P, double *Q, size_t sz) {
  size_t i;
  double k = 0;
  for (i = 0; i < sz; i++)
    if (P[i])
      k += P[i] * log(P[i] / Q[i]);
  return k;
}


x86 assembly (clang, -O0):
<KLdivergence>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c8 <KLdivergence+0xb8>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1167 <KLdivergence+0x57>
jp     1167 <KLdivergence+0x57>
jmp    11b2 <KLdivergence+0xa2>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
divsd  (%rax,%rcx,8),%xmm0
call   1030 <log@plt>
movaps %xmm0,%xmm2
movsd  -0x30(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11b7 <KLdivergence+0xa7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1134 <KLdivergence+0x24>
movsd  -0x28(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<KLdivergence>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     1184 <KLdivergence+0x74>
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
xorps  %xmm0,%xmm0
jmp    1139 <KLdivergence+0x29>
add    $0x1,%rbx
cmp    %rbx,%r12
je     1187 <KLdivergence+0x77>
movsd  (%r15,%rbx,8),%xmm1
ucomisd 0xeb9(%rip),%xmm1
jne    114b <KLdivergence+0x3b>
jnp    1130 <KLdivergence+0x20>
movsd  %xmm0,0x8(%rsp)
movapd %xmm1,%xmm0
divsd  (%r14,%rbx,8),%xmm0
movsd  %xmm1,0x10(%rsp)
call   1030 <log@plt>
mulsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
jmp    1130 <KLdivergence+0x20>
xorps  %xmm0,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define double @KLdivergence(double* %0, double* %1, i64 %2) {
%4 = alloca double*
%5 = alloca double*
%6 = alloca i64
%7 = alloca i64
%8 = alloca double
store double* %0, double** %4
store double* %1, double** %5
store i64 %2, i64* %6
store double 0.000000e+00, double* %8
store i64 0, i64* %7
br label %9
9: ; preds = %37, %3
%10 = load i64, i64* %7
%11 = load i64, i64* %6
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %40
13: ; preds = %9
%14 = load double*, double** %4
%15 = load i64, i64* %7
%16 = getelementptr inbounds double, double* %14, i64 %15
%17 = load double, double* %16
%18 = fcmp une double %17, 0.000000e+00
br i1 %18, label %19, label %36
19: ; preds = %13
%20 = load double*, double** %4
%21 = load i64, i64* %7
%22 = getelementptr inbounds double, double* %20, i64 %21
%23 = load double, double* %22
%24 = load double*, double** %4
%25 = load i64, i64* %7
%26 = getelementptr inbounds double, double* %24, i64 %25
%27 = load double, double* %26
%28 = load double*, double** %5
%29 = load i64, i64* %7
%30 = getelementptr inbounds double, double* %28, i64 %29
%31 = load double, double* %30
%32 = fdiv double %27, %31
%33 = call double @log(double %32)
%34 = load double, double* %8
%35 = call double @llvm.fmuladd.f64(double %23, double %33, double %34)
store double %35, double* %8
br label %36
36: ; preds = %19, %13
br label %37
37: ; preds = %36
%38 = load i64, i64* %7
%39 = add i64 %38, 1
store i64 %39, i64* %7
br label %9
40: ; preds = %9
%41 = load double, double* %8
ret double %41
}

LLVM IR (-O2):
define double @KLdivergence(double* nocapture %0, double* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %21, label %5
5: ; preds = %3, %17
%6 = phi double [ %18, %17 ], [ 0.000000e+00, %3 ]
%7 = phi i64 [ %19, %17 ], [ 0, %3 ]
%8 = getelementptr inbounds double, double* %0, i64 %7
%9 = load double, double* %8
%10 = fcmp une double %9, 0.000000e+00
br i1 %10, label %11, label %17
11: ; preds = %5
%12 = getelementptr inbounds double, double* %1, i64 %7
%13 = load double, double* %12
%14 = fdiv double %9, %13
%15 = tail call double @log(double %14)
%16 = tail call double @llvm.fmuladd.f64(double %9, double %15, double %6)
br label %17
17: ; preds = %5, %11
%18 = phi double [ %16, %11 ], [ %6, %5 ]
%19 = add nuw i64 %7, 1
%20 = icmp eq i64 %19, %2
br i1 %20, label %21, label %5
21: ; preds = %17, %3
%22 = phi double [ 0.000000e+00, %3 ], [ %18, %17 ]
ret double %22
}