#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);
    double sumloc = 0.0;
    for (int i = lb; i <= ub; 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    $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,-0x58(%rbp)
jmp    11ac <integrate_omp+0x8c>
mov    -0x40(%rbp),%eax
add    -0x3c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
mov    %eax,-0x44(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x44(%rbp),%eax
jg     120d <integrate_omp+0xed>
mov    -0x8(%rbp),%rax
movsd  -0x10(%rbp),%xmm1
movsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x54(%rbp),%xmm2
movsd  0xe19(%rip),%xmm3
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
addsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11c0 <integrate_omp+0xa0>
movsd  -0x50(%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    $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    11cd <integrate_omp+0xad>
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>
xorpd  %xmm0,%xmm0
addsd  %xmm0,%xmm1
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 double
%17 = alloca i32
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
store double 0.000000e+00, double* %16
%46 = load i32, i32* %14
store i32 %46, i32* %17
br label %47
47: ; preds = %62, %44
%48 = load i32, i32* %17
%49 = load i32, i32* %15
%50 = icmp sle i32 %48, %49
br i1 %50, label %51, label %65
51: ; preds = %47
%52 = load double (double)*, double (double)** %5
%53 = load double, double* %6
%54 = load double, double* %9
%55 = load i32, i32* %17
%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)
%60 = load double, double* %16
%61 = fadd double %60, %59
store double %61, double* %16
br label %62
62: ; preds = %51
%63 = load i32, i32* %17
%64 = add nsw i32 %63, 1
store i32 %64, i32* %17
br label %47
65: ; preds = %47
%66 = load double, double* %16
%67 = load double, double* %10
%68 = fadd double %67, %66
store double %68, double* %10
%69 = load double, double* %9
%70 = load double, double* %10
%71 = fmul double %70, %69
store double %71, double* %10
%72 = load double, double* %10
ret double %72
}

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 %22, label %19
17: ; preds = %22
%18 = fadd double %29, 0.000000e+00
br label %19
19: ; preds = %17, %4
%20 = phi double [ 0.000000e+00, %4 ], [ %18, %17 ]
%21 = fmul double %7, %20
ret double %21
22: ; preds = %4, %22
%23 = phi i32 [ %30, %22 ], [ %11, %4 ]
%24 = phi double [ %29, %22 ], [ 0.000000e+00, %4 ]
%25 = sitofp i32 %23 to double
%26 = fadd double %25, 5.000000e-01
%27 = tail call double @llvm.fmuladd.f64(double %7, double %26, double %1)
%28 = tail call double %0(double %27)
%29 = fadd double %24, %28
%30 = add i32 %23, 1
%31 = icmp eq i32 %30, %15
br i1 %31, label %17, label %22
}