#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 */
struct timespec {
  int tv_sec;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ __NR_gettid;
int asprintf(char **, char *, unsigned long);
int /*<<< orphan*/ assert(int);
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned long,
                           char const *, char const *);
int /*<<< orphan*/ free(char *);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
char *proc_string(char *, char *);
scalar_t__ sched_yield();
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char const *, char const *);
scalar_t__ syscall(int /*<<< orphan*/);
void state_wait(pid_t process, const char *wanted) {
  char *filename;
  int i;
  struct timespec start, now;
  const char *state;
  i = asprintf(&filename, "/proc/%lu/status", (unsigned long)process);
  assert(i > 0);
  i = clock_gettime(CLOCK_MONOTONIC, &start);
  assert(i == 0);
  do {
    state = proc_string(filename, "State:\t");
    if (strcmp(state, "T (tracing stop)") == 0)
      state = "t (tracing stop)";
    if (strcmp(state, wanted) == 0) {
      free(filename);
      return;
    }
    if (sched_yield()) {
      perror("sched_yield()");
      exit(EXIT_FAILURE);
    }
    i = clock_gettime(CLOCK_MONOTONIC, &now);
    assert(i == 0);
    assert(now.tv_sec >= start.tv_sec);
  } while (now.tv_sec - start.tv_sec <
           (syscall(__NR_gettid) == getpid() ? 10 : 15));
  fprintf(stderr, "Timed out waiting for PID %lu \"%s\" (now it is \"%s\")!\n",
          (unsigned long)process, wanted, state);
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<state_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   10c0 <asprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x18(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1060 <proc_string@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1257 <state_wait+0x97>
lea    0xdd7(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    127d <state_wait+0xbd>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1070 <sched_yield@plt>
cmp    $0x0,%rax
je     12a8 <state_wait+0xe8>
lea    0xda6(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x28(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10b0 <syscall@plt>
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdi
mov    -0x34(%rbp),%eax
mov    $0xf,%ecx
mov    $0xa,%edx
cmp    %rdi,%rsi
cmove  %edx,%ecx
cmp    %ecx,%eax
jl     121e <state_wait+0x5e>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xd08(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<state_wait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r13
mov    %rdi,%rdx
lea    0xe25(%rip),%rsi
lea    0x18(%rsp),%rdi
mov    %rdx,0x20(%rsp)
call   10c0 <asprintf@plt>
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1090 <assert@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1090 <assert@plt>
lea    0xdfb(%rip),%r12
lea    0xe05(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x18(%rsp),%rdi
lea    0xdd5(%rip),%rsi
call   1060 <proc_string@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
cmove  %r15,%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1305 <state_wait+0x145>
xor    %eax,%eax
call   1070 <sched_yield@plt>
test   %rax,%rax
jne    131e <state_wait+0x15e>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1090 <assert@plt>
mov    0x8(%rsp),%eax
xor    %edi,%edi
cmp    0x10(%rsp),%eax
setge  %dil
call   1090 <assert@plt>
mov    0x8(%rsp),%ebp
sub    0x10(%rsp),%ebp
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   10b0 <syscall@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <getpid@plt>
xor    %ecx,%ecx
cmp    %rax,%r14
setne  %cl
lea    (%rcx,%rcx,4),%eax
add    $0xa,%eax
cmp    %eax,%ebp
jl     1230 <state_wait+0x70>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
mov    0x20(%rsp),%rdx
mov    %r13,%rcx
mov    %rbx,%r8
call   10a0 <fprintf@plt>
jmp    132a <state_wait+0x16a>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd16(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [17 x i8] c"/proc/%lu/status\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [8 x i8] c"State:\09\00"
@.str.2 = [17 x i8] c"T (tracing stop)\00"
@.str.3 = [17 x i8] c"t (tracing stop)\00"
@.str.4 = [14 x i8] c"sched_yield()\00"
@EXIT_FAILURE = global i32 0
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str.5 = [54 x i8] c"Timed out waiting for PID %lu \22%s\22 (now it is \22%s\22)!\0A\00"
define void @state_wait(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.timespec
%8 = alloca %struct.timespec
%9 = alloca i8*
store i64 %0, i64* %3
store i8* %1, i8** %4
%10 = load i64, i64* %3
%11 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* @CLOCK_MONOTONIC
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %7)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
br label %22
22: ; preds = %58, %2
%23 = load i8*, i8** %5
%24 = call i8* @proc_string(i8* %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8** %9
br label %29
29: ; preds = %28, %22
%30 = load i8*, i8** %9
%31 = load i8*, i8** %4
%32 = call i64 @strcmp(i8* %30, i8* %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
ret void
37: ; preds = %29
%38 = call i64 (...) @sched_yield()
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %37
%45 = load i32, i32* @CLOCK_MONOTONIC
%46 = call i32 @clock_gettime(i32 %45, %struct.timespec* %8)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, 0
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%52 = load i32, i32* %51
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%54 = load i32, i32* %53
%55 = icmp sge i32 %52, %54
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
br label %58
58: ; preds = %44
%59 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%60 = load i32, i32* %59
%61 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%62 = load i32, i32* %61
%63 = sub nsw i32 %60, %62
%64 = load i32, i32* @__NR_gettid
%65 = call i64 @syscall(i32 %64)
%66 = call i64 (...) @getpid()
%67 = icmp eq i64 %65, %66
%68 = zext i1 %67 to i64
%69 = select i1 %67, i32 10, i32 15
%70 = icmp slt i32 %63, %69
br i1 %70, label %22, label %71
71: ; preds = %58
%72 = load i32, i32* @stderr
%73 = load i64, i64* %3
%74 = load i8*, i8** %4
%75 = load i8*, i8** %9
%76 = call i32 @fprintf(i32 %72, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i64 %73, i8* %74, i8* %75)
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [17 x i8] c"/proc/%lu/status\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [8 x i8] c"State:\09\00"
@.str.2 = [17 x i8] c"T (tracing stop)\00"
@.str.3 = [17 x i8] c"t (tracing stop)\00"
@.str.4 = [14 x i8] c"sched_yield()\00"
@EXIT_FAILURE = global i32 0
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str.5 = [54 x i8] c"Timed out waiting for PID %lu \22%s\22 (now it is \22%s\22)!\0A\00"
define void @state_wait(i64 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = bitcast i8** %3 to i8*
%7 = bitcast %struct.timespec* %4 to i8*
%8 = bitcast %struct.timespec* %5 to i8*
%9 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i64 %0)
%10 = icmp sgt i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %4)
%15 = icmp eq i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
br label %20
20: ; preds = %38, %2
%21 = load i8*, i8** %3
%22 = call i8* @proc_string(i8* %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %22
%26 = call i64 @strcmp(i8* %25, i8* %1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i8*, i8** %3
%30 = call i32 @free(i8* %29)
ret void
31: ; preds = %20
%32 = call i64 (...) @sched_yield()
%33 = icmp eq i64 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %31
%39 = load i32, i32* @CLOCK_MONOTONIC
%40 = call i32 @clock_gettime(i32 %39, %struct.timespec* %5)
%41 = icmp eq i32 %40, 0
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
%44 = load i32, i32* %18
%45 = load i32, i32* %19
%46 = icmp sge i32 %44, %45
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i32, i32* %18
%50 = load i32, i32* %19
%51 = sub nsw i32 %49, %50
%52 = load i32, i32* @__NR_gettid
%53 = call i64 @syscall(i32 %52)
%54 = call i64 (...) @getpid()
%55 = icmp eq i64 %53, %54
%56 = select i1 %55, i32 10, i32 15
%57 = icmp slt i32 %51, %56
br i1 %57, label %20, label %58
58: ; preds = %38
%59 = load i32, i32* @stderr
%60 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0), i64 %0, i8* %1, i8* %25)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
}