#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 sqrt(double);
double cosine_distance(double *A, double *B, int D) {
  double dot = 0.0;
  double lenA = 0.0, lenB = 0.0;
  for (int i = 0; i < D; i++) {
    dot += (*A) * (*B);
    lenA += (*A) * (*A);
    lenB += (*B) * (*B);
    A++;
    B++;
  }
  return dot / (sqrt(lenA) * sqrt(lenB));
}


x86 assembly (clang, -O0):
<cosine_distance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11da <cosine_distance+0xca>
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm2
movsd  -0x30(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1142 <cosine_distance+0x32>
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x28(%rbp),%xmm0
call   1030 <sqrt@plt>
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm0
call   1030 <sqrt@plt>
movsd  -0x48(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x40(%rbp),%xmm0
mulsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cosine_distance>:
sub    $0x28,%rsp
xorpd  %xmm1,%xmm1
test   %edx,%edx
jle    1137 <cosine_distance+0x27>
cmp    $0x1,%edx
jne    1144 <cosine_distance+0x34>
xorpd  %xmm0,%xmm0
xorpd  %xmm5,%xmm5
test   $0x1,%dl
jne    11ce <cosine_distance+0xbe>
jmp    11f2 <cosine_distance+0xe2>
xorpd  %xmm6,%xmm6
xorpd  %xmm5,%xmm5
jmp    11f2 <cosine_distance+0xe2>
mov    %edx,%r8d
and    $0xfffffffe,%r8d
xorpd  %xmm0,%xmm0
xorpd  %xmm5,%xmm5
xor    %eax,%eax
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
movsd  (%rdi,%rax,8),%xmm2
movsd  0x8(%rdi,%rax,8),%xmm6
movsd  (%rsi,%rax,8),%xmm3
movapd %xmm3,%xmm4
mulsd  %xmm3,%xmm3
addsd  %xmm5,%xmm3
movsd  0x8(%rsi,%rax,8),%xmm5
unpcklpd %xmm2,%xmm4
unpcklpd %xmm2,%xmm2
mulpd  %xmm4,%xmm2
addpd  %xmm0,%xmm2
movapd %xmm5,%xmm0
unpcklpd %xmm6,%xmm0
unpcklpd %xmm6,%xmm6
mulpd  %xmm0,%xmm6
movapd %xmm6,%xmm0
addpd  %xmm2,%xmm0
mulsd  %xmm5,%xmm5
addsd  %xmm3,%xmm5
add    $0xfffffffffffffff0,%rcx
add    $0x2,%rax
cmp    %eax,%r8d
jne    1160 <cosine_distance+0x50>
addpd  %xmm2,%xmm6
sub    %rcx,%rdi
sub    %rcx,%rsi
test   $0x1,%dl
je     11f2 <cosine_distance+0xe2>
movsd  (%rsi),%xmm2
movsd  (%rdi),%xmm6
movapd %xmm2,%xmm3
unpcklpd %xmm6,%xmm3
unpcklpd %xmm6,%xmm6
mulpd  %xmm3,%xmm6
addpd  %xmm0,%xmm6
mulsd  %xmm2,%xmm2
addsd  %xmm2,%xmm5
movapd %xmm6,%xmm0
unpckhpd %xmm6,%xmm0
ucomisd %xmm1,%xmm0
movapd %xmm6,0x10(%rsp)
jb     120f <cosine_distance+0xff>
xorps  %xmm2,%xmm2
sqrtsd %xmm0,%xmm2
jmp    1222 <cosine_distance+0x112>
movapd %xmm5,(%rsp)
call   1030 <sqrt@plt>
movapd (%rsp),%xmm5
movapd %xmm0,%xmm2
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm5
jb     1235 <cosine_distance+0x125>
xorps  %xmm0,%xmm0
sqrtsd %xmm5,%xmm0
jmp    1248 <cosine_distance+0x138>
movapd %xmm5,%xmm0
movsd  %xmm2,(%rsp)
call   1030 <sqrt@plt>
movsd  (%rsp),%xmm2
movapd 0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm2
divsd  %xmm2,%xmm1
movapd %xmm1,%xmm0
add    $0x28,%rsp
ret

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

LLVM IR (-O2):
define double @cosine_distance(double* nocapture %0, double* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %29
5: ; preds = %3
%6 = and i32 %2, 1
%7 = icmp eq i32 %2, 1
br i1 %7, label %12, label %8
8: ; preds = %5
%9 = and i32 %2, -2
br label %38
10: ; preds = %38
%11 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %57, <2 x double> %59, <2 x double> %50)
br label %12
12: ; preds = %10, %5
%13 = phi <2 x double> [ undef, %5 ], [ %11, %10 ]
%14 = phi double [ undef, %5 ], [ %61, %10 ]
%15 = phi double [ 0.000000e+00, %5 ], [ %61, %10 ]
%16 = phi double* [ %0, %5 ], [ %62, %10 ]
%17 = phi double* [ %1, %5 ], [ %63, %10 ]
%18 = phi <2 x double> [ zeroinitializer, %5 ], [ %60, %10 ]
%19 = icmp eq i32 %6, 0
br i1 %19, label %29, label %20
20: ; preds = %12
%21 = load double, double* %16
%22 = load double, double* %17
%23 = insertelement <2 x double> poison, double %21, i64 0
%24 = shufflevector <2 x double> %23, <2 x double> poison, <2 x i32> zeroinitializer
%25 = insertelement <2 x double> poison, double %22, i64 0
%26 = insertelement <2 x double> %25, double %21, i64 1
%27 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %24, <2 x double> %26, <2 x double> %18)
%28 = tail call double @llvm.fmuladd.f64(double %22, double %22, double %15)
br label %29
29: ; preds = %20, %12, %3
%30 = phi double [ 0.000000e+00, %3 ], [ %14, %12 ], [ %28, %20 ]
%31 = phi <2 x double> [ zeroinitializer, %3 ], [ %13, %12 ], [ %27, %20 ]
%32 = extractelement <2 x double> %31, i64 1
%33 = tail call double @sqrt(double %32)
%34 = tail call double @sqrt(double %30)
%35 = fmul double %33, %34
%36 = extractelement <2 x double> %31, i64 0
%37 = fdiv double %36, %35
ret double %37
38: ; preds = %38, %8
%39 = phi double [ 0.000000e+00, %8 ], [ %61, %38 ]
%40 = phi double* [ %0, %8 ], [ %62, %38 ]
%41 = phi double* [ %1, %8 ], [ %63, %38 ]
%42 = phi <2 x double> [ zeroinitializer, %8 ], [ %60, %38 ]
%43 = phi i32 [ 0, %8 ], [ %64, %38 ]
%44 = load double, double* %40
%45 = load double, double* %41
%46 = insertelement <2 x double> poison, double %44, i64 0
%47 = shufflevector <2 x double> %46, <2 x double> poison, <2 x i32> zeroinitializer
%48 = insertelement <2 x double> poison, double %45, i64 0
%49 = insertelement <2 x double> %48, double %44, i64 1
%50 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %47, <2 x double> %49, <2 x double> %42)
%51 = tail call double @llvm.fmuladd.f64(double %45, double %45, double %39)
%52 = getelementptr inbounds double, double* %40, i64 1
%53 = getelementptr inbounds double, double* %41, i64 1
%54 = load double, double* %52
%55 = load double, double* %53
%56 = insertelement <2 x double> poison, double %54, i64 0
%57 = shufflevector <2 x double> %56, <2 x double> poison, <2 x i32> zeroinitializer
%58 = insertelement <2 x double> poison, double %55, i64 0
%59 = insertelement <2 x double> %58, double %54, i64 1
%60 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %57, <2 x double> %59, <2 x double> %50)
%61 = tail call double @llvm.fmuladd.f64(double %55, double %55, double %51)
%62 = getelementptr inbounds double, double* %40, i64 2
%63 = getelementptr inbounds double, double* %41, i64 2
%64 = add i32 %43, 2
%65 = icmp eq i32 %64, %9
br i1 %65, label %10, label %38
}