#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 getbusy(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/);
int /*<<< orphan*/ setbusy(int);
int /*<<< orphan*/ sleep(int);
void *foo(void *pArgs) {
  int thread_number = *(int *)pArgs;
  printf("number:%d\n", thread_number);
  printf("entered foo\n");
  printf("number: %d\n", thread_number);
  while (true) {
    printf("entered while in foo\n");
    int check = getbusy(thread_number);
    printf("after getbusy in while in foo\n");
    printf("after while in while in foo\n");
    printf("number: %d\n", thread_number);
    printf("########################check state is: %d\n", check);
    if (check == 1) {
      printf("number:%d\n", thread_number);
      printf("after printf args\n");
      setbusy(thread_number);
      printf("after setbusy\n");
    }
    if (check == 2) {
      printf("\n\nclose thread %d\n\n", thread_number);
      pthread_exit(0);
    }
    if (check == 0) {
      printf("idling\n");
      sleep(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <getbusy@plt>
mov    %eax,-0x10(%rbp)
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x10(%rbp)
jne    122b <foo+0xdb>
mov    -0xc(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <setbusy@plt>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x2,-0x10(%rbp)
jne    124d <foo+0xfd>
mov    -0xc(%rbp),%esi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <pthread_exit@plt>
cmpl   $0x0,-0x10(%rbp)
jne    126f <foo+0x11f>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1195 <foo+0x45>

x86 assembly (clang, -O2):
<foo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    (%rdi),%ebx
lea    0xe8c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed3(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xec3(%rip),%r12
lea    0xed1(%rip),%r13
lea    0xee8(%rip),%rbp
lea    0xe62(%rip),%r14
jmp    11d7 <foo+0x77>
nopw   0x0(%rax,%rax,1)
lea    0xe7c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <pthread_exit@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <getbusy@plt>
mov    %eax,%r15d
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %rbp,%rdi
call   1030 <puts@plt>
lea    0xe0b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r15d,%r15d
je     1260 <foo+0x100>
cmp    $0x2,%r15d
je     11c0 <foo+0x60>
cmp    $0x1,%r15d
jne    11d7 <foo+0x77>
lea    0xdd2(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <setbusy@plt>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
jmp    11d7 <foo+0x77>
nopl   0x0(%rax,%rax,1)
lea    0xe4b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11d7 <foo+0x77>

LLVM IR (-O0):
@.str = [11 x i8] c"number:%d\0A\00"
@.str.1 = [13 x i8] c"entered foo\0A\00"
@.str.2 = [12 x i8] c"number: %d\0A\00"
@.str.3 = [22 x i8] c"entered while in foo\0A\00"
@.str.4 = [31 x i8] c"after getbusy in while in foo\0A\00"
@.str.5 = [29 x i8] c"after while in while in foo\0A\00"
@.str.6 = [44 x i8] c"########################check state is: %d\0A\00"
@.str.7 = [19 x i8] c"after printf args\0A\00"
@.str.8 = [15 x i8] c"after setbusy\0A\00"
@.str.9 = [20 x i8] c"\0A\0Aclose thread %d\0A\0A\00"
@.str.10 = [8 x i8] c"idling\0A\00"
define i8* @foo(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = bitcast i8* %5 to i32*
%7 = load i32, i32* %6
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %1, %45
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%15 = load i32, i32* %3
%16 = call i32 @getbusy(i32 %15)
store i32 %16, i32* %4
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%19 = load i32, i32* %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %4
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %32
25: ; preds = %13
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%29 = load i32, i32* %3
%30 = call i32 @setbusy(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
br label %32
32: ; preds = %25, %13
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 2
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i32, i32* %3
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %36)
%38 = call i32 @pthread_exit(i32 0)
br label %39
39: ; preds = %35, %32
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.10, i64 0, i64 0))
%44 = call i32 @sleep(i32 1)
br label %45
45: ; preds = %42, %39
br label %13
}

LLVM IR (-O2):
@.str = [11 x i8] c"number:%d\0A\00"
@.str.2 = [12 x i8] c"number: %d\0A\00"
@.str.6 = [44 x i8] c"########################check state is: %d\0A\00"
@.str.9 = [20 x i8] c"\0A\0Aclose thread %d\0A\0A\00"
@str = [12 x i8] c"entered foo\00"
@str.11 = [21 x i8] c"entered while in foo\00"
@str.12 = [30 x i8] c"after getbusy in while in foo\00"
@str.13 = [28 x i8] c"after while in while in foo\00"
@str.14 = [7 x i8] c"idling\00"
@str.15 = [18 x i8] c"after printf args\00"
@str.16 = [14 x i8] c"after setbusy\00"
define noalias i8* @foo(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %3)
br label %7
7: ; preds = %22, %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.11, i64 0, i64 0))
%9 = tail call i32 @getbusy(i32 %3)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.12, i64 0, i64 0))
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.13, i64 0, i64 0))
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i32 %9)
switch i32 %9, label %22 [
i32 1, label %14
i32 2, label %19
i32 0, label %23
]
14: ; preds = %7
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.15, i64 0, i64 0))
%17 = tail call i32 @setbusy(i32 %3)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.16, i64 0, i64 0))
br label %22
19: ; preds = %7
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0), i32 %3)
%21 = tail call i32 @pthread_exit(i32 0)
br label %22
22: ; preds = %19, %14, %7, %23
br label %7
23: ; preds = %7
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.14, i64 0, i64 0))
%25 = tail call i32 @sleep(i32 1)
br label %22
}