#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 = n / 2;
  double res1;
  double res2;
  if (n == 0)
    return 0;
  if (n == 1)
    return *a;
  if (n < 1000) {
    return sum(a, half) + sum(a + half, n - half);
  } else {
#pragma omp task shared(res1)
    res1 = sum(a, half);
#pragma omp task shared(res2)
    res2 = sum(a + half, n - half);
#pragma omp taskwait
    res1 = res1 + res2;
    return res1;
  }
}


x86 assembly (clang, -O0):
<sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1144 <sum+0x34>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <sum+0xef>
cmpq   $0x1,-0x18(%rbp)
jne    1161 <sum+0x51>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <sum+0xef>
cmpq   $0x3e8,-0x18(%rbp)
jae    11b3 <sum+0xa3>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <sum@plt>
movsd  %xmm0,-0x38(%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  -0x38(%rbp),%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11ff <sum+0xef>
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>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%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
%7 = alloca double
%8 = alloca double
store double* %0, double** %4
store i64 %1, i64* %5
%9 = load i64, i64* %5
%10 = udiv i64 %9, 2
store i64 %10, i64* %6
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store double 0.000000e+00, double* %3
br label %50
14: ; preds = %2
%15 = load i64, i64* %5
%16 = icmp eq i64 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load double*, double** %4
%19 = load double, double* %18
store double %19, double* %3
br label %50
20: ; preds = %14
%21 = load i64, i64* %5
%22 = icmp ult i64 %21, 1000
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = load double*, double** %4
%25 = load i64, i64* %6
%26 = call double @sum(double* %24, i64 %25)
%27 = load double*, double** %4
%28 = load i64, i64* %6
%29 = getelementptr inbounds double, double* %27, i64 %28
%30 = load i64, i64* %5
%31 = load i64, i64* %6
%32 = sub i64 %30, %31
%33 = call double @sum(double* %29, i64 %32)
%34 = fadd double %26, %33
store double %34, double* %3
br label %50
35: ; preds = %20
%36 = load double*, double** %4
%37 = load i64, i64* %6
%38 = call double @sum(double* %36, i64 %37)
store double %38, double* %7
%39 = load double*, double** %4
%40 = load i64, i64* %6
%41 = getelementptr inbounds double, double* %39, i64 %40
%42 = load i64, i64* %5
%43 = load i64, i64* %6
%44 = sub i64 %42, %43
%45 = call double @sum(double* %41, i64 %44)
store double %45, double* %8
%46 = load double, double* %7
%47 = load double, double* %8
%48 = fadd double %46, %47
store double %48, double* %7
%49 = load double, double* %7
store double %49, double* %3
br label %50
50: ; preds = %35, %23, %17, %13
%51 = load double, double* %3
ret double %51
}

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