void grid_gen_corners(long n, const double *restrict vals,
                      double *restrict corners) {
  long i;
  if (n == 1) {
    corners[0] = vals[0];
    corners[1] = vals[0];
  } else {
    for (i = 0; i < n - 1; ++i)
      corners[i + 1] = 0.5 * (vals[i] + vals[i + 1]);
    corners[0] = 2 * vals[0] - corners[1];
    corners[n] = 2 * vals[n - 1] - corners[n - 1];
  }
}


x86 assembly (clang, -O0):
<grid_gen_corners>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x1,-0x8(%rbp)
jne    1141 <grid_gen_corners+0x41>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,0x8(%rax)
jmp    122a <grid_gen_corners+0x12a>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jge    11a4 <grid_gen_corners+0xa4>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
addsd  0x8(%rax,%rcx,8),%xmm1
movsd  0xe7f(%rip),%xmm0
mulsd  %xmm1,%xmm0
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsd  %xmm0,0x8(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1149 <grid_gen_corners+0x49>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x18(%rbp),%rax
movsd  0x8(%rax),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
mov    -0x18(%rbp),%rax
movsd  %xmm0,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    $0x1,%rcx
movsd  (%rax,%rcx,8),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
addsd  %xmm0,%xmm0
addsd  %xmm1,%xmm0
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
pop    %rbp
ret

x86 assembly (clang, -O2):
<grid_gen_corners>:
cmp    $0x1,%rdi
jne    1119 <grid_gen_corners+0x19>
movsd  (%rsi),%xmm0
movsd  %xmm0,(%rdx)
mov    $0x1,%edi
movsd  %xmm0,(%rdx,%rdi,8)
ret
movsd  (%rsi),%xmm0
jle    125f <grid_gen_corners+0x15f>
lea    -0x1(%rdi),%r8
cmp    $0x4,%r8
jae    1138 <grid_gen_corners+0x38>
xor    %eax,%eax
movapd %xmm0,%xmm1
jmp    1227 <grid_gen_corners+0x127>
mov    %r8,%rax
and    $0xfffffffffffffffc,%rax
unpcklpd %xmm0,%xmm1
lea    -0x4(%rax),%rcx
mov    %rcx,%r9
shr    $0x2,%r9
add    $0x1,%r9
test   %rcx,%rcx
je     1282 <grid_gen_corners+0x182>
mov    %r9,%r10
and    $0xfffffffffffffffe,%r10
mov    $0x1,%ecx
movapd 0xe91(%rip),%xmm2
nop
movupd (%rsi,%rcx,8),%xmm3
movupd 0x10(%rsi,%rcx,8),%xmm4
shufpd $0x1,%xmm3,%xmm1
addpd  %xmm3,%xmm1
shufpd $0x1,%xmm4,%xmm3
addpd  %xmm4,%xmm3
mulpd  %xmm2,%xmm1
mulpd  %xmm2,%xmm3
movupd %xmm1,(%rdx,%rcx,8)
movupd %xmm3,0x10(%rdx,%rcx,8)
movupd 0x20(%rsi,%rcx,8),%xmm3
movupd 0x30(%rsi,%rcx,8),%xmm1
shufpd $0x1,%xmm3,%xmm4
addpd  %xmm3,%xmm4
shufpd $0x1,%xmm1,%xmm3
addpd  %xmm1,%xmm3
mulpd  %xmm2,%xmm4
mulpd  %xmm2,%xmm3
movupd %xmm4,0x20(%rdx,%rcx,8)
movupd %xmm3,0x30(%rdx,%rcx,8)
add    $0x8,%rcx
add    $0xfffffffffffffffe,%r10
jne    1170 <grid_gen_corners+0x70>
test   $0x1,%r9b
je     121e <grid_gen_corners+0x11e>
movupd (%rsi,%rcx,8),%xmm2
movupd 0x10(%rsi,%rcx,8),%xmm3
shufpd $0x1,%xmm2,%xmm1
addpd  %xmm2,%xmm1
shufpd $0x1,%xmm3,%xmm2
addpd  %xmm3,%xmm2
movapd 0xdf9(%rip),%xmm4
mulpd  %xmm4,%xmm1
mulpd  %xmm4,%xmm2
movupd %xmm1,(%rdx,%rcx,8)
movupd %xmm2,0x10(%rdx,%rcx,8)
movapd %xmm3,%xmm1
cmp    %rax,%r8
je     125f <grid_gen_corners+0x15f>
unpckhpd %xmm1,%xmm1
add    $0x1,%rax
movsd  0xddd(%rip),%xmm2
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsd  (%rsi,%rax,8),%xmm3
addsd  %xmm3,%xmm1
mulsd  %xmm2,%xmm1
movsd  %xmm1,(%rdx,%rax,8)
add    $0x1,%rax
movapd %xmm3,%xmm1
cmp    %rax,%rdi
jne    1240 <grid_gen_corners+0x140>
addsd  %xmm0,%xmm0
subsd  0x8(%rdx),%xmm0
movsd  %xmm0,(%rdx)
movsd  -0x8(%rsi,%rdi,8),%xmm0
addsd  %xmm0,%xmm0
subsd  -0x8(%rdx,%rdi,8),%xmm0
movsd  %xmm0,(%rdx,%rdi,8)
ret
mov    $0x1,%ecx
test   $0x1,%r9b
jne    11e2 <grid_gen_corners+0xe2>
jmp    121e <grid_gen_corners+0x11e>

LLVM IR (-O0):
define void @grid_gen_corners(i64 %0, double* noalias %1, double* noalias %2) {
%4 = alloca i64
%5 = alloca double*
%6 = alloca double*
%7 = alloca i64
store i64 %0, i64* %4
store double* %1, double** %5
store double* %2, double** %6
%8 = load i64, i64* %4
%9 = icmp eq i64 %8, 1
br i1 %9, label %10, label %21
10: ; preds = %3
%11 = load double*, double** %5
%12 = getelementptr inbounds double, double* %11, i64 0
%13 = load double, double* %12
%14 = load double*, double** %6
%15 = getelementptr inbounds double, double* %14, i64 0
store double %13, double* %15
%16 = load double*, double** %5
%17 = getelementptr inbounds double, double* %16, i64 0
%18 = load double, double* %17
%19 = load double*, double** %6
%20 = getelementptr inbounds double, double* %19, i64 1
store double %18, double* %20
br label %72
21: ; preds = %3
store i64 0, i64* %7
br label %22
22: ; preds = %43, %21
%23 = load i64, i64* %7
%24 = load i64, i64* %4
%25 = sub nsw i64 %24, 1
%26 = icmp slt i64 %23, %25
br i1 %26, label %27, label %46
27: ; preds = %22
%28 = load double*, double** %5
%29 = load i64, i64* %7
%30 = getelementptr inbounds double, double* %28, i64 %29
%31 = load double, double* %30
%32 = load double*, double** %5
%33 = load i64, i64* %7
%34 = add nsw i64 %33, 1
%35 = getelementptr inbounds double, double* %32, i64 %34
%36 = load double, double* %35
%37 = fadd double %31, %36
%38 = fmul double 5.000000e-01, %37
%39 = load double*, double** %6
%40 = load i64, i64* %7
%41 = add nsw i64 %40, 1
%42 = getelementptr inbounds double, double* %39, i64 %41
store double %38, double* %42
br label %43
43: ; preds = %27
%44 = load i64, i64* %7
%45 = add nsw i64 %44, 1
store i64 %45, i64* %7
br label %22
46: ; preds = %22
%47 = load double*, double** %5
%48 = getelementptr inbounds double, double* %47, i64 0
%49 = load double, double* %48
%50 = load double*, double** %6
%51 = getelementptr inbounds double, double* %50, i64 1
%52 = load double, double* %51
%53 = fneg double %52
%54 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %49, double %53)
%55 = load double*, double** %6
%56 = getelementptr inbounds double, double* %55, i64 0
store double %54, double* %56
%57 = load double*, double** %5
%58 = load i64, i64* %4
%59 = sub nsw i64 %58, 1
%60 = getelementptr inbounds double, double* %57, i64 %59
%61 = load double, double* %60
%62 = load double*, double** %6
%63 = load i64, i64* %4
%64 = sub nsw i64 %63, 1
%65 = getelementptr inbounds double, double* %62, i64 %64
%66 = load double, double* %65
%67 = fneg double %66
%68 = call double @llvm.fmuladd.f64(double 2.000000e+00, double %61, double %67)
%69 = load double*, double** %6
%70 = load i64, i64* %4
%71 = getelementptr inbounds double, double* %69, i64 %70
store double %68, double* %71
br label %72
72: ; preds = %46, %10
ret void
}

