#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, 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
mov    -0x10(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1258 <state_wait+0x98>
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     1283 <state_wait+0xc3>
lea    0xda9(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d5e(%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    0x2d14(%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    0x2cc9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xd0b(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2ca0(%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,%r12
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    0xdf3(%rip),%r15
lea    0x8(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x18(%rsp),%rdi
mov    %r15,%rsi
call   1060 <proc_string@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     12f0 <state_wait+0x130>
xor    %eax,%eax
call   1070 <sched_yield@plt>
test   %rax,%rax
jne    1309 <state_wait+0x149>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
mov    %r13,%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),%r14d
sub    0x10(%rsp),%r14d
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10b0 <syscall@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1050 <getpid@plt>
xor    %ecx,%ecx
cmp    %rax,%rbp
setne  %cl
lea    (%rcx,%rcx,4),%eax
add    $0xa,%eax
cmp    %eax,%r14d
jl     1230 <state_wait+0x70>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
mov    0x20(%rsp),%rdx
mov    %r12,%rcx
mov    %rbx,%r8
call   10a0 <fprintf@plt>
jmp    1315 <state_wait+0x155>
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    0xd09(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2cac(%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 = [14 x i8] c"sched_yield()\00"
@EXIT_FAILURE = global i32 0
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str.3 = [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 = %53, %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 = load i8*, i8** %4
%27 = call i64 @strcmp(i8* %25, i8* %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i8*, i8** %5
%31 = call i32 @free(i8* %30)
ret void
32: ; preds = %22
%33 = call i64 (...) @sched_yield()
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %32
%40 = load i32, i32* @CLOCK_MONOTONIC
%41 = call i32 @clock_gettime(i32 %40, %struct.timespec* %8)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp eq i32 %42, 0
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%49 = load i32, i32* %48
%50 = icmp sge i32 %47, %49
%51 = zext i1 %50 to i32
%52 = call i32 @assert(i32 %51)
br label %53
53: ; preds = %39
%54 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%55 = load i32, i32* %54
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%57 = load i32, i32* %56
%58 = sub nsw i32 %55, %57
%59 = load i32, i32* @__NR_gettid
%60 = call i64 @syscall(i32 %59)
%61 = call i64 (...) @getpid()
%62 = icmp eq i64 %60, %61
%63 = zext i1 %62 to i64
%64 = select i1 %62, i32 10, i32 15
%65 = icmp slt i32 %58, %64
br i1 %65, label %22, label %66
66: ; preds = %53
%67 = load i32, i32* @stderr
%68 = load i64, i64* %3
%69 = load i8*, i8** %4
%70 = load i8*, i8** %9
%71 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %68, i8* %69, i8* %70)
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
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 = [14 x i8] c"sched_yield()\00"
@EXIT_FAILURE = global i32 0
@__NR_gettid = global i32 0
@stderr = global i32 0
@.str.3 = [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 = %35, %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* %1)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i8*, i8** %3
%27 = call i32 @free(i8* %26)
ret void
28: ; preds = %20
%29 = call i64 (...) @sched_yield()
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %28
%36 = load i32, i32* @CLOCK_MONOTONIC
%37 = call i32 @clock_gettime(i32 %36, %struct.timespec* %5)
%38 = icmp eq i32 %37, 0
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i32, i32* %18
%42 = load i32, i32* %19
%43 = icmp sge i32 %41, %42
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load i32, i32* %18
%47 = load i32, i32* %19
%48 = sub nsw i32 %46, %47
%49 = load i32, i32* @__NR_gettid
%50 = call i64 @syscall(i32 %49)
%51 = call i64 (...) @getpid()
%52 = icmp eq i64 %50, %51
%53 = select i1 %52, i32 10, i32 15
%54 = icmp slt i32 %48, %53
br i1 %54, label %20, label %55
55: ; preds = %35
%56 = load i32, i32* @stderr
%57 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %0, i8* %1, i8* %22)
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
}