double sum(double *v, int low, int high) {
  if (low == high)
    return v[low];
  int mid = (low + high) / 2;
  return sum(v, low, mid) + sum(v, mid + 1, high);
}


x86 assembly (clang, -O0):
<sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1145 <sum+0x35>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    118d <sum+0x7d>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1030 <sum@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%edx
call   1030 <sum@plt>
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
cmp    %edx,%esi
jne    112a <sum+0x1a>
movslq %esi,%rax
movsd  (%r14,%rax,8),%xmm0
jmp    115d <sum+0x4d>
mov    %edx,%ebx
lea    (%rbx,%rsi,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
mov    %r14,%rdi
mov    %ebp,%edx
call   1030 <sum@plt>
movsd  %xmm0,0x8(%rsp)
add    $0x1,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <sum@plt>
addsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define double @sum(double* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, %2
br i1 %4, label %5, label %11
5: ; preds = %3
%6 = sext i32 %1 to i64
%7 = getelementptr inbounds double, double* %0, i64 %6
%8 = load double, double* %7
br label %9
9: ; preds = %5, %11
%10 = phi double [ %17, %11 ], [ %8, %5 ]
ret double %10
11: ; preds = %3
%12 = add nsw i32 %2, %1
%13 = sdiv i32 %12, 2
%14 = tail call double @sum(double* %0, i32 %1, i32 %13)
%15 = add nsw i32 %13, 1
%16 = tail call double @sum(double* %0, i32 %15, i32 %2)
%17 = fadd double %14, %16
br label %9
}