void vector_add_double(const double *a, const double *b, double *c,
                       const unsigned int dim) {
  for (unsigned int i = 0; i < dim; i++) {
    c[i] = a[i] + b[i];
  }
}


x86 assembly (clang, -O0):
<vector_add_double>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1158 <vector_add_double+0x58>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
addsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    111a <vector_add_double+0x1a>
pop    %rbp
ret

x86 assembly (clang, -O2):
<vector_add_double>:
test   %ecx,%ecx
je     12b7 <vector_add_double+0x1b7>
mov    %ecx,%r8d
cmp    $0x4,%ecx
jae    11b3 <vector_add_double+0xb3>
xor    %ecx,%ecx
mov    %rcx,%r9
not    %r9
add    %r8,%r9
mov    %r8,%rax
and    $0x3,%rax
je     1149 <vector_add_double+0x49>
nopl   0x0(%rax,%rax,1)
movsd  (%rdi,%rcx,8),%xmm0
addsd  (%rsi,%rcx,8),%xmm0
movsd  %xmm0,(%rdx,%rcx,8)
add    $0x1,%rcx
add    $0xffffffffffffffff,%rax
jne    1130 <vector_add_double+0x30>
cmp    $0x3,%r9
jb     12b7 <vector_add_double+0x1b7>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rdi,%rcx,8),%xmm0
addsd  (%rsi,%rcx,8),%xmm0
movsd  %xmm0,(%rdx,%rcx,8)
movsd  0x8(%rdi,%rcx,8),%xmm0
addsd  0x8(%rsi,%rcx,8),%xmm0
movsd  %xmm0,0x8(%rdx,%rcx,8)
movsd  0x10(%rdi,%rcx,8),%xmm0
addsd  0x10(%rsi,%rcx,8),%xmm0
movsd  %xmm0,0x10(%rdx,%rcx,8)
movsd  0x18(%rdi,%rcx,8),%xmm0
addsd  0x18(%rsi,%rcx,8),%xmm0
movsd  %xmm0,0x18(%rdx,%rcx,8)
add    $0x4,%rcx
cmp    %rcx,%r8
jne    1160 <vector_add_double+0x60>
jmp    12b7 <vector_add_double+0x1b7>
lea    (%rdx,%r8,8),%rcx
lea    (%rdi,%r8,8),%rax
cmp    %rdx,%rax
seta   %r10b
lea    (%rsi,%r8,8),%rax
cmp    %rdi,%rcx
seta   %r11b
cmp    %rdx,%rax
seta   %al
cmp    %rsi,%rcx
seta   %r9b
xor    %ecx,%ecx
test   %r11b,%r10b
jne    1116 <vector_add_double+0x16>
and    %r9b,%al
jne    1116 <vector_add_double+0x16>
mov    %r8d,%ecx
and    $0xfffffffc,%ecx
lea    -0x4(%rcx),%rax
mov    %rax,%r9
shr    $0x2,%r9
add    $0x1,%r9
test   %rax,%rax
je     12b8 <vector_add_double+0x1b8>
mov    %r9,%r10
and    $0xfffffffffffffffe,%r10
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
movupd (%rdi,%rax,8),%xmm0
movupd 0x10(%rdi,%rax,8),%xmm1
movupd (%rsi,%rax,8),%xmm2
addpd  %xmm0,%xmm2
movupd 0x10(%rsi,%rax,8),%xmm0
addpd  %xmm1,%xmm0
movupd %xmm2,(%rdx,%rax,8)
movupd %xmm0,0x10(%rdx,%rax,8)
movupd 0x20(%rdi,%rax,8),%xmm0
movupd 0x30(%rdi,%rax,8),%xmm1
movupd 0x20(%rsi,%rax,8),%xmm2
addpd  %xmm0,%xmm2
movupd 0x30(%rsi,%rax,8),%xmm0
addpd  %xmm1,%xmm0
movupd %xmm2,0x20(%rdx,%rax,8)
movupd %xmm0,0x30(%rdx,%rax,8)
add    $0x8,%rax
add    $0xfffffffffffffffe,%r10
jne    1220 <vector_add_double+0x120>
test   $0x1,%r9b
je     12ae <vector_add_double+0x1ae>
movupd (%rdi,%rax,8),%xmm0
movupd 0x10(%rdi,%rax,8),%xmm1
movupd (%rsi,%rax,8),%xmm2
addpd  %xmm0,%xmm2
movupd 0x10(%rsi,%rax,8),%xmm0
addpd  %xmm1,%xmm0
movupd %xmm2,(%rdx,%rax,8)
movupd %xmm0,0x10(%rdx,%rax,8)
cmp    %r8,%rcx
jne    1116 <vector_add_double+0x16>
ret
xor    %eax,%eax
test   $0x1,%r9b
jne    1285 <vector_add_double+0x185>
jmp    12ae <vector_add_double+0x1ae>

