#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_num();
double integrate_omp_opt(double (*func)(double), double a, double b, int n) {
  double h = (b - a) / n;
  double sum = 0.0;
  struct thread_param {
    double sum;
    double padding[7];
  };
  struct thread_param thread_sum[32] __attribute__((aligned(64)));
#pragma omp parallel
  {
    int tid = omp_get_thread_num();
    thread_sum[tid].sum = 0;
#pragma omp for
    for (int i = 0; i < n; i++)
      thread_sum[tid].sum += func(a + h * (i + 0.5));
#pragma omp atomic
    sum += thread_sum[tid].sum;
  }
  sum *= h;
  return sum;
}


x86 assembly (clang, -O0):
<integrate_omp_opt>:
push   %rbp
mov    %rsp,%rbp
and    $0xffffffffffffffc0,%rsp
sub    $0x880,%rsp
mov    %rdi,0x868(%rsp)
movsd  %xmm0,0x860(%rsp)
movsd  %xmm1,0x858(%rsp)
mov    %esi,0x854(%rsp)
movsd  0x858(%rsp),%xmm0
subsd  0x860(%rsp),%xmm0
cvtsi2sdl 0x854(%rsp),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,0x848(%rsp)
xorps  %xmm0,%xmm0
movsd  %xmm0,0x840(%rsp)
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,0x3c(%rsp)
movslq 0x3c(%rsp),%rcx
lea    0x40(%rsp),%rax
shl    $0x6,%rcx
add    %rcx,%rax
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax)
movl   $0x0,0x38(%rsp)
mov    0x38(%rsp),%eax
cmp    0x854(%rsp),%eax
jge    120f <integrate_omp_opt+0xff>
mov    0x868(%rsp),%rax
movsd  0x860(%rsp),%xmm1
movsd  0x848(%rsp),%xmm0
cvtsi2sdl 0x38(%rsp),%xmm2
movsd  0xe28(%rip),%xmm3
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
call   *%rax
movslq 0x3c(%rsp),%rcx
lea    0x40(%rsp),%rax
shl    $0x6,%rcx
add    %rcx,%rax
addsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
mov    0x38(%rsp),%eax
add    $0x1,%eax
mov    %eax,0x38(%rsp)
jmp    119f <integrate_omp_opt+0x8f>
movslq 0x3c(%rsp),%rcx
lea    0x40(%rsp),%rax
shl    $0x6,%rcx
add    %rcx,%rax
movsd  (%rax),%xmm0
addsd  0x840(%rsp),%xmm0
movsd  %xmm0,0x840(%rsp)
movsd  0x848(%rsp),%xmm0
mulsd  0x840(%rsp),%xmm0
movsd  %xmm0,0x840(%rsp)
movsd  0x840(%rsp),%xmm0
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<integrate_omp_opt>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
movsd  %xmm0,0x18(%rsp)
subsd  %xmm0,%xmm1
xorps  %xmm0,%xmm0
cvtsi2sd %esi,%xmm0
divsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
xorpd  %xmm1,%xmm1
test   %ebx,%ebx
jle    119e <integrate_omp_opt+0x8e>
xorpd  %xmm0,%xmm0
nopw   0x0(%rax,%rax,1)
movsd  %xmm0,0x10(%rsp)
movsd  %xmm1,0x20(%rsp)
movsd  0x10(%rsp),%xmm0
addsd  0xe96(%rip),%xmm0
mulsd  0x8(%rsp),%xmm0
addsd  0x18(%rsp),%xmm0
call   *%r14
movsd  0x20(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  0x10(%rsp),%xmm0
addsd  0xe77(%rip),%xmm0
add    $0xffffffff,%ebx
jne    1150 <integrate_omp_opt+0x40>
xorpd  %xmm0,%xmm0
addsd  %xmm0,%xmm1
movsd  0x8(%rsp),%xmm0
mulsd  %xmm1,%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.thread_param = type { double, [7 x double] }
define double @integrate_omp_opt(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 [32 x %struct.thread_param]
%12 = alloca i32
%13 = alloca i32
store double (double)* %0, double (double)** %5
store double %1, double* %6
store double %2, double* %7
store i32 %3, i32* %8
%14 = load double, double* %7
%15 = load double, double* %6
%16 = fsub double %14, %15
%17 = load i32, i32* %8
%18 = sitofp i32 %17 to double
%19 = fdiv double %16, %18
store double %19, double* %9
store double 0.000000e+00, double* %10
%20 = call i32 (...) @omp_get_thread_num()
store i32 %20, i32* %12
%21 = load i32, i32* %12
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [32 x %struct.thread_param], [32 x %struct.thread_param]* %11, i64 0, i64 %22
%24 = getelementptr inbounds %struct.thread_param, %struct.thread_param* %23, i32 0, i32 0
store double 0.000000e+00, double* %24
store i32 0, i32* %13
br label %25
25: ; preds = %44, %4
%26 = load i32, i32* %13
%27 = load i32, i32* %8
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %47
29: ; preds = %25
%30 = load double (double)*, double (double)** %5
%31 = load double, double* %6
%32 = load double, double* %9
%33 = load i32, i32* %13
%34 = sitofp i32 %33 to double
%35 = fadd double %34, 5.000000e-01
%36 = call double @llvm.fmuladd.f64(double %32, double %35, double %31)
%37 = call double %30(double %36)
%38 = load i32, i32* %12
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [32 x %struct.thread_param], [32 x %struct.thread_param]* %11, i64 0, i64 %39
%41 = getelementptr inbounds %struct.thread_param, %struct.thread_param* %40, i32 0, i32 0
%42 = load double, double* %41
%43 = fadd double %42, %37
store double %43, double* %41
br label %44
44: ; preds = %29
%45 = load i32, i32* %13
%46 = add nsw i32 %45, 1
store i32 %46, i32* %13
br label %25
47: ; preds = %25
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [32 x %struct.thread_param], [32 x %struct.thread_param]* %11, i64 0, i64 %49
%51 = getelementptr inbounds %struct.thread_param, %struct.thread_param* %50, i32 0, i32 0
%52 = load double, double* %51
%53 = load double, double* %10
%54 = fadd double %53, %52
store double %54, double* %10
%55 = load double, double* %9
%56 = load double, double* %10
%57 = fmul double %56, %55
store double %57, double* %10
%58 = load double, double* %10
ret double %58
}

LLVM IR (-O2):
define double @integrate_omp_opt(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_thread_num()
%9 = icmp sgt i32 %3, 0
br i1 %9, label %15, label %12
10: ; preds = %15
%11 = fadd double %22, 0.000000e+00
br label %12
12: ; preds = %10, %4
%13 = phi double [ %11, %10 ], [ 0.000000e+00, %4 ]
%14 = fmul double %7, %13
ret double %14
15: ; preds = %4, %15
%16 = phi double [ %22, %15 ], [ 0.000000e+00, %4 ]
%17 = phi i32 [ %23, %15 ], [ 0, %4 ]
%18 = sitofp i32 %17 to double
%19 = fadd double %18, 5.000000e-01
%20 = tail call double @llvm.fmuladd.f64(double %7, double %19, double %1)
%21 = tail call double %0(double %20)
%22 = fadd double %21, %16
%23 = add nuw nsw i32 %17, 1
%24 = icmp eq i32 %23, %3
br i1 %24, label %10, label %15
}