#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 */
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ errno;
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int waitfor(pid_t pid, int *statp) {
  int res;
  for (;;) {
    res = waitpid(pid, statp, 0);
    if (res == 0)
      continue;
    if (res == -1) {
      if (errno == EINTR)
        continue;
      return -1;
    }
    if (WIFEXITED(*statp) || WIFSIGNALED(*statp))
      return 0;
  }
}


x86 assembly (clang, -O0):
<waitfor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    115f <waitfor+0x2f>
jmp    113f <waitfor+0xf>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1194 <waitfor+0x64>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
jne    1188 <waitfor+0x58>
jmp    113f <waitfor+0xf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <waitfor+0x9f>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
jne    11be <waitfor+0x8e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11ca <waitfor+0x9a>
movl   $0x0,-0x4(%rbp)
jmp    11cf <waitfor+0x9f>
jmp    113f <waitfor+0xf>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitfor>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e9b(%rip),%r15
mov    0x2e8c(%rip),%r12
xor    %ebp,%ebp
jmp    1159 <waitfor+0x29>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1189 <waitfor+0x59>
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %eax,%eax
je     1159 <waitfor+0x29>
cmp    $0xffffffff,%eax
je     1150 <waitfor+0x20>
mov    (%rbx),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
jne    118e <waitfor+0x5e>
mov    (%rbx),%edi
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     1159 <waitfor+0x29>
jmp    118e <waitfor+0x5e>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @waitfor(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
br label %7
7: ; preds = %34, %21, %13, %2
%8 = load i32, i32* %4
%9 = load i32*, i32** %5
%10 = call i32 @waitpid(i32 %8, i32* %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %7
br label %7
14: ; preds = %7
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17
br label %7
22: ; preds = %17
store i32 -1, i32* %3
br label %35
23: ; preds = %14
%24 = load i32*, i32** %5
%25 = load i32, i32* %24
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %23
%29 = load i32*, i32** %5
%30 = load i32, i32* %29
%31 = call i64 @WIFSIGNALED(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28, %23
store i32 0, i32* %3
br label %35
34: ; preds = %28
br label %7
35: ; preds = %33, %22
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @waitfor(i32 %0, i32* %1) {
br label %3
3: ; preds = %9, %2
%4 = tail call i32 @waitpid(i32 %0, i32* %1, i32 0)
switch i32 %4, label %10 [
i32 0, label %9
i32 -1, label %5
]
5: ; preds = %3
%6 = load i64, i64* @errno
%7 = load i64, i64* @EINTR
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %18
9: ; preds = %5, %14, %3
br label %3
10: ; preds = %3
%11 = load i32, i32* %1
%12 = tail call i64 @WIFEXITED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* %1
%16 = tail call i64 @WIFSIGNALED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %9, label %18
18: ; preds = %10, %14, %5
%19 = phi i32 [ -1, %5 ], [ 0, %14 ], [ 0, %10 ]
ret i32 %19
}