LLVM IR (-O2):
define void @grid_gen_corners(i64 %0, double* noalias nocapture %1, double* noalias nocapture %2) {
%4 = icmp eq i64 %0, 1
br i1 %4, label %93, label %5
5: ; preds = %3
%6 = add i64 %0, -1
%7 = icmp sgt i64 %0, 1
%8 = load double, double* %1
br i1 %7, label %9, label %105
9: ; preds = %5
%10 = icmp ult i64 %6, 4
br i1 %10, label %90, label %11
11: ; preds = %9
%12 = and i64 %6, -4
%13 = insertelement <2 x double> poison, double %8, i64 1
%14 = add i64 %12, -4
%15 = lshr exact i64 %14, 2
%16 = add nuw nsw i64 %15, 1
%17 = and i64 %16, 1
%18 = icmp eq i64 %14, 0
br i1 %18, label %64, label %19
19: ; preds = %11
%20 = and i64 %16, 9223372036854775806
br label %21
21: ; preds = %21, %19
%22 = phi i64 [ 0, %19 ], [ %59, %21 ]
%23 = phi <2 x double> [ %13, %19 ], [ %48, %21 ]
%24 = phi i64 [ 0, %19 ], [ %60, %21 ]
%25 = or i64 %22, 1
%26 = getelementptr inbounds double, double* %1, i64 %25
%27 = bitcast double* %26 to <2 x double>*
%28 = load <2 x double>, <2 x double>* %27
%29 = getelementptr inbounds double, double* %26, i64 2
%30 = bitcast double* %29 to <2 x double>*
%31 = load <2 x double>, <2 x double>* %30
%32 = shufflevector <2 x double> %23, <2 x double> %28, <2 x i32> <i32 1, i32 2>
%33 = shufflevector <2 x double> %28, <2 x double> %31, <2 x i32> <i32 1, i32 2>
%34 = fadd <2 x double> %32, %28
%35 = fadd <2 x double> %33, %31
%36 = fmul <2 x double> %34, <double 5.000000e-01, double 5.000000e-01>
%37 = fmul <2 x double> %35, <double 5.000000e-01, double 5.000000e-01>
%38 = getelementptr inbounds double, double* %2, i64 %25
%39 = bitcast double* %38 to <2 x double>*
store <2 x double> %36, <2 x double>* %39
%40 = getelementptr inbounds double, double* %38, i64 2
%41 = bitcast double* %40 to <2 x double>*
store <2 x double> %37, <2 x double>* %41
%42 = or i64 %22, 5
%43 = getelementptr inbounds double, double* %1, i64 %42
%44 = bitcast double* %43 to <2 x double>*
%45 = load <2 x double>, <2 x double>* %44
%46 = getelementptr inbounds double, double* %43, i64 2
%47 = bitcast double* %46 to <2 x double>*
%48 = load <2 x double>, <2 x double>* %47
%49 = shufflevector <2 x double> %31, <2 x double> %45, <2 x i32> <i32 1, i32 2>
%50 = shufflevector <2 x double> %45, <2 x double> %48, <2 x i32> <i32 1, i32 2>
%51 = fadd <2 x double> %49, %45
%52 = fadd <2 x double> %50, %48
%53 = fmul <2 x double> %51, <double 5.000000e-01, double 5.000000e-01>
%54 = fmul <2 x double> %52, <double 5.000000e-01, double 5.000000e-01>
%55 = getelementptr inbounds double, double* %2, i64 %42
%56 = bitcast double* %55 to <2 x double>*
store <2 x double> %53, <2 x double>* %56
%57 = getelementptr inbounds double, double* %55, i64 2
%58 = bitcast double* %57 to <2 x double>*
store <2 x double> %54, <2 x double>* %58
%59 = add nuw i64 %22, 8
%60 = add i64 %24, 2
%61 = icmp eq i64 %60, %20
br i1 %61, label %62, label %21
62: ; preds = %21
%63 = or i64 %59, 1
br label %64
64: ; preds = %62, %11
%65 = phi <2 x double> [ undef, %11 ], [ %48, %62 ]
%66 = phi i64 [ 1, %11 ], [ %63, %62 ]
%67 = phi <2 x double> [ %13, %11 ], [ %48, %62 ]
%68 = icmp eq i64 %17, 0
br i1 %68, label %86, label %69
69: ; preds = %64
%70 = getelementptr inbounds double, double* %1, i64 %66
%71 = bitcast double* %70 to <2 x double>*
%72 = load <2 x double>, <2 x double>* %71
%73 = getelementptr inbounds double, double* %70, i64 2
%74 = bitcast double* %73 to <2 x double>*
%75 = load <2 x double>, <2 x double>* %74
%76 = shufflevector <2 x double> %67, <2 x double> %72, <2 x i32> <i32 1, i32 2>
%77 = shufflevector <2 x double> %72, <2 x double> %75, <2 x i32> <i32 1, i32 2>
%78 = fadd <2 x double> %76, %72
%79 = fadd <2 x double> %77, %75
%80 = fmul <2 x double> %78, <double 5.000000e-01, double 5.000000e-01>
%81 = fmul <2 x double> %79, <double 5.000000e-01, double 5.000000e-01>
%82 = getelementptr inbounds double, double* %2, i64 %66
%83 = bitcast double* %82 to <2 x double>*
store <2 x double> %80, <2 x double>* %83
%84 = getelementptr inbounds double, double* %82, i64 2
%85 = bitcast double* %84 to <2 x double>*
store <2 x double> %81, <2 x double>* %85
br label %86
86: ; preds = %64, %69
%87 = phi <2 x double> [ %65, %64 ], [ %75, %69 ]
%88 = icmp eq i64 %6, %12
%89 = extractelement <2 x double> %87, i64 1
br i1 %88, label %105, label %90
90: ; preds = %9, %86
%91 = phi double [ %89, %86 ], [ %8, %9 ]
%92 = phi i64 [ %12, %86 ], [ 0, %9 ]
br label %95
93: ; preds = %3
%94 = load double, double* %1
store double %94, double* %2
br label %116
95: ; preds = %90, %95
%96 = phi double [ %100, %95 ], [ %91, %90 ]
%97 = phi i64 [ %98, %95 ], [ %92, %90 ]
%98 = add nuw nsw i64 %97, 1
%99 = getelementptr inbounds double, double* %1, i64 %98
%100 = load double, double* %99
%101 = fadd double %96, %100
%102 = fmul double %101, 5.000000e-01
%103 = getelementptr inbounds double, double* %2, i64 %98
store double %102, double* %103
%104 = icmp eq i64 %98, %6
br i1 %104, label %105, label %95
105: ; preds = %95, %86, %5
%106 = getelementptr inbounds double, double* %2, i64 1
%107 = load double, double* %106
%108 = fneg double %107
%109 = tail call double @llvm.fmuladd.f64(double %8, double 2.000000e+00, double %108)
store double %109, double* %2
%110 = getelementptr inbounds double, double* %1, i64 %6
%111 = load double, double* %110
%112 = getelementptr inbounds double, double* %2, i64 %6
%113 = load double, double* %112
%114 = fneg double %113
%115 = tail call double @llvm.fmuladd.f64(double %111, double 2.000000e+00, double %114)
br label %116
116: ; preds = %105, %93
%117 = phi i64 [ %0, %105 ], [ 1, %93 ]
%118 = phi double [ %115, %105 ], [ %94, %93 ]
%119 = getelementptr inbounds double, double* %2, i64 %117
store double %118, double* %119
ret void
}