#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_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ proc_path;
typedef int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int tst_process_state_wait3(pid_t pid, const char state, long maxwait_s) {
  char proc_path[128], cur_state;
  int wait_step_ms = 10;
  struct timespec wait_step_ts;
  long iteration, max_iterations = (maxwait_s * 1000) / wait_step_ms;
  wait_step_ts.tv_sec = 0;
  wait_step_ts.tv_nsec = wait_step_ms * 1000000;
  snprintf(proc_path, sizeof(proc_path), "/proc/%i/stat", pid);
  for (iteration = 0; iteration < max_iterations; iteration++) {
    FILE *f = fopen(proc_path, "r");
    if (!f) {
      fprintf(stderr, "Failed to open '%s': %s\n", proc_path, strerror(errno));
      return 1;
    }
    if (fscanf(f, "%*i %*s %c", &cur_state) != 1) {
      fclose(f);
      fprintf(stderr, "Failed to read '%s': %s\n", proc_path, strerror(errno));
      return 1;
    }
    fclose(f);
    if (state == cur_state)
      return 0;
    nanosleep(&wait_step_ts, NULL);
  }
  fprintf(stderr, "Reached max wait time\n");
  return 1;
}


x86 assembly (clang, -O0):
<tst_process_state_wait3>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xa,-0xa8(%rbp)
imul   $0x3e8,-0x18(%rbp),%rax
movslq -0xa8(%rbp),%rcx
cqto
idiv   %rcx
mov    %rax,-0xc8(%rbp)
movq   $0x0,-0xb0(%rbp)
imul   $0xf4240,-0xa8(%rbp),%eax
mov    %eax,-0xb8(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xe22(%rip),%rdx
call   1040 <snprintf@plt>
movq   $0x0,-0xc0(%rbp)
mov    -0xc0(%rbp),%rax
cmp    -0xc8(%rbp),%rax
jge    1355 <tst_process_state_wait3+0x1e5>
lea    -0xa0(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xd0(%rbp)
cmpq   $0x0,-0xd0(%rbp)
jne    127f <tst_process_state_wait3+0x10f>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xdc(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xd8(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0xdc(%rbp),%edi
mov    -0xd8(%rbp),%rdx
mov    %rax,%rcx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1373 <tst_process_state_wait3+0x203>
mov    -0xd0(%rbp),%rdi
lea    0xd9c(%rip),%rsi
lea    -0xa1(%rbp),%rdx
call   1060 <fscanf@plt>
cmp    $0x1,%eax
je     1303 <tst_process_state_wait3+0x193>
mov    -0xd0(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xec(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xe8(%rbp)
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0xec(%rbp),%edi
mov    -0xe8(%rbp),%rdx
mov    %rax,%rcx
lea    0xd44(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1373 <tst_process_state_wait3+0x203>
mov    -0xd0(%rbp),%rdi
call   1030 <fclose@plt>
movsbl -0x9(%rbp),%eax
movsbl -0xa1(%rbp),%ecx
cmp    %ecx,%eax
jne    132e <tst_process_state_wait3+0x1be>
movl   $0x0,-0x4(%rbp)
jmp    1373 <tst_process_state_wait3+0x203>
lea    -0xb8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <nanosleep@plt>
mov    -0xc0(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xc0(%rbp)
jmp    11ee <tst_process_state_wait3+0x7e>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
lea    0xce8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tst_process_state_wait3>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rdx,%rbp
mov    %esi,%ebx
mov    %edi,%ecx
movq   $0x0,0x18(%rsp)
movl   $0x989680,0x10(%rsp)
lea    0xe60(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x80,%esi
call   1040 <snprintf@plt>
test   %rbp,%rbp
jle    1226 <tst_process_state_wait3+0xb6>
imul   $0x64,%rbp,%rax
cmp    $0x2,%rax
mov    $0x1,%r12d
cmovge %rax,%r12
lea    0x20(%rsp),%r15
lea    0xf(%rsp),%r13
mov    %r15,%rdi
lea    0xe34(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     123f <tst_process_state_wait3+0xcf>
mov    %rax,%rbp
mov    %rax,%rdi
lea    0xe38(%rip),%rsi
mov    %r13,%rdx
call   1060 <fscanf@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%r14d
jne    125f <tst_process_state_wait3+0xef>
cmp    %bl,0xf(%rsp)
je     12a5 <tst_process_state_wait3+0x135>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <nanosleep@plt>
add    $0xffffffffffffffff,%r12
jne    11d0 <tst_process_state_wait3+0x60>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128e <tst_process_state_wait3+0x11e>
mov    0x2d9a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb3(%rip),%rsi
jmp    127d <tst_process_state_wait3+0x10d>
mov    0x2d7a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdb7(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    1293 <tst_process_state_wait3+0x123>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@.str = [14 x i8] c"/proc/%i/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Failed to open '%s': %s\0A\00"
@errno = global i32 0
@.str.3 = [11 x i8] c"%*i %*s %c\00"
@.str.4 = [25 x i8] c"Failed to read '%s': %s\0A\00"
@.str.5 = [23 x i8] c"Reached max wait time\0A\00"
define i32 @tst_process_state_wait3(i32 %0, i8 signext %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i64
%8 = alloca [128 x i8]
%9 = alloca i8
%10 = alloca i32
%11 = alloca %struct.timespec
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32*
store i32 %0, i32* %5
store i8 %1, i8* %6
store i64 %2, i64* %7
store i32 10, i32* %10
%15 = load i64, i64* %7
%16 = mul nsw i64 %15, 1000
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = sdiv i64 %16, %18
store i64 %19, i64* %13
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* %10
%22 = mul nsw i32 %21, 1000000
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%25 = load i32, i32* %5
%26 = call i32 @snprintf(i8* %24, i32 128, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %25)
store i64 0, i64* %12
br label %27
27: ; preds = %65, %3
%28 = load i64, i64* %12
%29 = load i64, i64* %13
%30 = icmp slt i64 %28, %29
br i1 %30, label %31, label %68
31: ; preds = %27
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %33, i32** %14
%34 = load i32*, i32** %14
%35 = icmp ne i32* %34, null
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %38, i8* %40)
store i32 1, i32* %4
br label %71
42: ; preds = %31
%43 = load i32*, i32** %14
%44 = call i32 @fscanf(i32* %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%45 = icmp ne i32 %44, 1
br i1 %45, label %46, label %54
46: ; preds = %42
%47 = load i32*, i32** %14
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %50, i8* %52)
store i32 1, i32* %4
br label %71
54: ; preds = %42
%55 = load i32*, i32** %14
%56 = call i32 @fclose(i32* %55)
%57 = load i8, i8* %6
%58 = sext i8 %57 to i32
%59 = load i8, i8* %9
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %58, %60
br i1 %61, label %62, label %63
62: ; preds = %54
store i32 0, i32* %4
br label %71
63: ; preds = %54
%64 = call i32 @nanosleep(%struct.timespec* %11, i32* null)
br label %65
65: ; preds = %63
%66 = load i64, i64* %12
%67 = add nsw i64 %66, 1
store i64 %67, i64* %12
br label %27
68: ; preds = %27
%69 = load i32, i32* @stderr
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %4
br label %71
71: ; preds = %68, %62, %46, %36
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@.str = [14 x i8] c"/proc/%i/stat\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Failed to open '%s': %s\0A\00"
@errno = global i32 0
@.str.3 = [11 x i8] c"%*i %*s %c\00"
@.str.4 = [25 x i8] c"Failed to read '%s': %s\0A\00"
@.str.5 = [23 x i8] c"Reached max wait time\0A\00"
define i32 @tst_process_state_wait3(i32 %0, i8 signext %1, i64 %2) {
%4 = alloca [128 x i8]
%5 = alloca i8
%6 = alloca %struct.timespec
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.timespec* %6 to i8*
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 1
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
store i32 10000000, i32* %10
%11 = call i32 @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = icmp sgt i64 %2, 0
br i1 %12, label %13, label %41
13: ; preds = %3
%14 = mul i64 %2, 100
%15 = call i64 @llvm.smax.i64(i64 %14, i64 1)
br label %16
16: ; preds = %13, %37
%17 = phi i64 [ %39, %37 ], [ 0, %13 ]
%18 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* %23)
br label %44
25: ; preds = %16
%26 = call i32 @fscanf(i32* %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%27 = icmp eq i32 %26, 1
%28 = call i32 @fclose(i32* %18)
br i1 %27, label %34, label %29
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %7, i8* %32)
br label %44
34: ; preds = %25
%35 = load i8, i8* %5
%36 = icmp eq i8 %35, %1
br i1 %36, label %44, label %37
37: ; preds = %34
%38 = call i32 @nanosleep(%struct.timespec* %6, i32* null)
%39 = add nuw nsw i64 %17, 1
%40 = icmp eq i64 %39, %15
br i1 %40, label %41, label %16
41: ; preds = %37, %3
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %34, %20, %29, %41
%45 = phi i32 [ 1, %41 ], [ 1, %20 ], [ 1, %29 ], [ 0, %34 ]
ret i32 %45
}