#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*/ fprintf(int /*<<< orphan*/, char *, unsigned int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int wait_for_var_to_become_x(volatile int *var, unsigned int timeout,
                             unsigned int x) {
  unsigned int time_waited = 0;
  while ((*var != x) && (time_waited < timeout)) {
    usleep(1000);
    ++time_waited;
  }
  if (*var == x) {
    return 1;
  }
  fprintf(stderr, "\n\nTIMEOUT WAITING FOR %u \n\n", x);
  return 0;
}


x86 assembly (clang, -O0):
<wait_for_var_to_become_x>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
xor    %eax,%eax
cmp    -0x18(%rbp),%ecx
mov    %al,-0x1d(%rbp)
je     1159 <wait_for_var_to_become_x+0x39>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1169 <wait_for_var_to_become_x+0x49>
jmp    1181 <wait_for_var_to_become_x+0x61>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1139 <wait_for_var_to_become_x+0x19>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    -0x18(%rbp),%eax
jne    119c <wait_for_var_to_become_x+0x7c>
movl   $0x1,-0x4(%rbp)
jmp    11bb <wait_for_var_to_become_x+0x9b>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xe51(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_var_to_become_x>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rdi,%rbx
mov    (%rdi),%eax
cmp    %edx,%eax
je     115b <wait_for_var_to_become_x+0x3b>
mov    %esi,%r15d
test   %esi,%esi
je     115b <wait_for_var_to_become_x+0x3b>
mov    $0x1,%ebp
nop
mov    $0x3e8,%edi
call   1040 <usleep@plt>
mov    (%rbx),%eax
cmp    %r14d,%eax
je     115b <wait_for_var_to_become_x+0x3b>
lea    0x1(%rbp),%eax
cmp    %r15d,%ebp
mov    %eax,%ebp
jb     1140 <wait_for_var_to_become_x+0x20>
mov    (%rbx),%ecx
mov    $0x1,%eax
cmp    %r14d,%ecx
je     1181 <wait_for_var_to_become_x+0x61>
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
lea    0xe89(%rip),%rsi
mov    %r14d,%edx
call   1030 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"\0A\0ATIMEOUT WAITING FOR %u \0A\0A\00"
define i32 @wait_for_var_to_become_x(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %20, %3
%10 = load i32*, i32** %5
%11 = load volatile i32, i32* %10
%12 = load i32, i32* %7
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* %8
%16 = load i32, i32* %6
%17 = icmp ult i32 %15, %16
br label %18
18: ; preds = %14, %9
%19 = phi i1 [ false, %9 ], [ %17, %14 ]
br i1 %19, label %20, label %24
20: ; preds = %18
%21 = call i32 @usleep(i32 1000)
%22 = load i32, i32* %8
%23 = add i32 %22, 1
store i32 %23, i32* %8
br label %9
24: ; preds = %18
%25 = load i32*, i32** %5
%26 = load volatile i32, i32* %25
%27 = load i32, i32* %7
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 1, i32* %4
br label %34
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i32, i32* %7
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %32)
store i32 0, i32* %4
br label %34
34: ; preds = %30, %29
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"\0A\0ATIMEOUT WAITING FOR %u \0A\0A\00"
define i32 @wait_for_var_to_become_x(i32* %0, i32 %1, i32 %2) {
%4 = load volatile i32, i32* %0
%5 = icmp ne i32 %4, %2
%6 = icmp ne i32 %1, 0
%7 = and i1 %5, %6
br i1 %7, label %8, label %16
8: ; preds = %3, %8
%9 = phi i32 [ %11, %8 ], [ 0, %3 ]
%10 = tail call i32 @usleep(i32 1000)
%11 = add nuw i32 %9, 1
%12 = load volatile i32, i32* %0
%13 = icmp ne i32 %12, %2
%14 = icmp ult i32 %11, %1
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %8, label %16
16: ; preds = %8, %3
%17 = load volatile i32, i32* %0
%18 = icmp eq i32 %17, %2
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %22
22: ; preds = %16, %19
%23 = phi i32 [ 0, %19 ], [ 1, %16 ]
ret i32 %23
}