#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 */
typedef int /*<<< orphan*/ omp_lock_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ omp_destroy_lock(int /*<<< orphan*/ *);
int omp_get_thread_num();
int /*<<< orphan*/ omp_init_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ omp_set_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ omp_set_num_threads(int);
int /*<<< orphan*/ omp_unset_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, int, int);
int main(int argc, char **argv) {
  int N, nthreads, curr_thread = -1;
  int i, tid;
  double sum = 0;
  if (argc > 1) {
    N = atoi(argv[1]);
    nthreads = atoi(argv[2]);
  } else {
    N = 10;
    nthreads = 3;
  }
  omp_lock_t writelock;
  omp_set_num_threads(nthreads);
  omp_init_lock(&writelock);
#pragma omp parallel shared(N, curr_thread) private(tid)
  {
    tid = omp_get_thread_num();
    if (tid == 0) {
      omp_set_lock(&writelock);
      N += 1;
      curr_thread = tid;
      omp_unset_lock(&writelock);
    } else {
      while (curr_thread < tid - 1) {
        continue;
      }
      omp_set_lock(&writelock);
      printf("Thread %d received token %d from token %d\n", tid, N, tid - 1);
      N += 1;
      curr_thread = tid;
      omp_unset_lock(&writelock);
    }
    if (tid == 0) {
      while (curr_thread < nthreads - 1) {
        continue;
      }
      printf("Thread %d received token %d from token %d\n", tid, N,
             nthreads - 1);
    }
  }
  omp_destroy_lock(&writelock);
  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)
movl   $0xffffffff,-0x1c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11d4 <main+0x54>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    11e2 <main+0x62>
movl   $0xa,-0x14(%rbp)
movl   $0x3,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <omp_set_num_threads@plt>
lea    -0x34(%rbp),%rdi
call   1090 <omp_init_lock@plt>
mov    $0x0,%al
call   1030 <omp_get_thread_num@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    122d <main+0xad>
lea    -0x34(%rbp),%rdi
call   1060 <omp_set_lock@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <omp_unset_lock@plt>
jmp    1281 <main+0x101>
jmp    1232 <main+0xb2>
mov    -0x1c(%rbp),%eax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1248 <main+0xc8>
jmp    1232 <main+0xb2>
lea    -0x34(%rbp),%rdi
call   1060 <omp_set_lock@plt>
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
lea    0xd9c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <omp_unset_lock@plt>
cmpl   $0x0,-0x24(%rbp)
jne    12be <main+0x13e>
jmp    1290 <main+0x110>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12a6 <main+0x126>
jmp    1290 <main+0x110>
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
lea    0xd47(%rip),%rdi
call   1050 <printf@plt>
lea    -0x34(%rbp),%rdi
call   10a0 <omp_destroy_lock@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     11a8 <main+0x28>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
jmp    11b3 <main+0x33>
mov    $0xa,%r14d
mov    $0x3,%ebp
mov    %ebp,%edi
call   1070 <omp_set_num_threads@plt>
lea    0x4(%rsp),%rdi
call   1090 <omp_init_lock@plt>
xor    %eax,%eax
call   1030 <omp_get_thread_num@plt>
test   %eax,%eax
je     11e2 <main+0x62>
jle    1202 <main+0x82>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
jmp    11e0 <main+0x60>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <omp_set_lock@plt>
mov    %rbx,%rdi
call   1040 <omp_unset_lock@plt>
cmp    $0x2,%ebp
jl     1233 <main+0xb3>
nopl   0x0(%rax)
jmp    1200 <main+0x80>
mov    %eax,%ebx
mov    %eax,%ebp
add    $0xffffffff,%ebp
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1060 <omp_set_lock@plt>
lea    0xde3(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1050 <printf@plt>
mov    %r15,%rdi
call   1040 <omp_unset_lock@plt>
jmp    124d <main+0xcd>
add    $0x1,%r14d
add    $0xffffffff,%ebp
lea    0xdbf(%rip),%rdi
xor    %esi,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
call   1050 <printf@plt>
lea    0x4(%rsp),%rdi
call   10a0 <omp_destroy_lock@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Thread %d received token %d from token %d\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 double
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %8
store double 0.000000e+00, double* %11
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %24
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %7
br label %25
24: ; preds = %2
store i32 10, i32* %6
store i32 3, i32* %7
br label %25
25: ; preds = %24, %15
%26 = load i32, i32* %7
%27 = call i32 @omp_set_num_threads(i32 %26)
%28 = call i32 @omp_init_lock(i32* %12)
%29 = call i32 (...) @omp_get_thread_num()
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %25
%33 = call i32 @omp_set_lock(i32* %12)
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
%36 = load i32, i32* %10
store i32 %36, i32* %8
%37 = call i32 @omp_unset_lock(i32* %12)
br label %56
38: ; preds = %25
br label %39
39: ; preds = %44, %38
%40 = load i32, i32* %8
%41 = load i32, i32* %10
%42 = sub nsw i32 %41, 1
%43 = icmp slt i32 %40, %42
br i1 %43, label %44, label %45
44: ; preds = %39
br label %39
45: ; preds = %39
%46 = call i32 @omp_set_lock(i32* %12)
%47 = load i32, i32* %10
%48 = load i32, i32* %6
%49 = load i32, i32* %10
%50 = sub nsw i32 %49, 1
%51 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %47, i32 %48, i32 %50)
%52 = load i32, i32* %6
%53 = add nsw i32 %52, 1
store i32 %53, i32* %6
%54 = load i32, i32* %10
store i32 %54, i32* %8
%55 = call i32 @omp_unset_lock(i32* %12)
br label %56
56: ; preds = %45, %32
%57 = load i32, i32* %10
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %72
59: ; preds = %56
br label %60
60: ; preds = %65, %59
%61 = load i32, i32* %8
%62 = load i32, i32* %7
%63 = sub nsw i32 %62, 1
%64 = icmp slt i32 %61, %63
br i1 %64, label %65, label %66
65: ; preds = %60
br label %60
66: ; preds = %60
%67 = load i32, i32* %10
%68 = load i32, i32* %6
%69 = load i32, i32* %7
%70 = sub nsw i32 %69, 1
%71 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %67, i32 %68, i32 %70)
br label %72
72: ; preds = %66, %56
%73 = call i32 @omp_destroy_lock(i32* %12)
ret i32 0
}

