#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 */
int omp_get_thread_limit();
double sum(double *, int, int);
double sum_omp(double *v, int low, int high) {
  static int nthreads_used = 1;
  if (low == high)
    return v[low];
  double sum_left, sum_right;
  int mid = (low + high) / 2;
  if (nthreads_used >= omp_get_thread_limit()) {
    return sum(v, low, high);
  }
#pragma omp atomic
  nthreads_used++;
#pragma omp parallel num_threads(2)
  {
#pragma omp sections
    {
#pragma omp section
      sum_left = sum_omp(v, low, mid);
#pragma omp section
      sum_right = sum_omp(v, mid + 1, high);
    }
  }
  return sum_left + sum_right;
}


x86 assembly (clang, -O0):
<sum_omp>:
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    1165 <sum_omp+0x35>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11f5 <sum_omp+0xc5>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    0x2ebc(%rip),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <omp_get_thread_limit@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
cmp    %ecx,%eax
jl     11ac <sum_omp+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <sum@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    11f5 <sum_omp+0xc5>
mov    0x2e86(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e7d(%rip)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1030 <sum_omp@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
add    $0x1,%esi
mov    -0x18(%rbp),%edx
call   1030 <sum_omp@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>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
cmp    %edx,%esi
jne    114b <sum_omp+0x1b>
movslq %ebx,%rax
movsd  (%r14,%rax,8),%xmm0
jmp    1199 <sum_omp+0x69>
mov    %edx,%r15d
mov    0x2ee4(%rip),%ebp
xor    %eax,%eax
call   1040 <omp_get_thread_limit@plt>
cmp    %eax,%ebp
jge    11a4 <sum_omp+0x74>
lea    (%r15,%rbx,1),%eax
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
addl   $0x1,0x2ec5(%rip)
mov    %r14,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1030 <sum_omp@plt>
movsd  %xmm0,(%rsp)
add    $0x1,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
call   1030 <sum_omp@plt>
addsd  (%rsp),%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <sum@plt>

LLVM IR (-O0):
@sum_omp.nthreads_used = internal global i32 1
define double @sum_omp(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 %48
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 i32, i32* @sum_omp.nthreads_used
%26 = call i32 (...) @omp_get_thread_limit()
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %20
%29 = load double*, double** %5
%30 = load i32, i32* %6
%31 = load i32, i32* %7
%32 = call double @sum(double* %29, i32 %30, i32 %31)
store double %32, double* %4
br label %48
33: ; preds = %20
%34 = load i32, i32* @sum_omp.nthreads_used
%35 = add nsw i32 %34, 1
store i32 %35, i32* @sum_omp.nthreads_used
%36 = load double*, double** %5
%37 = load i32, i32* %6
%38 = load i32, i32* %10
%39 = call double @sum_omp(double* %36, i32 %37, i32 %38)
store double %39, double* %8
%40 = load double*, double** %5
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 1
%43 = load i32, i32* %7
%44 = call double @sum_omp(double* %40, i32 %42, i32 %43)
store double %44, double* %9
%45 = load double, double* %8
%46 = load double, double* %9
%47 = fadd double %45, %46
store double %47, double* %4
br label %48
48: ; preds = %33, %28, %14
%49 = load double, double* %4
ret double %49
}

LLVM IR (-O2):
@sum_omp.nthreads_used = internal unnamed_addr global i32 1
define double @sum_omp(double* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, %2
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = sext i32 %1 to i64
%7 = getelementptr inbounds double, double* %0, i64 %6
%8 = load double, double* %7
br label %17
9: ; preds = %3
%10 = add nsw i32 %2, %1
%11 = sdiv i32 %10, 2
%12 = load i32, i32* @sum_omp.nthreads_used
%13 = tail call i32 (...) @omp_get_thread_limit()
%14 = icmp slt i32 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = tail call double @sum(double* %0, i32 %1, i32 %2)
br label %17
17: ; preds = %5, %15, %19
%18 = phi double [ %25, %19 ], [ %8, %5 ], [ %16, %15 ]
ret double %18
19: ; preds = %9
%20 = load i32, i32* @sum_omp.nthreads_used
%21 = add nsw i32 %20, 1
store i32 %21, i32* @sum_omp.nthreads_used
%22 = tail call double @sum_omp(double* %0, i32 %1, i32 %11)
%23 = add nsw i32 %11, 1
%24 = tail call double @sum_omp(double* %0, i32 %23, i32 %2)
%25 = fadd double %22, %24
br label %17
}