double integrate_omp(double (*func)(double), double a, double b, int n) {
  double h = (b - a) / n;
  double sum = 0.0;
#pragma omp parallel
  {
    double sumloc = 0.0;
#pragma omp for
    for (int i = 0; i < n; i++)
      sumloc += func(a + h * (i + 0.5));
#pragma omp atomic
    sum += sumloc;
  }
  sum *= h;
  return sum;
}


x86 assembly (clang, -O0):
<integrate_omp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
movsd  %xmm1,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x10(%rbp),%xmm0
cvtsi2sdl -0x1c(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1195 <integrate_omp+0x95>
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x3c(%rbp),%xmm2
movsd  0xe91(%rip),%xmm3
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1148 <integrate_omp+0x48>
movsd  -0x38(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integrate_omp>:
movapd %xmm0,%xmm2
subsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
divsd  %xmm0,%xmm1
test   %esi,%esi
jle    119f <integrate_omp+0x9f>
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
xorpd  %xmm0,%xmm0
xorpd  %xmm3,%xmm3
movsd  %xmm1,0x18(%rsp)
movsd  %xmm2,0x10(%rsp)
nopl   0x0(%rax,%rax,1)
movsd  %xmm0,0x8(%rsp)
movsd  %xmm3,0x20(%rsp)
movsd  0x8(%rsp),%xmm0
addsd  0xea6(%rip),%xmm0
mulsd  %xmm1,%xmm0
addsd  %xmm2,%xmm0
call   *%r14
movsd  0x20(%rsp),%xmm3
movsd  0x10(%rsp),%xmm2
movsd  0x18(%rsp),%xmm1
addsd  %xmm0,%xmm3
movsd  0x8(%rsp),%xmm0
addsd  0xe7f(%rip),%xmm0
add    $0xffffffff,%ebx
jne    1140 <integrate_omp+0x40>
xorpd  %xmm0,%xmm0
addsd  %xmm0,%xmm3
add    $0x28,%rsp
pop    %rbx
pop    %r14
jmp    11a3 <integrate_omp+0xa3>
xorpd  %xmm3,%xmm3
mulsd  %xmm3,%xmm1
movapd %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @integrate_omp(double (double)* %0, double %1, double %2, i32 %3) {
%5 = alloca double (double)*
%6 = alloca double
%7 = alloca double
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca i32
store double (double)* %0, double (double)** %5
store double %1, double* %6
store double %2, double* %7
store i32 %3, i32* %8
%13 = load double, double* %7
%14 = load double, double* %6
%15 = fsub double %13, %14
%16 = load i32, i32* %8
%17 = sitofp i32 %16 to double
%18 = fdiv double %15, %17
store double %18, double* %9
store double 0.000000e+00, double* %10
store double 0.000000e+00, double* %11
store i32 0, i32* %12
br label %19
19: ; preds = %34, %4
%20 = load i32, i32* %12
%21 = load i32, i32* %8
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = load double (double)*, double (double)** %5
%25 = load double, double* %6
%26 = load double, double* %9
%27 = load i32, i32* %12
%28 = sitofp i32 %27 to double
%29 = fadd double %28, 5.000000e-01
%30 = call double @llvm.fmuladd.f64(double %26, double %29, double %25)
%31 = call double %24(double %30)
%32 = load double, double* %11
%33 = fadd double %32, %31
store double %33, double* %11
br label %34
34: ; preds = %23
%35 = load i32, i32* %12
%36 = add nsw i32 %35, 1
store i32 %36, i32* %12
br label %19
37: ; preds = %19
%38 = load double, double* %11
%39 = load double, double* %10
%40 = fadd double %39, %38
store double %40, double* %10
%41 = load double, double* %9
%42 = load double, double* %10
%43 = fmul double %42, %41
store double %43, double* %10
%44 = load double, double* %10
ret double %44
}

LLVM IR (-O2):
define double @integrate_omp(double (double)* nocapture %0, double %1, double %2, i32 %3) {
%5 = fsub double %2, %1
%6 = sitofp i32 %3 to double
%7 = fdiv double %5, %6
%8 = icmp sgt i32 %3, 0
br i1 %8, label %14, label %11
9: ; preds = %14
%10 = fadd double %21, 0.000000e+00
br label %11
11: ; preds = %9, %4
%12 = phi double [ 0.000000e+00, %4 ], [ %10, %9 ]
%13 = fmul double %7, %12
ret double %13
14: ; preds = %4, %14
%15 = phi i32 [ %22, %14 ], [ 0, %4 ]
%16 = phi double [ %21, %14 ], [ 0.000000e+00, %4 ]
%17 = sitofp i32 %15 to double
%18 = fadd double %17, 5.000000e-01
%19 = tail call double @llvm.fmuladd.f64(double %7, double %18, double %1)
%20 = tail call double %0(double %19)
%21 = fadd double %16, %20
%22 = add nuw nsw i32 %15, 1
%23 = icmp eq i32 %22, %3
br i1 %23, label %9, label %14
}