LLVM IR (-O2):
@.str = [43 x i8] c"Thread %d received token %d from token %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
br label %12
12: ; preds = %2, %5
%13 = phi i32 [ %11, %5 ], [ 3, %2 ]
%14 = phi i32 [ %8, %5 ], [ 10, %2 ]
%15 = bitcast i32* %3 to i8*
%16 = tail call i32 @omp_set_num_threads(i32 %13)
%17 = call i32 @omp_init_lock(i32* %3)
%18 = call i32 (...) @omp_get_thread_num()
%19 = icmp eq i32 %18, 0
br i1 %19, label %28, label %20
20: ; preds = %12
%21 = icmp sgt i32 %18, 0
br i1 %21, label %22, label %23
22: ; preds = %20, %22
br label %22
23: ; preds = %20
%24 = add nsw i32 %18, -1
%25 = call i32 @omp_set_lock(i32* %3)
%26 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %14, i32 %24)
%27 = call i32 @omp_unset_lock(i32* %3)
br label %37
28: ; preds = %12
%29 = call i32 @omp_set_lock(i32* %3)
%30 = call i32 @omp_unset_lock(i32* %3)
%31 = icmp sgt i32 %13, 1
br i1 %31, label %32, label %33
32: ; preds = %28, %32
br label %32
33: ; preds = %28
%34 = add nsw i32 %14, 1
%35 = add nsw i32 %13, -1
%36 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 0, i32 %34, i32 %35)
br label %37
37: ; preds = %23, %33
%38 = call i32 @omp_destroy_lock(i32* %3)
ret i32 0
}