#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_similarity(double *x, double *y, int n_dims) {
  double dot = 0.0, denom_x = 0.0, denom_y = 0.0;
  int i;
  for (i = 0; i < n_dims; i++) {
    dot += x[i] * y[i];
    denom_x += x[i] * x[i];
    denom_y += y[i] * y[i];
  }
  return dot / (sqrt(denom_x) * sqrt(denom_y));
}


x86 assembly (clang, -O0):
<cosine_similarity>:
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    11e0 <cosine_similarity+0xd0>
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x30(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1142 <cosine_similarity+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_similarity>:
sub    $0x28,%rsp
xorpd  %xmm1,%xmm1
test   %edx,%edx
jle    113a <cosine_similarity+0x2a>
mov    %edx,%eax
cmp    $0x1,%edx
jne    1147 <cosine_similarity+0x37>
xorpd  %xmm0,%xmm0
xorpd  %xmm5,%xmm5
xor    %ecx,%ecx
test   $0x1,%al
jne    11c3 <cosine_similarity+0xb3>
jmp    11e9 <cosine_similarity+0xd9>
xorpd  %xmm6,%xmm6
xorpd  %xmm5,%xmm5
jmp    11e9 <cosine_similarity+0xd9>
mov    %eax,%edx
and    $0xfffffffe,%edx
xorpd  %xmm0,%xmm0
xorpd  %xmm5,%xmm5
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
movsd  (%rdi,%rcx,8),%xmm2
movsd  0x8(%rdi,%rcx,8),%xmm6
movsd  (%rsi,%rcx,8),%xmm3
movapd %xmm3,%xmm4
mulsd  %xmm3,%xmm3
addsd  %xmm5,%xmm3
movsd  0x8(%rsi,%rcx,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    $0x2,%rcx
cmp    %rcx,%rdx
jne    1160 <cosine_similarity+0x50>
addpd  %xmm2,%xmm6
test   $0x1,%al
je     11e9 <cosine_similarity+0xd9>
movsd  (%rsi,%rcx,8),%xmm2
movsd  (%rdi,%rcx,8),%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     1206 <cosine_similarity+0xf6>
xorps  %xmm2,%xmm2
sqrtsd %xmm0,%xmm2
jmp    1219 <cosine_similarity+0x109>
movapd %xmm5,(%rsp)
call   1030 <sqrt@plt>
movapd (%rsp),%xmm5
movapd %xmm0,%xmm2
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm5
jb     122c <cosine_similarity+0x11c>
xorps  %xmm0,%xmm0
sqrtsd %xmm5,%xmm0
jmp    123f <cosine_similarity+0x12f>
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_similarity(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 = %52, %3
%12 = load i32, i32* %10
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %55
15: ; preds = %11
%16 = load double*, double** %4
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds double, double* %16, i64 %18
%20 = load double, double* %19
%21 = load double*, double** %5
%22 = load i32, i32* %10
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds double, double* %21, i64 %23
%25 = load double, double* %24
%26 = load double, double* %7
%27 = call double @llvm.fmuladd.f64(double %20, double %25, double %26)
store double %27, double* %7
%28 = load double*, double** %4
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
%32 = load double, double* %31
%33 = load double*, double** %4
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds double, double* %33, i64 %35
%37 = load double, double* %36
%38 = load double, double* %8
%39 = call double @llvm.fmuladd.f64(double %32, double %37, double %38)
store double %39, double* %8
%40 = load double*, double** %5
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds double, double* %40, i64 %42
%44 = load double, double* %43
%45 = load double*, double** %5
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds double, double* %45, i64 %47
%49 = load double, double* %48
%50 = load double, double* %9
%51 = call double @llvm.fmuladd.f64(double %44, double %49, double %50)
store double %51, double* %9
br label %52
52: ; preds = %15
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %11
55: ; preds = %11
%56 = load double, double* %7
%57 = load double, double* %8
%58 = call double @sqrt(double %57)
%59 = load double, double* %9
%60 = call double @sqrt(double %59)
%61 = fmul double %58, %60
%62 = fdiv double %56, %61
ret double %62
}

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