void sum_b(const double *restrict input, double *restrict output, int len) {
  output[0] = input[0];
  for (int i = 1; i < len; i++)
    output[i] = input[2 * i] + input[2 * i - 1];
}


x86 assembly (clang, -O0):
<sum_b>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1174 <sum_b+0x74>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <sum_b+0x26>
pop    %rbp
ret

x86 assembly (clang, -O2):
<sum_b>:
movsd  (%rdi),%xmm0
movsd  %xmm0,(%rsi)
cmp    $0x2,%edx
jl     11bb <sum_b+0xbb>
mov    %edx,%r9d
lea    -0x1(%r9),%r8
mov    $0x1,%ecx
cmp    $0x4,%r8
jb     1191 <sum_b+0x91>
mov    %r8,%r10
and    $0xfffffffffffffffc,%r10
lea    0x1(%r10),%rcx
lea    0x28(%rdi),%rax
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movupd -0x20(%rax),%xmm0
movupd -0x10(%rax),%xmm1
movupd (%rax),%xmm2
movupd 0x10(%rax),%xmm3
movapd %xmm0,%xmm4
unpcklpd %xmm1,%xmm4
movapd %xmm2,%xmm5
unpcklpd %xmm3,%xmm5
unpckhpd %xmm1,%xmm0
addpd  %xmm4,%xmm0
unpckhpd %xmm3,%xmm2
addpd  %xmm5,%xmm2
movupd %xmm0,0x8(%rsi,%rdx,8)
movupd %xmm2,0x18(%rsi,%rdx,8)
add    $0x4,%rdx
add    $0x40,%rax
cmp    %rdx,%r10
jne    1140 <sum_b+0x40>
cmp    %r10,%r8
je     11bb <sum_b+0xbb>
mov    %rcx,%rax
shl    $0x4,%rax
add    %rax,%rdi
nopl   0x0(%rax,%rax,1)
movsd  (%rdi),%xmm0
addsd  -0x8(%rdi),%xmm0
movsd  %xmm0,(%rsi,%rcx,8)
add    $0x1,%rcx
add    $0x10,%rdi
cmp    %rcx,%r9
jne    11a0 <sum_b+0xa0>
ret

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

LLVM IR (-O2):
define void @sum_b(double* noalias nocapture %0, double* noalias nocapture writeonly %1, i32 %2) {
%4 = load double, double* %0
store double %4, double* %1
%5 = icmp sgt i32 %2, 1
br i1 %5, label %6, label %43
6: ; preds = %3
%7 = zext i32 %2 to i64
%8 = add nsw i64 %7, -1
%9 = icmp ult i64 %8, 4
br i1 %9, label %41, label %10
10: ; preds = %6
%11 = and i64 %8, -4
%12 = or i64 %11, 1
%13 = getelementptr inbounds double, double* %0, i64 -1
%14 = getelementptr inbounds double, double* %0, i64 -1
br label %15
15: ; preds = %15, %10
%16 = phi i64 [ 0, %10 ], [ %37, %15 ]
%17 = or i64 %16, 1
%18 = shl nuw nsw i64 %17, 1
%19 = shl i64 %16, 1
%20 = or i64 %19, 6
%21 = getelementptr inbounds double, double* %13, i64 %18
%22 = bitcast double* %21 to <4 x double>*
%23 = getelementptr inbounds double, double* %14, i64 %20
%24 = bitcast double* %23 to <4 x double>*
%25 = load <4 x double>, <4 x double>* %22
%26 = load <4 x double>, <4 x double>* %24
%27 = shufflevector <4 x double> %25, <4 x double> poison, <2 x i32> <i32 0, i32 2>
%28 = shufflevector <4 x double> %26, <4 x double> poison, <2 x i32> <i32 0, i32 2>
%29 = shufflevector <4 x double> %25, <4 x double> poison, <2 x i32> <i32 1, i32 3>
%30 = shufflevector <4 x double> %26, <4 x double> poison, <2 x i32> <i32 1, i32 3>
%31 = fadd <2 x double> %29, %27
%32 = fadd <2 x double> %30, %28
%33 = getelementptr inbounds double, double* %1, i64 %17
%34 = bitcast double* %33 to <2 x double>*
store <2 x double> %31, <2 x double>* %34
%35 = getelementptr inbounds double, double* %33, i64 2
%36 = bitcast double* %35 to <2 x double>*
store <2 x double> %32, <2 x double>* %36
%37 = add nuw i64 %16, 4
%38 = icmp eq i64 %37, %11
br i1 %38, label %39, label %15
39: ; preds = %15
%40 = icmp eq i64 %8, %11
br i1 %40, label %43, label %41
41: ; preds = %6, %39
%42 = phi i64 [ 1, %6 ], [ %12, %39 ]
br label %44
43: ; preds = %44, %39, %3
ret void
44: ; preds = %41, %44
%45 = phi i64 [ %54, %44 ], [ %42, %41 ]
%46 = shl nuw nsw i64 %45, 1
%47 = getelementptr inbounds double, double* %0, i64 %46
%48 = load double, double* %47
%49 = add nsw i64 %46, -1
%50 = getelementptr inbounds double, double* %0, i64 %49
%51 = load double, double* %50
%52 = fadd double %48, %51
%53 = getelementptr inbounds double, double* %1, i64 %45
store double %52, double* %53
%54 = add nuw nsw i64 %45, 1
%55 = icmp eq i64 %54, %7
br i1 %55, label %43, label %44
}