#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++)
      sum += func(a + h * (i + 0.5));
  }
  sum *= h;
  return sum;
}


x86 assembly (clang, -O0):
<integrate_omp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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,-0x4c(%rbp)
jmp    11ac <integrate_omp+0x8c>
mov    -0x40(%rbp),%eax
add    -0x3c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x44(%rbp),%eax
jg     1205 <integrate_omp+0xe5>
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
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    $0x50,%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
store double (double)* %0, double (double)** %5
store double %1, double* %6
store double %2, double* %7
store i32 %3, i32* %8
%17 = load double, double* %7
%18 = load double, double* %6
%19 = fsub double %17, %18
%20 = load i32, i32* %8
%21 = sitofp i32 %20 to double
%22 = fdiv double %19, %21
store double %22, double* %9
store double 0.000000e+00, double* %10
%23 = call i32 (...) @omp_get_num_threads()
store i32 %23, i32* %11
%24 = call i32 (...) @omp_get_thread_num()
store i32 %24, i32* %12
%25 = load i32, i32* %8
%26 = load i32, i32* %11
%27 = sdiv i32 %25, %26
store i32 %27, i32* %13
%28 = load i32, i32* %12
%29 = load i32, i32* %13
%30 = mul nsw i32 %28, %29
store i32 %30, i32* %14
%31 = load i32, i32* %12
%32 = load i32, i32* %11
%33 = sub nsw i32 %32, 1
%34 = icmp eq i32 %31, %33
br i1 %34, label %35, label %38
35: ; preds = %4
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 1
br label %43
38: ; preds = %4
%39 = load i32, i32* %14
%40 = load i32, i32* %13
%41 = add nsw i32 %39, %40
%42 = sub nsw i32 %41, 1
br label %43
43: ; preds = %38, %35
%44 = phi i32 [ %37, %35 ], [ %42, %38 ]
store i32 %44, i32* %15
%45 = load i32, i32* %14
store i32 %45, i32* %16
br label %46
46: ; preds = %61, %43
%47 = load i32, i32* %16
%48 = load i32, i32* %15
%49 = icmp sle i32 %47, %48
br i1 %49, label %50, label %64
50: ; preds = %46
%51 = load double (double)*, double (double)** %5
%52 = load double, double* %6
%53 = load double, double* %9
%54 = load i32, i32* %16
%55 = sitofp i32 %54 to double
%56 = fadd double %55, 5.000000e-01
%57 = call double @llvm.fmuladd.f64(double %53, double %56, double %52)
%58 = call double %51(double %57)
%59 = load double, double* %10
%60 = fadd double %59, %58
store double %60, double* %10
br label %61
61: ; preds = %50
%62 = load i32, i32* %16
%63 = add nsw i32 %62, 1
store i32 %63, i32* %16
br label %46
64: ; preds = %46
%65 = load double, double* %9
%66 = load double, double* %10
%67 = fmul double %66, %65
store double %67, double* %10
%68 = load double, double* %10
ret double %68
}

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 i32 [ %28, %20 ], [ %11, %4 ]
%22 = phi double [ %27, %20 ], [ 0.000000e+00, %4 ]
%23 = sitofp i32 %21 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 %22, %26
%28 = add i32 %21, 1
%29 = icmp eq i32 %28, %15
br i1 %29, label %17, label %20
}