double rec_reduce(double *x, int size) {
  double tmp1, tmp2;
  if (size == 1) {
    return x[0];
  } else if (size == 2) {
    return x[0] + x[1];
  } else {
    tmp1 = rec_reduce(x, size / 2);
    tmp2 = rec_reduce(x + size / 2, size / 2 + size % 2);
    return tmp1 + tmp2;
  }
}


x86 assembly (clang, -O0):
<rec_reduce>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    113b <rec_reduce+0x2b>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c6 <rec_reduce+0xb6>
cmpl   $0x2,-0x14(%rbp)
jne    1160 <rec_reduce+0x50>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
addsd  0x8(%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11c6 <rec_reduce+0xb6>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1030 <rec_reduce@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0x14(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
add    %edx,%esi
call   1030 <rec_reduce@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rec_reduce>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
cmp    $0x2,%esi
je     112d <rec_reduce+0x1d>
mov    %esi,%ebx
cmp    $0x1,%esi
jne    113a <rec_reduce+0x2a>
movsd  (%r14),%xmm0
jmp    116f <rec_reduce+0x5f>
movsd  (%r14),%xmm0
addsd  0x8(%r14),%xmm0
jmp    116f <rec_reduce+0x5f>
mov    %ebx,%ebp
shr    $0x1f,%ebp
add    %ebx,%ebp
mov    %ebp,%r15d
sar    %r15d
mov    %r14,%rdi
mov    %r15d,%esi
call   1030 <rec_reduce@plt>
movsd  %xmm0,(%rsp)
movslq %r15d,%rsi
lea    (%r14,%rsi,8),%rdi
and    $0xfffffffe,%ebp
sub    %ebp,%ebx
add    %ebx,%esi
call   1030 <rec_reduce@plt>
addsd  (%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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