#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 naiseki_double(double *, double *, int);
void calc_kl_component(double *kl, int dim, double *a, double **vec, int n) {
  double tmp[dim];
  int d;
  for (d = 0; d < dim; d++)
    tmp[d] = naiseki_double(a, (double *)((double *)vec + d * n), n);
  for (d = 0; d < dim; d++)
    kl[d] = tmp[d];
}


x86 assembly (clang, -O0):
<calc_kl_component>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a0 <calc_kl_component+0x90>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x3c(%rbp),%eax
imul   -0x24(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
call   1030 <naiseki_double@plt>
mov    -0x48(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1159 <calc_kl_component+0x49>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11db <calc_kl_component+0xcb>
mov    -0x48(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11a7 <calc_kl_component+0x97>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_kl_component>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,-0x2c(%rbp)
mov    %esi,%r13d
mov    %rsp,%r15
lea    0xf(,%r13,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
test   %esi,%esi
jle    11aa <calc_kl_component+0x8a>
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %esi,-0x30(%rbp)
mov    %rdi,-0x38(%rbp)
movslq -0x2c(%rbp),%rax
shl    $0x3,%rax
mov    %rax,-0x40(%rbp)
xor    %r14d,%r14d
nopl   (%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    -0x2c(%rbp),%edx
call   1040 <naiseki_double@plt>
movsd  %xmm0,(%r15,%r14,8)
add    $0x1,%r14
add    -0x40(%rbp),%rbx
cmp    %r14,%r13
jne    1170 <calc_kl_component+0x50>
cmpl   $0x0,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
jle    11aa <calc_kl_component+0x8a>
shl    $0x3,%r13
mov    %r15,%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @calc_kl_component(double* %0, i32 %1, double* %2, double** %3, i32 %4) {
%6 = alloca double*
%7 = alloca i32
%8 = alloca double*
%9 = alloca double**
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store double* %0, double** %6
store i32 %1, i32* %7
store double* %2, double** %8
store double** %3, double*** %9
store i32 %4, i32* %10
%14 = load i32, i32* %7
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %11
%17 = alloca double, i64 %15
store i64 %15, i64* %12
store i32 0, i32* %13
br label %18
18: ; preds = %36, %5
%19 = load i32, i32* %13
%20 = load i32, i32* %7
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %39
22: ; preds = %18
%23 = load double*, double** %8
%24 = load double**, double*** %9
%25 = bitcast double** %24 to double*
%26 = load i32, i32* %13
%27 = load i32, i32* %10
%28 = mul nsw i32 %26, %27
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds double, double* %25, i64 %29
%31 = load i32, i32* %10
%32 = call double @naiseki_double(double* %23, double* %30, i32 %31)
%33 = load i32, i32* %13
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds double, double* %17, i64 %34
store double %32, double* %35
br label %36
36: ; preds = %22
%37 = load i32, i32* %13
%38 = add nsw i32 %37, 1
store i32 %38, i32* %13
br label %18
39: ; preds = %18
store i32 0, i32* %13
br label %40
40: ; preds = %53, %39
%41 = load i32, i32* %13
%42 = load i32, i32* %7
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %56
44: ; preds = %40
%45 = load i32, i32* %13
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds double, double* %17, i64 %46
%48 = load double, double* %47
%49 = load double*, double** %6
%50 = load i32, i32* %13
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds double, double* %49, i64 %51
store double %48, double* %52
br label %53
53: ; preds = %44
%54 = load i32, i32* %13
%55 = add nsw i32 %54, 1
store i32 %55, i32* %13
br label %40
56: ; preds = %40
%57 = load i8*, i8** %11
ret void
}

LLVM IR (-O2):
define void @calc_kl_component(double* nocapture writeonly %0, i32 %1, double* %2, double** %3, i32 %4) {
%6 = bitcast double* %0 to i8*
%7 = zext i32 %1 to i64
%8 = alloca double, i64 %7
%9 = bitcast double* %8 to i8*
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %25
11: ; preds = %5
%12 = sext i32 %4 to i64
br label %16
13: ; preds = %16
br i1 %10, label %14, label %25
14: ; preds = %13
%15 = shl nuw nsw i64 %7, 3
br label %25
16: ; preds = %11, %16
%17 = phi i64 [ 0, %11 ], [ %23, %16 ]
%18 = mul nsw i64 %17, %12
%19 = getelementptr inbounds double*, double** %3, i64 %18
%20 = bitcast double** %19 to double*
%21 = tail call double @naiseki_double(double* %2, double* %20, i32 %4)
%22 = getelementptr inbounds double, double* %8, i64 %17
store double %21, double* %22
%23 = add nuw nsw i64 %17, 1
%24 = icmp eq i64 %23, %7
br i1 %24, label %13, label %16
25: ; preds = %5, %14, %13
ret void
}