LLVM IR (-O0):
define void @vector_add_double(double* %0, double* %1, double* %2, i32 %3) {
%5 = alloca double*
%6 = alloca double*
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
store double* %0, double** %5
store double* %1, double** %6
store double* %2, double** %7
store i32 %3, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %30, %4
%11 = load i32, i32* %9
%12 = load i32, i32* %8
%13 = icmp ult i32 %11, %12
br i1 %13, label %14, label %33
14: ; preds = %10
%15 = load double*, double** %5
%16 = load i32, i32* %9
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds double, double* %15, i64 %17
%19 = load double, double* %18
%20 = load double*, double** %6
%21 = load i32, i32* %9
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds double, double* %20, i64 %22
%24 = load double, double* %23
%25 = fadd double %19, %24
%26 = load double*, double** %7
%27 = load i32, i32* %9
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds double, double* %26, i64 %28
store double %25, double* %29
br label %30
30: ; preds = %14
%31 = load i32, i32* %9
%32 = add i32 %31, 1
store i32 %32, i32* %9
br label %10
33: ; preds = %10
ret void
}

LLVM IR (-O2):
define void @vector_add_double(double* nocapture %0, double* nocapture %1, double* nocapture writeonly %2, i32 %3) {
%5 = icmp eq i32 %3, 0
br i1 %5, label %117, label %6
6: ; preds = %4
%7 = zext i32 %3 to i64
%8 = icmp ult i32 %3, 4
br i1 %8, label %96, label %9
9: ; preds = %6
%10 = getelementptr double, double* %2, i64 %7
%11 = getelementptr double, double* %0, i64 %7
%12 = getelementptr double, double* %1, i64 %7
%13 = icmp ugt double* %11, %2
%14 = icmp ugt double* %10, %0
%15 = and i1 %13, %14
%16 = icmp ugt double* %12, %2
%17 = icmp ugt double* %10, %1
%18 = and i1 %16, %17
%19 = or i1 %15, %18
br i1 %19, label %96, label %20
20: ; preds = %9
%21 = and i64 %7, 4294967292
%22 = add nsw i64 %21, -4
%23 = lshr exact i64 %22, 2
%24 = add nuw nsw i64 %23, 1
%25 = and i64 %24, 1
%26 = icmp eq i64 %22, 0
br i1 %26, label %72, label %27
27: ; preds = %20
%28 = and i64 %24, 9223372036854775806
br label %29
29: ; preds = %29, %27
%30 = phi i64 [ 0, %27 ], [ %69, %29 ]
%31 = phi i64 [ 0, %27 ], [ %70, %29 ]
%32 = getelementptr inbounds double, double* %0, i64 %30
%33 = bitcast double* %32 to <2 x double>*
%34 = load <2 x double>, <2 x double>* %33
%35 = getelementptr inbounds double, double* %32, i64 2
%36 = bitcast double* %35 to <2 x double>*
%37 = load <2 x double>, <2 x double>* %36
%38 = getelementptr inbounds double, double* %1, i64 %30
%39 = bitcast double* %38 to <2 x double>*
%40 = load <2 x double>, <2 x double>* %39
%41 = getelementptr inbounds double, double* %38, i64 2
%42 = bitcast double* %41 to <2 x double>*
%43 = load <2 x double>, <2 x double>* %42
%44 = fadd <2 x double> %34, %40
%45 = fadd <2 x double> %37, %43
%46 = getelementptr inbounds double, double* %2, i64 %30
%47 = bitcast double* %46 to <2 x double>*
store <2 x double> %44, <2 x double>* %47
%48 = getelementptr inbounds double, double* %46, i64 2
%49 = bitcast double* %48 to <2 x double>*
store <2 x double> %45, <2 x double>* %49
%50 = or i64 %30, 4
%51 = getelementptr inbounds double, double* %0, i64 %50
%52 = bitcast double* %51 to <2 x double>*
%53 = load <2 x double>, <2 x double>* %52
%54 = getelementptr inbounds double, double* %51, i64 2
%55 = bitcast double* %54 to <2 x double>*
%56 = load <2 x double>, <2 x double>* %55
%57 = getelementptr inbounds double, double* %1, i64 %50
%58 = bitcast double* %57 to <2 x double>*
%59 = load <2 x double>, <2 x double>* %58
%60 = getelementptr inbounds double, double* %57, i64 2
%61 = bitcast double* %60 to <2 x double>*
%62 = load <2 x double>, <2 x double>* %61
%63 = fadd <2 x double> %53, %59
%64 = fadd <2 x double> %56, %62
%65 = getelementptr inbounds double, double* %2, i64 %50
%66 = bitcast double* %65 to <2 x double>*
store <2 x double> %63, <2 x double>* %66
%67 = getelementptr inbounds double, double* %65, i64 2
%68 = bitcast double* %67 to <2 x double>*
store <2 x double> %64, <2 x double>* %68
%69 = add nuw i64 %30, 8
%70 = add i64 %31, 2
%71 = icmp eq i64 %70, %28
br i1 %71, label %72, label %29
72: ; preds = %29, %20
%73 = phi i64 [ 0, %20 ], [ %69, %29 ]
%74 = icmp eq i64 %25, 0
br i1 %74, label %94, label %75
75: ; preds = %72
%76 = getelementptr inbounds double, double* %0, i64 %73
%77 = bitcast double* %76 to <2 x double>*
%78 = load <2 x double>, <2 x double>* %77
%79 = getelementptr inbounds double, double* %76, i64 2
%80 = bitcast double* %79 to <2 x double>*
%81 = load <2 x double>, <2 x double>* %80
%82 = getelementptr inbounds double, double* %1, i64 %73
%83 = bitcast double* %82 to <2 x double>*
%84 = load <2 x double>, <2 x double>* %83
%85 = getelementptr inbounds double, double* %82, i64 2
%86 = bitcast double* %85 to <2 x double>*
%87 = load <2 x double>, <2 x double>* %86
%88 = fadd <2 x double> %78, %84
%89 = fadd <2 x double> %81, %87
%90 = getelementptr inbounds double, double* %2, i64 %73
%91 = bitcast double* %90 to <2 x double>*
store <2 x double> %88, <2 x double>* %91
%92 = getelementptr inbounds double, double* %90, i64 2
%93 = bitcast double* %92 to <2 x double>*
store <2 x double> %89, <2 x double>* %93
br label %94
94: ; preds = %72, %75
%95 = icmp eq i64 %21, %7
br i1 %95, label %117, label %96
96: ; preds = %9, %6, %94
%97 = phi i64 [ 0, %9 ], [ 0, %6 ], [ %21, %94 ]
%98 = xor i64 %97, -1
%99 = add nsw i64 %98, %7
%100 = and i64 %7, 3
%101 = icmp eq i64 %100, 0
br i1 %101, label %114, label %102
102: ; preds = %96, %102
%103 = phi i64 [ %111, %102 ], [ %97, %96 ]
%104 = phi i64 [ %112, %102 ], [ 0, %96 ]
%105 = getelementptr inbounds double, double* %0, i64 %103
%106 = load double, double* %105
%107 = getelementptr inbounds double, double* %1, i64 %103
%108 = load double, double* %107
%109 = fadd double %106, %108
%110 = getelementptr inbounds double, double* %2, i64 %103
store double %109, double* %110
%111 = add nuw nsw i64 %103, 1
%112 = add i64 %104, 1
%113 = icmp eq i64 %112, %100
br i1 %113, label %114, label %102
114: ; preds = %102, %96
%115 = phi i64 [ %97, %96 ], [ %111, %102 ]
%116 = icmp ult i64 %99, 3
br i1 %116, label %117, label %118
117: ; preds = %114, %118, %94, %4
ret void
118: ; preds = %114, %118
%119 = phi i64 [ %147, %118 ], [ %115, %114 ]
%120 = getelementptr inbounds double, double* %0, i64 %119
%121 = load double, double* %120
%122 = getelementptr inbounds double, double* %1, i64 %119
%123 = load double, double* %122
%124 = fadd double %121, %123
%125 = getelementptr inbounds double, double* %2, i64 %119
store double %124, double* %125
%126 = add nuw nsw i64 %119, 1
%127 = getelementptr inbounds double, double* %0, i64 %126
%128 = load double, double* %127
%129 = getelementptr inbounds double, double* %1, i64 %126
%130 = load double, double* %129
%131 = fadd double %128, %130
%132 = getelementptr inbounds double, double* %2, i64 %126
store double %131, double* %132
%133 = add nuw nsw i64 %119, 2
%134 = getelementptr inbounds double, double* %0, i64 %133
%135 = load double, double* %134
%136 = getelementptr inbounds double, double* %1, i64 %133
%137 = load double, double* %136
%138 = fadd double %135, %137
%139 = getelementptr inbounds double, double* %2, i64 %133
store double %138, double* %139
%140 = add nuw nsw i64 %119, 3
%141 = getelementptr inbounds double, double* %0, i64 %140
%142 = load double, double* %141
%143 = getelementptr inbounds double, double* %1, i64 %140
%144 = load double, double* %143
%145 = fadd double %142, %144
%146 = getelementptr inbounds double, double* %2, i64 %140
store double %145, double* %146
%147 = add nuw nsw i64 %119, 4
%148 = icmp eq i64 %147, %7
br i1 %148, label %117, label %118
}