#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*/ CheckAndUpdateTlsVars(int, int);
int /*<<< orphan*/ CounterAdvance(int, int);
int /*<<< orphan*/ CounterWait(int, int);
int /*<<< orphan*/ printf(char *, int, ...);
void *MyThreadFunction(void *arg) {
  int id = *(int *)arg;
  int r;
  printf("[%d] entering thread\n", id);
  if (id < 0 || id >= 2) {
    printf("[%d] ERROR: bad id\n", id);
    return 0;
  }
  for (r = 0; r < 10; ++r) {
    printf("[%d] before round %d\n", id, r);
    CounterWait(id, r);
    CheckAndUpdateTlsVars(id, r);
    CounterAdvance(id, r);
    printf("[%d] after round %d\n", id, r);
  }
  printf("[%d] exiting thread\n", id);
  return 0;
}


x86 assembly (clang, -O0):
<MyThreadFunction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jl     117a <MyThreadFunction+0x3a>
cmpl   $0x2,-0x14(%rbp)
jl     1198 <MyThreadFunction+0x58>
mov    -0x14(%rbp),%esi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1219 <MyThreadFunction+0xd9>
movl   $0x0,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
jge    1200 <MyThreadFunction+0xc0>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <CounterWait@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1060 <CheckAndUpdateTlsVars@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <CounterAdvance@plt>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119f <MyThreadFunction+0x5f>
mov    -0x14(%rbp),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MyThreadFunction>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    (%rdi),%ebx
lea    0xeaf(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x1,%ebx
jbe    1168 <MyThreadFunction+0x28>
lea    0xeb0(%rip),%r14
jmp    11bf <MyThreadFunction+0x7f>
lea    0xebb(%rip),%r15
lea    0xeca(%rip),%r12
lea    0xed8(%rip),%r14
xor    %ebp,%ebp
nop
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <CounterWait@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1060 <CheckAndUpdateTlsVars@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <CounterAdvance@plt>
mov    %r12,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    $0xa,%ebp
jne    1180 <MyThreadFunction+0x40>
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"[%d] entering thread\0A\00"
@.str.1 = [20 x i8] c"[%d] ERROR: bad id\0A\00"
@.str.2 = [22 x i8] c"[%d] before round %d\0A\00"
@.str.3 = [21 x i8] c"[%d] after round %d\0A\00"
@.str.4 = [21 x i8] c"[%d] exiting thread\0A\00"
define i8* @MyThreadFunction(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = bitcast i8* %6 to i32*
%8 = load i32, i32* %7
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = load i32, i32* %4
%15 = icmp sge i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %13, %1
%17 = load i32, i32* %4
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %17)
store i8* null, i8** %2
br label %45
19: ; preds = %13
store i32 0, i32* %5
br label %20
20: ; preds = %39, %19
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 10
br i1 %22, label %23, label %42
23: ; preds = %20
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %25)
%27 = load i32, i32* %4
%28 = load i32, i32* %5
%29 = call i32 @CounterWait(i32 %27, i32 %28)
%30 = load i32, i32* %4
%31 = load i32, i32* %5
%32 = call i32 @CheckAndUpdateTlsVars(i32 %30, i32 %31)
%33 = load i32, i32* %4
%34 = load i32, i32* %5
%35 = call i32 @CounterAdvance(i32 %33, i32 %34)
%36 = load i32, i32* %4
%37 = load i32, i32* %5
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %23
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %20
42: ; preds = %20
%43 = load i32, i32* %4
%44 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %43)
store i8* null, i8** %2
br label %45
45: ; preds = %42, %16
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@.str = [22 x i8] c"[%d] entering thread\0A\00"
@.str.1 = [20 x i8] c"[%d] ERROR: bad id\0A\00"
@.str.2 = [22 x i8] c"[%d] before round %d\0A\00"
@.str.3 = [21 x i8] c"[%d] after round %d\0A\00"
@.str.4 = [21 x i8] c"[%d] exiting thread\0A\00"
define noalias i8* @MyThreadFunction(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp ugt i32 %3, 1
br i1 %5, label %15, label %6
6: ; preds = %1, %6
%7 = phi i32 [ %13, %6 ], [ 0, %1 ]
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %3, i32 %7)
%9 = tail call i32 @CounterWait(i32 %3, i32 %7)
%10 = tail call i32 @CheckAndUpdateTlsVars(i32 %3, i32 %7)
%11 = tail call i32 @CounterAdvance(i32 %3, i32 %7)
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %3, i32 %7)
%13 = add nuw nsw i32 %7, 1
%14 = icmp eq i32 %13, 10
br i1 %14, label %15, label %6
15: ; preds = %6, %1
%16 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), %6 ]
%17 = tail call i32 (i8*, i32, ...) @printf(i8* %16, i32 %3)
ret i8* null
}