#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 pid_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ errno;
int waitpid(int, int *, int /*<<< orphan*/);
int wait_for_pid(pid_t pid) {
  int status, ret;
again:
  ret = waitpid(pid, &status, 0);
  if (ret == -1) {
    if (errno == EINTR)
      goto again;
    return -1;
  }
  if (ret != pid)
    goto again;
  if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<wait_for_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1181 <wait_for_pid+0x51>
mov    0x2e83(%rip),%rax
mov    (%rax),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
jne    1175 <wait_for_pid+0x45>
jmp    113b <wait_for_pid+0xb>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <wait_for_pid+0x98>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1192 <wait_for_pid+0x62>
jmp    113b <wait_for_pid+0xb>
mov    -0xc(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%eax
je     11b5 <wait_for_pid+0x85>
mov    -0xc(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     11c1 <wait_for_pid+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <wait_for_pid+0x98>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_pid>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x4(%rsp),%r14
mov    0x2e9a(%rip),%r15
mov    0x2e8b(%rip),%r12
jmp    1159 <wait_for_pid+0x29>
nop
mov    (%r15),%rax
cmp    (%r12),%rax
jne    118b <wait_for_pid+0x5b>
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1150 <wait_for_pid+0x20>
cmp    %ebx,%eax
jne    1159 <wait_for_pid+0x29>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %eax,%eax
je     118b <wait_for_pid+0x5b>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
neg    %rax
sbb    %eax,%eax
jmp    1190 <wait_for_pid+0x60>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @wait_for_pid(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %11, %1
%5 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %23
11: ; preds = %7, %12
br label %4
12: ; preds = %4
%13 = icmp eq i32 %5, %0
br i1 %13, label %14, label %11
14: ; preds = %12
%15 = load i32, i32* %2
%16 = call i32 @WIFEXITED(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = load i32, i32* %2
%20 = call i64 @WEXITSTATUS(i32 %19)
%21 = icmp ne i64 %20, 0
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %7, %18, %14
%24 = phi i32 [ -1, %14 ], [ %22, %18 ], [ -1, %7 ]
ret i32 %24
}