double sum_omp_tasks(double *v, int low, int high) {
  if (low == high)
    return v[low];
  double sum_left, sum_right;
  int mid = (low + high) / 2;
#pragma omp task shared(sum_left)
  sum_left = sum_omp_tasks(v, low, mid);
#pragma omp task shared(sum_right)
  sum_right = sum_omp_tasks(v, mid + 1, high);
#pragma omp taskwait
  return sum_left + sum_right;
}


x86 assembly (clang, -O0):
<sum_omp_tasks>:
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_omp_tasks+0x35>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1190 <sum_omp_tasks+0x80>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1030 <sum_omp_tasks@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%edx
call   1030 <sum_omp_tasks@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):
<sum_omp_tasks>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
cmp    %edx,%esi
jne    112a <sum_omp_tasks+0x1a>
movslq %esi,%rax
movsd  (%r14,%rax,8),%xmm0
jmp    115d <sum_omp_tasks+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_omp_tasks@plt>
movsd  %xmm0,0x8(%rsp)
add    $0x1,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <sum_omp_tasks@plt>
addsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define double @sum_omp_tasks(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_omp_tasks(double* %0, i32 %1, i32 %13)
%15 = add nsw i32 %13, 1
%16 = tail call double @sum_omp_tasks(double* %0, i32 %15, i32 %2)
%17 = fadd double %14, %16
br label %9
}