#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_num_threads();
int omp_get_thread_num();
double integrate_omp(double (*func)(double), double a, double b, int n) {
  double h = (b - a) / n;
  double sum = 0.0;
#pragma omp parallel
  {
    int nthreads = omp_get_num_threads();
    int threadid = omp_get_thread_num();
    int items_per_thread = n / nthreads;
    int lb = threadid * items_per_thread;
    int ub = (threadid == nthreads - 1) ? (n - 1) : (lb + items_per_thread - 1);
    for (int i = lb; i <= ub; i++) {
      double f = func(a + h * (i + 0.5));
#pragma omp critical
      { sum += f; }
    }
  }
  sum *= h;
  return sum;
}


x86 assembly (clang, -O0):
<integrate_omp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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)
mov    $0x0,%al
call   1040 <omp_get_num_threads@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
cltd
idivl  -0x34(%rbp)
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%eax
imul   -0x3c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%eax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    11a0 <integrate_omp+0x80>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11ac <integrate_omp+0x8c>
mov    -0x40(%rbp),%eax
add    -0x3c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jg     120f <integrate_omp+0xef>
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x48(%rbp),%xmm2
movsd  0xe21(%rip),%xmm3
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11b8 <integrate_omp+0x98>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integrate_omp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%r12
movsd  %xmm0,0x10(%rsp)
subsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
xor    %eax,%eax
call   1040 <omp_get_num_threads@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
mov    %eax,%ecx
mov    %r14d,%eax
cltd
idiv   %r15d
mov    %eax,%ebx
mov    %eax,%ebp
imul   %ecx,%ebp
add    $0xffffffff,%r15d
add    %ebp,%ebx
cmp    %r15d,%ecx
cmove  %r14d,%ebx
xorpd  %xmm1,%xmm1
cmp    %ebx,%ebp
jge    11c5 <integrate_omp+0xa5>
xorpd  %xmm1,%xmm1
nopw   0x0(%rax,%rax,1)
movsd  %xmm1,0x18(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
addsd  0xe5b(%rip),%xmm0
mulsd  0x8(%rsp),%xmm0
addsd  0x10(%rsp),%xmm0
call   *%r12
movsd  0x18(%rsp),%xmm1
addsd  %xmm0,%xmm1
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1190 <integrate_omp+0x70>
movsd  0x8(%rsp),%xmm0
mulsd  %xmm1,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
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 i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca double
store double (double)* %0, double (double)** %5
store double %1, double* %6
store double %2, double* %7
store i32 %3, i32* %8
%18 = load double, double* %7
%19 = load double, double* %6
%20 = fsub double %18, %19
%21 = load i32, i32* %8
%22 = sitofp i32 %21 to double
%23 = fdiv double %20, %22
store double %23, double* %9
store double 0.000000e+00, double* %10
%24 = call i32 (...) @omp_get_num_threads()
store i32 %24, i32* %11
%25 = call i32 (...) @omp_get_thread_num()
store i32 %25, i32* %12
%26 = load i32, i32* %8
%27 = load i32, i32* %11
%28 = sdiv i32 %26, %27
store i32 %28, i32* %13
%29 = load i32, i32* %12
%30 = load i32, i32* %13
%31 = mul nsw i32 %29, %30
store i32 %31, i32* %14
%32 = load i32, i32* %12
%33 = load i32, i32* %11
%34 = sub nsw i32 %33, 1
%35 = icmp eq i32 %32, %34
br i1 %35, label %36, label %39
36: ; preds = %4
%37 = load i32, i32* %8
%38 = sub nsw i32 %37, 1
br label %44
39: ; preds = %4
%40 = load i32, i32* %14
%41 = load i32, i32* %13
%42 = add nsw i32 %40, %41
%43 = sub nsw i32 %42, 1
br label %44
44: ; preds = %39, %36
%45 = phi i32 [ %38, %36 ], [ %43, %39 ]
store i32 %45, i32* %15
%46 = load i32, i32* %14
store i32 %46, i32* %16
br label %47
47: ; preds = %63, %44
%48 = load i32, i32* %16
%49 = load i32, i32* %15
%50 = icmp sle i32 %48, %49
br i1 %50, label %51, label %66
51: ; preds = %47
%52 = load double (double)*, double (double)** %5
%53 = load double, double* %6
%54 = load double, double* %9
%55 = load i32, i32* %16
%56 = sitofp i32 %55 to double
%57 = fadd double %56, 5.000000e-01
%58 = call double @llvm.fmuladd.f64(double %54, double %57, double %53)
%59 = call double %52(double %58)
store double %59, double* %17
%60 = load double, double* %17
%61 = load double, double* %10
%62 = fadd double %61, %60
store double %62, double* %10
br label %63
63: ; preds = %51
%64 = load i32, i32* %16
%65 = add nsw i32 %64, 1
store i32 %65, i32* %16
br label %47
66: ; preds = %47
%67 = load double, double* %9
%68 = load double, double* %10
%69 = fmul double %68, %67
store double %69, double* %10
%70 = load double, double* %10
ret double %70
}

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 = tail call i32 (...) @omp_get_num_threads()
%9 = tail call i32 (...) @omp_get_thread_num()
%10 = sdiv i32 %3, %8
%11 = mul nsw i32 %10, %9
%12 = add nsw i32 %8, -1
%13 = icmp eq i32 %9, %12
%14 = add nsw i32 %11, %10
%15 = select i1 %13, i32 %3, i32 %14
%16 = icmp slt i32 %11, %15
br i1 %16, label %20, label %17
17: ; preds = %20, %4
%18 = phi double [ 0.000000e+00, %4 ], [ %27, %20 ]
%19 = fmul double %7, %18
ret double %19
20: ; preds = %4, %20
%21 = phi double [ %27, %20 ], [ 0.000000e+00, %4 ]
%22 = phi i32 [ %28, %20 ], [ %11, %4 ]
%23 = sitofp i32 %22 to double
%24 = fadd double %23, 5.000000e-01
%25 = tail call double @llvm.fmuladd.f64(double %7, double %24, double %1)
%26 = tail call double %0(double %25)
%27 = fadd double %21, %26
%28 = add i32 %22, 1
%29 = icmp eq i32 %28, %15
br i1 %29, label %17, label %20
}