#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*/ assert(int volatile *);
scalar_t__ calloc(int, int);
unsigned long long current_time_ns();
int omp_get_num_threads();
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char **argv) {
  int nthreads;
#pragma omp parallel default(none) shared(nthreads)
#pragma omp master
  { nthreads = omp_get_num_threads(); }
  printf("Using %d OpenMP threads\n", nthreads);
#pragma omp parallel default(none)
#pragma omp master
  {
    volatile int *signals = (volatile int *)calloc(1000, sizeof(int));
    assert(signals);
    const unsigned long long start_time = current_time_ns();
#pragma omp taskgroup
    {
#pragma omp task default(none) firstprivate(signals)
      {
        int i;
        for (i = 0; i < 1000; i++) {
          signals[i] = 1;
        }
      }
      int incr = 0;
      int i;
      for (i = 0; i < 1000; i++) {
        while (signals[i] == 0)
          ;
#pragma omp task default(none) firstprivate(incr)
        { incr = incr + 1; }
      }
    }
    const unsigned long long end_time = current_time_ns();
    printf("METRIC producer_consumer %d %.20f\n", 1000,
           (double)1000 / ((double)(end_time - start_time) / 1000.0));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <omp_get_num_threads@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3e8,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <assert@plt>
mov    $0x0,%al
call   1070 <current_time_ns@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3e8,-0x2c(%rbp)
jge    11d9 <main+0x89>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11af <main+0x5f>
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x3e8,-0x34(%rbp)
jge    1229 <main+0xd9>
jmp    11f9 <main+0xa9>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cmp    $0x0,%eax
jne    1212 <main+0xc2>
jmp    11f9 <main+0xa9>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e7 <main+0x97>
xor    %eax,%eax
call   1070 <current_time_ns@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
movq   %rax,%xmm1
movaps 0xdc5(%rip),%xmm0
punpckldq %xmm0,%xmm1
movapd 0xdc9(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xd92(%rip),%xmm0
divsd  %xmm0,%xmm1
movsd  0xd86(%rip),%xmm0
divsd  %xmm1,%xmm0
lea    0xdc4(%rip),%rdi
mov    $0x3e8,%esi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <omp_get_num_threads@plt>
lea    0xec6(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x3e8,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <assert@plt>
xor    %eax,%eax
call   1070 <current_time_ns@plt>
mov    %rax,%r14
mov    $0x7,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movl   $0x1,-0x1c(%rbx,%rax,4)
movl   $0x1,-0x18(%rbx,%rax,4)
movl   $0x1,-0x14(%rbx,%rax,4)
movl   $0x1,-0x10(%rbx,%rax,4)
movl   $0x1,-0xc(%rbx,%rax,4)
movl   $0x1,-0x8(%rbx,%rax,4)
movl   $0x1,-0x4(%rbx,%rax,4)
movl   $0x1,(%rbx,%rax,4)
add    $0x8,%rax
cmp    $0x3ef,%rax
jne    11a0 <main+0x50>
xor    %eax,%eax
nopl   (%rax)
cmpl   $0x0,(%rbx,%rax,4)
je     11f0 <main+0xa0>
cs nopw 0x0(%rax,%rax,1)
cmpl   $0x0,0x4(%rbx,%rax,4)
je     1200 <main+0xb0>
nopw   0x0(%rax,%rax,1)
cmpl   $0x0,0x8(%rbx,%rax,4)
je     1210 <main+0xc0>
nopw   0x0(%rax,%rax,1)
cmpl   $0x0,0xc(%rbx,%rax,4)
je     1220 <main+0xd0>
nopw   0x0(%rax,%rax,1)
cmpl   $0x0,0x10(%rbx,%rax,4)
je     1230 <main+0xe0>
add    $0x5,%rax
cmp    $0x3e8,%rax
jne    11f0 <main+0xa0>
xor    %eax,%eax
call   1070 <current_time_ns@plt>
sub    %r14,%rax
movq   %rax,%xmm0
punpckldq 0xda6(%rip),%xmm0
subpd  0xdae(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movsd  0xdaa(%rip),%xmm0
divsd  %xmm0,%xmm1
divsd  %xmm1,%xmm0
lea    0xdbc(%rip),%rdi
mov    $0x3e8,%esi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Using %d OpenMP threads\0A\00"
@.str.1 = [35 x i8] c"METRIC producer_consumer %d %.20f\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 i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 (...) @omp_get_num_threads()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i64 @calloc(i32 1000, i32 4)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %7
%18 = load i32*, i32** %7
%19 = call i32 @assert(i32* %18)
%20 = call i64 (...) @current_time_ns()
store i64 %20, i64* %8
store i32 0, i32* %9
br label %21
21: ; preds = %29, %2
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 1000
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i32*, i32** %7
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
store volatile i32 1, i32* %28
br label %29
29: ; preds = %24
%30 = load i32, i32* %9
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
br label %21
32: ; preds = %21
store i32 0, i32* %10
store i32 0, i32* %11
br label %33
33: ; preds = %48, %32
%34 = load i32, i32* %11
%35 = icmp slt i32 %34, 1000
br i1 %35, label %36, label %51
36: ; preds = %33
br label %37
37: ; preds = %44, %36
%38 = load i32*, i32** %7
%39 = load i32, i32* %11
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %38, i64 %40
%42 = load volatile i32, i32* %41
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %37
br label %37
45: ; preds = %37
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
store i32 %47, i32* %10
br label %48
48: ; preds = %45
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %33
51: ; preds = %33
%52 = call i64 (...) @current_time_ns()
store i64 %52, i64* %12
%53 = load i64, i64* %12
%54 = load i64, i64* %8
%55 = sub i64 %53, %54
%56 = uitofp i64 %55 to double
%57 = fdiv double %56, 1.000000e+03
%58 = fdiv double 1.000000e+03, %57
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 1000, double %58)
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"Using %d OpenMP threads\0A\00"
@.str.1 = [35 x i8] c"METRIC producer_consumer %d %.20f\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @omp_get_num_threads()
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i64 @calloc(i32 1000, i32 4)
%6 = inttoptr i64 %5 to i32*
%7 = tail call i32 @assert(i32* %6)
%8 = tail call i64 (...) @current_time_ns()
br label %9
9: ; preds = %9, %2
%10 = phi i64 [ 0, %2 ], [ %26, %9 ]
%11 = getelementptr inbounds i32, i32* %6, i64 %10
store volatile i32 1, i32* %11
%12 = or i64 %10, 1
%13 = getelementptr inbounds i32, i32* %6, i64 %12
store volatile i32 1, i32* %13
%14 = or i64 %10, 2
%15 = getelementptr inbounds i32, i32* %6, i64 %14
store volatile i32 1, i32* %15
%16 = or i64 %10, 3
%17 = getelementptr inbounds i32, i32* %6, i64 %16
store volatile i32 1, i32* %17
%18 = or i64 %10, 4
%19 = getelementptr inbounds i32, i32* %6, i64 %18
store volatile i32 1, i32* %19
%20 = or i64 %10, 5
%21 = getelementptr inbounds i32, i32* %6, i64 %20
store volatile i32 1, i32* %21
%22 = or i64 %10, 6
%23 = getelementptr inbounds i32, i32* %6, i64 %22
store volatile i32 1, i32* %23
%24 = or i64 %10, 7
%25 = getelementptr inbounds i32, i32* %6, i64 %24
store volatile i32 1, i32* %25
%26 = add nuw nsw i64 %10, 8
%27 = icmp eq i64 %26, 1000
br i1 %27, label %28, label %9
28: ; preds = %9, %58
%29 = phi i64 [ %59, %58 ], [ 0, %9 ]
%30 = getelementptr inbounds i32, i32* %6, i64 %29
br label %31
31: ; preds = %28, %31
%32 = load volatile i32, i32* %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %31, label %34
34: ; preds = %31
%35 = add nuw nsw i64 %29, 1
%36 = getelementptr inbounds i32, i32* %6, i64 %35
br label %37
37: ; preds = %37, %34
%38 = load volatile i32, i32* %36
%39 = icmp eq i32 %38, 0
br i1 %39, label %37, label %40
40: ; preds = %37
%41 = add nuw nsw i64 %29, 2
%42 = getelementptr inbounds i32, i32* %6, i64 %41
br label %43
43: ; preds = %43, %40
%44 = load volatile i32, i32* %42
%45 = icmp eq i32 %44, 0
br i1 %45, label %43, label %46
46: ; preds = %43
%47 = add nuw nsw i64 %29, 3
%48 = getelementptr inbounds i32, i32* %6, i64 %47
br label %49
49: ; preds = %49, %46
%50 = load volatile i32, i32* %48
%51 = icmp eq i32 %50, 0
br i1 %51, label %49, label %52
52: ; preds = %49
%53 = add nuw nsw i64 %29, 4
%54 = getelementptr inbounds i32, i32* %6, i64 %53
br label %55
55: ; preds = %55, %52
%56 = load volatile i32, i32* %54
%57 = icmp eq i32 %56, 0
br i1 %57, label %55, label %58
58: ; preds = %55
%59 = add nuw nsw i64 %29, 5
%60 = icmp eq i64 %59, 1000
br i1 %60, label %61, label %28
61: ; preds = %58
%62 = tail call i64 (...) @current_time_ns()
%63 = sub i64 %62, %8
%64 = uitofp i64 %63 to double
%65 = fdiv double %64, 1.000000e+03
%66 = fdiv double 1.000000e+03, %65
%67 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 1000, double %66)
ret i32 0
}