#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
double sum(const double *a, size_t n) {
  size_t half;
  if (n == 0)
    return 0;
  if (n == 1)
    return *a;
  half = n / 2;
  return sum(a, half) + sum(a + half, n - half);
}


x86 assembly (clang, -O0):
<sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1138 <sum+0x28>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11a0 <sum+0x90>
cmpq   $0x1,-0x18(%rbp)
jne    1155 <sum+0x45>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11a0 <sum+0x90>
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <sum@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
sub    -0x20(%rbp),%rsi
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   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     1131 <sum+0x21>
mov    %rsi,%rbx
mov    %rdi,%r14
cmp    $0x1,%rsi
jne    1137 <sum+0x27>
movsd  (%r14),%xmm0
jmp    1163 <sum+0x53>
xorpd  %xmm0,%xmm0
jmp    1163 <sum+0x53>
mov    %rbx,%r15
shr    %r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <sum@plt>
movsd  %xmm0,0x8(%rsp)
lea    (%r14,%r15,8),%rdi
sub    %r15,%rbx
mov    %rbx,%rsi
call   1030 <sum@plt>
addsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define double @sum(double* %0, i64 %1) {
switch i64 %1, label %7 [
i64 0, label %5
i64 1, label %3
]
3: ; preds = %2
%4 = load double, double* %0
br label %5
5: ; preds = %3, %2, %7
%6 = phi double [ %13, %7 ], [ %4, %3 ], [ 0.000000e+00, %2 ]
ret double %6
7: ; preds = %2
%8 = lshr i64 %1, 1
%9 = tail call double @sum(double* %0, i64 %8)
%10 = getelementptr inbounds double, double* %0, i64 %8
%11 = sub i64 %1, %8
%12 = tail call double @sum(double* %10, i64 %11)
%13 = fadd double %9, %12
br label %5
}