#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();
int /*<<< orphan*/ printf(char *, double);
int main(int argc, char *argv[]) {
  int nthreads, i;
  double total = 0.0;
#pragma omp parallel
  {
    int tid;
    tid = omp_get_thread_num();
    if (tid == 0) {
      nthreads = omp_get_num_threads();
      printf("Number of threads = %d\n", nthreads);
    }
#pragma omp barrier
    printf("Thread %d is starting...\n", tid);
#pragma omp barrier
#pragma omp for schedule(dynamic, 10)
    for (i = 0; i < 1000000; i++) {
#pragma omp atomic
      total += i * 1.0;
    }
    printf("Thread %d is done! \n", tid);
  }
  printf("Total: %f \n", total);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    117d <main+0x4d>
mov    $0x0,%al
call   1050 <omp_get_num_threads@plt>
mov    %eax,-0x14(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
lea    0xe88(%rip),%rdi
call   1040 <printf@plt>
cvtsi2sdl -0x24(%rbp),%xmm0
lea    0xe8f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0xf4240,-0x18(%rbp)
jge    11c3 <main+0x93>
cvtsi2sdl -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1195 <main+0x65>
cvtsi2sdl -0x24(%rbp),%xmm0
lea    0xe63(%rip),%rdi
call   1040 <printf@plt>
movsd  -0x20(%rbp),%xmm0
lea    0xe67(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1159 <main+0x29>
xor    %eax,%eax
call   1050 <omp_get_num_threads@plt>
cvtsi2sd %eax,%xmm0
lea    0xeb4(%rip),%rdi
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebx,%xmm0
lea    0xeb9(%rip),%rdi
movsd  %xmm0,0x8(%rsp)
call   1040 <printf@plt>
xorpd  %xmm0,%xmm0
mov    $0x7,%eax
movsd  0xe7d(%rip),%xmm1
xorpd  %xmm5,%xmm5
nopw   0x0(%rax,%rax,1)
lea    -0x6(%rax),%ecx
xorps  %xmm2,%xmm2
cvtsi2sd %ecx,%xmm2
addsd  %xmm0,%xmm5
lea    -0x5(%rax),%ecx
xorps  %xmm3,%xmm3
cvtsi2sd %ecx,%xmm3
addsd  %xmm5,%xmm2
lea    -0x4(%rax),%ecx
xorps  %xmm4,%xmm4
cvtsi2sd %ecx,%xmm4
addsd  %xmm2,%xmm3
lea    -0x3(%rax),%ecx
xorps  %xmm2,%xmm2
cvtsi2sd %ecx,%xmm2
addsd  %xmm3,%xmm4
lea    -0x2(%rax),%ecx
xorps  %xmm3,%xmm3
cvtsi2sd %ecx,%xmm3
addsd  %xmm4,%xmm2
lea    -0x1(%rax),%ecx
xorps  %xmm4,%xmm4
cvtsi2sd %ecx,%xmm4
addsd  %xmm2,%xmm3
addsd  %xmm3,%xmm4
xorps  %xmm5,%xmm5
cvtsi2sd %eax,%xmm5
addsd  %xmm4,%xmm5
addsd  %xmm1,%xmm0
add    $0x8,%eax
cmp    $0xf4247,%eax
jne    1190 <main+0x60>
lea    0xe32(%rip),%rdi
movsd  0x8(%rsp),%xmm0
movsd  %xmm5,0x8(%rsp)
call   1040 <printf@plt>
lea    0xe2f(%rip),%rdi
movsd  0x8(%rsp),%xmm0
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Number of threads = %d\0A\00"
@.str.1 = [26 x i8] c"Thread %d is starting...\0A\00"
@.str.2 = [21 x i8] c"Thread %d is done! \0A\00"
@.str.3 = [12 x i8] c"Total: %f \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 double
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store double 0.000000e+00, double* %8
%10 = call i32 (...) @omp_get_thread_num()
store i32 %10, i32* %9
%11 = load i32, i32* %9
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = call i32 (...) @omp_get_num_threads()
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = sitofp i32 %15 to double
%17 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), double %16)
br label %18
18: ; preds = %13, %2
%19 = load i32, i32* %9
%20 = sitofp i32 %19 to double
%21 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), double %20)
store i32 0, i32* %7
br label %22
22: ; preds = %30, %18
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 1000000
br i1 %24, label %25, label %33
25: ; preds = %22
%26 = load i32, i32* %7
%27 = sitofp i32 %26 to double
%28 = load double, double* %8
%29 = call double @llvm.fmuladd.f64(double %27, double 1.000000e+00, double %28)
store double %29, double* %8
br label %30
30: ; preds = %25
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %22
33: ; preds = %22
%34 = load i32, i32* %9
%35 = sitofp i32 %34 to double
%36 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), double %35)
%37 = load double, double* %8
%38 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), double %37)
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [24 x i8] c"Number of threads = %d\0A\00"
@.str.1 = [26 x i8] c"Thread %d is starting...\0A\00"
@.str.2 = [21 x i8] c"Thread %d is done! \0A\00"
@.str.3 = [12 x i8] c"Total: %f \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @omp_get_thread_num()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = tail call i32 (...) @omp_get_num_threads()
%7 = sitofp i32 %6 to double
%8 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), double %7)
br label %9
9: ; preds = %5, %2
%10 = sitofp i32 %3 to double
%11 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), double %10)
br label %12
12: ; preds = %12, %9
%13 = phi i32 [ 0, %9 ], [ %38, %12 ]
%14 = phi double [ 0.000000e+00, %9 ], [ %37, %12 ]
%15 = sitofp i32 %13 to double
%16 = fadd double %14, %15
%17 = or i32 %13, 1
%18 = sitofp i32 %17 to double
%19 = fadd double %16, %18
%20 = or i32 %13, 2
%21 = sitofp i32 %20 to double
%22 = fadd double %19, %21
%23 = or i32 %13, 3
%24 = sitofp i32 %23 to double
%25 = fadd double %22, %24
%26 = or i32 %13, 4
%27 = sitofp i32 %26 to double
%28 = fadd double %25, %27
%29 = or i32 %13, 5
%30 = sitofp i32 %29 to double
%31 = fadd double %28, %30
%32 = or i32 %13, 6
%33 = sitofp i32 %32 to double
%34 = fadd double %31, %33
%35 = or i32 %13, 7
%36 = sitofp i32 %35 to double
%37 = fadd double %34, %36
%38 = add nuw nsw i32 %13, 8
%39 = icmp eq i32 %38, 1000000
br i1 %39, label %40, label %12
40: ; preds = %12
%41 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), double %10)
%42 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), double %37)
ret i32 0
}