#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 /*<<< orphan*/ dtime(double *);
double func1(int, int);
int omp_get_num_procs();
int omp_get_num_threads();
int omp_get_thread_num();
int /*<<< orphan*/ printf(char *, double, ...);
int main(int argc, char *argv[]) {
  int i, id, Nthrds, Nprocs, N, k, kp;
  double sum[100], tsum;
  double stime, etime;
  dtime(&stime);
  N = 30000000;
  k = 0;
#pragma omp parallel shared(k, sum) private(i, id, kp, tsum)
  {
    id = omp_get_thread_num();
    Nthrds = omp_get_num_threads();
    Nprocs = omp_get_num_procs();
    printf("Nprocs=%2d Nthrds=%2d id=%d\n", Nprocs, Nthrds, id);
    kp = id;
    k = Nthrds - 1;
    do {
      sum[kp] = func1(kp, N);
      printf("id=%2d kp=%2d sum=%15.12f\n", id, kp, sum[kp]);
#pragma omp critical
      kp = ++k;
    } while (k < 20);
#pragma omp barrier
    {
      tsum = 0.0;
      for (i = 0; i < 20; i++) {
        tsum += sum[i];
      }
    }
    printf("tsum=%15.12f\n", tsum);
  }
  dtime(&etime);
  printf("Elapsed time (s)=%15.12f\n", etime - stime);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x370,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x360(%rbp),%rdi
call   1080 <dtime@plt>
movl   $0x1c9c380,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1040 <omp_get_thread_num@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <omp_get_num_threads@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <omp_get_num_procs@plt>
mov    %eax,-0x20(%rbp)
cvtsi2sdl -0x20(%rbp),%xmm0
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe3d(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1030 <func1@plt>
movslq -0x2c(%rbp),%rax
movsd  %xmm0,-0x350(%rbp,%rax,8)
ff ff
cvtsi2sdl -0x18(%rbp),%xmm0
mov    -0x2c(%rbp),%esi
movslq -0x2c(%rbp),%rax
movsd  -0x350(%rbp,%rax,8),%xmm1
ff ff
lea    0xe10(%rip),%rdi
mov    $0x2,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    %eax,-0x2c(%rbp)
cmpl   $0x14,-0x28(%rbp)
jl     11d9 <main+0x79>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x358(%rbp)
ff
movl   $0x0,-0x14(%rbp)
cmpl   $0x14,-0x14(%rbp)
jge    1271 <main+0x111>
movslq -0x14(%rbp),%rax
movsd  -0x350(%rbp,%rax,8),%xmm0
ff ff
addsd  -0x358(%rbp),%xmm0
ff
movsd  %xmm0,-0x358(%rbp)
ff
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123c <main+0xdc>
movsd  -0x358(%rbp),%xmm0
ff
lea    0xdb8(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
lea    -0x368(%rbp),%rdi
call   1080 <dtime@plt>
movsd  -0x368(%rbp),%xmm0
ff
subsd  -0x360(%rbp),%xmm0
ff
lea    0xd9c(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x370,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x348,%rsp
lea    0x18(%rsp),%rdi
call   1080 <dtime@plt>
xor    %eax,%eax
call   1040 <omp_get_thread_num@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <omp_get_num_threads@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1070 <omp_get_num_procs@plt>
cvtsi2sd %eax,%xmm0
lea    0xe64(%rip),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    $0x1,%al
call   1050 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
movsd  %xmm0,0x8(%rsp)
lea    0xe61(%rip),%r14
nopl   0x0(%rax)
mov    %r15d,%ebp
mov    %ebx,%edi
mov    $0x1c9c380,%esi
call   1030 <func1@plt>
movapd %xmm0,%xmm1
movslq %ebx,%rsi
movsd  %xmm0,0x20(%rsp,%rsi,8)
mov    %r14,%rdi
movsd  0x8(%rsp),%xmm0
mov    $0x2,%al
call   1050 <printf@plt>
lea    0x1(%rbp),%r15d
lea    -0x1(%rbp),%eax
mov    %ebp,%ebx
cmp    $0x13,%eax
jl     11c0 <main+0x60>
xorpd  %xmm0,%xmm0
addsd  0x20(%rsp),%xmm0
addsd  0x28(%rsp),%xmm0
addsd  0x30(%rsp),%xmm0
addsd  0x38(%rsp),%xmm0
addsd  0x40(%rsp),%xmm0
addsd  0x48(%rsp),%xmm0
addsd  0x50(%rsp),%xmm0
addsd  0x58(%rsp),%xmm0
addsd  0x60(%rsp),%xmm0
addsd  0x68(%rsp),%xmm0
addsd  0x70(%rsp),%xmm0
addsd  0x78(%rsp),%xmm0
addsd  0x80(%rsp),%xmm0
addsd  0x88(%rsp),%xmm0
addsd  0x90(%rsp),%xmm0
addsd  0x98(%rsp),%xmm0
addsd  0xa0(%rsp),%xmm0
addsd  0xa8(%rsp),%xmm0
addsd  0xb0(%rsp),%xmm0
addsd  0xb8(%rsp),%xmm0
lea    0xda3(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
lea    0x10(%rsp),%rdi
call   1080 <dtime@plt>
movsd  0x10(%rsp),%xmm0
subsd  0x18(%rsp),%xmm0
lea    0xd8d(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x348,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Nprocs=%2d Nthrds=%2d id=%d\0A\00"
@.str.1 = [27 x i8] c"id=%2d kp=%2d sum=%15.12f\0A\00"
@.str.2 = [14 x i8] c"tsum=%15.12f\0A\00"
@.str.3 = [26 x i8] c"Elapsed time (s)=%15.12f\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [100 x double]
%14 = alloca double
%15 = alloca double
%16 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = call i32 @dtime(double* %15)
store i32 30000000, i32* %10
store i32 0, i32* %11
%18 = call i32 (...) @omp_get_thread_num()
store i32 %18, i32* %7
%19 = call i32 (...) @omp_get_num_threads()
store i32 %19, i32* %8
%20 = call i32 (...) @omp_get_num_procs()
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = sitofp i32 %21 to double
%23 = load i32, i32* %8
%24 = load i32, i32* %7
%25 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), double %22, i32 %23, i32 %24)
%26 = load i32, i32* %7
store i32 %26, i32* %12
%27 = load i32, i32* %8
%28 = sub nsw i32 %27, 1
store i32 %28, i32* %11
br label %29
29: ; preds = %46, %2
%30 = load i32, i32* %12
%31 = load i32, i32* %10
%32 = call double @func1(i32 %30, i32 %31)
%33 = load i32, i32* %12
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [100 x double], [100 x double]* %13, i64 0, i64 %34
store double %32, double* %35
%36 = load i32, i32* %7
%37 = sitofp i32 %36 to double
%38 = load i32, i32* %12
%39 = load i32, i32* %12
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [100 x double], [100 x double]* %13, i64 0, i64 %40
%42 = load double, double* %41
%43 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double %37, i32 %38, double %42)
%44 = load i32, i32* %11
%45 = add nsw i32 %44, 1
store i32 %45, i32* %11
store i32 %45, i32* %12
br label %46
46: ; preds = %29
%47 = load i32, i32* %11
%48 = icmp slt i32 %47, 20
br i1 %48, label %29, label %49
49: ; preds = %46
store double 0.000000e+00, double* %14
store i32 0, i32* %6
br label %50
50: ; preds = %60, %49
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 20
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = load i32, i32* %6
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [100 x double], [100 x double]* %13, i64 0, i64 %55
%57 = load double, double* %56
%58 = load double, double* %14
%59 = fadd double %58, %57
store double %59, double* %14
br label %60
60: ; preds = %53
%61 = load i32, i32* %6
%62 = add nsw i32 %61, 1
store i32 %62, i32* %6
br label %50
63: ; preds = %50
%64 = load double, double* %14
%65 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), double %64)
%66 = call i32 @dtime(double* %16)
%67 = load double, double* %16
%68 = load double, double* %15
%69 = fsub double %67, %68
%70 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), double %69)
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"Nprocs=%2d Nthrds=%2d id=%d\0A\00"
@.str.1 = [27 x i8] c"id=%2d kp=%2d sum=%15.12f\0A\00"
@.str.2 = [14 x i8] c"tsum=%15.12f\0A\00"
@.str.3 = [26 x i8] c"Elapsed time (s)=%15.12f\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x double]
%4 = alloca double
%5 = alloca double
%6 = bitcast [100 x double]* %3 to i8*
%7 = bitcast double* %4 to i8*
%8 = bitcast double* %5 to i8*
%9 = call i32 @dtime(double* %4)
%10 = call i32 (...) @omp_get_thread_num()
%11 = call i32 (...) @omp_get_num_threads()
%12 = call i32 (...) @omp_get_num_procs()
%13 = sitofp i32 %12 to double
%14 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), double %13, i32 %11, i32 %10)
%15 = add nsw i32 %11, -1
%16 = sitofp i32 %10 to double
br label %17
17: ; preds = %17, %2
%18 = phi i32 [ %15, %2 ], [ %24, %17 ]
%19 = phi i32 [ %10, %2 ], [ %24, %17 ]
%20 = call double @func1(i32 %19, i32 30000000)
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 %21
store double %20, double* %22
%23 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double %16, i32 %19, double %20)
%24 = add nsw i32 %18, 1
%25 = icmp slt i32 %18, 19
br i1 %25, label %17, label %26
26: ; preds = %17
%27 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 0
%28 = load double, double* %27
%29 = fadd double %28, 0.000000e+00
%30 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 1
%31 = load double, double* %30
%32 = fadd double %29, %31
%33 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 2
%34 = load double, double* %33
%35 = fadd double %32, %34
%36 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 3
%37 = load double, double* %36
%38 = fadd double %35, %37
%39 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 4
%40 = load double, double* %39
%41 = fadd double %38, %40
%42 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 5
%43 = load double, double* %42
%44 = fadd double %41, %43
%45 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 6
%46 = load double, double* %45
%47 = fadd double %44, %46
%48 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 7
%49 = load double, double* %48
%50 = fadd double %47, %49
%51 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 8
%52 = load double, double* %51
%53 = fadd double %50, %52
%54 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 9
%55 = load double, double* %54
%56 = fadd double %53, %55
%57 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 10
%58 = load double, double* %57
%59 = fadd double %56, %58
%60 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 11
%61 = load double, double* %60
%62 = fadd double %59, %61
%63 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 12
%64 = load double, double* %63
%65 = fadd double %62, %64
%66 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 13
%67 = load double, double* %66
%68 = fadd double %65, %67
%69 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 14
%70 = load double, double* %69
%71 = fadd double %68, %70
%72 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 15
%73 = load double, double* %72
%74 = fadd double %71, %73
%75 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 16
%76 = load double, double* %75
%77 = fadd double %74, %76
%78 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 17
%79 = load double, double* %78
%80 = fadd double %77, %79
%81 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 18
%82 = load double, double* %81
%83 = fadd double %80, %82
%84 = getelementptr inbounds [100 x double], [100 x double]* %3, i64 0, i64 19
%85 = load double, double* %84
%86 = fadd double %83, %85
%87 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), double %86)
%88 = call i32 @dtime(double* %5)
%89 = load double, double* %5
%90 = load double, double* %4
%91 = fsub double %89, %90
%92 = call i32 (i8*, double, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), double %91)
ret i32 0
}