#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;
int 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))
    return -1;
  return WEXITSTATUS(status);
}


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    11ba <wait_for_pid+0x8a>
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
jne    11af <wait_for_pid+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ba <wait_for_pid+0x8a>
mov    -0xc(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
mov    %eax,-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    1186 <wait_for_pid+0x56>
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     1186 <wait_for_pid+0x56>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
jmp    118b <wait_for_pid+0x5b>
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 %30
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 %27, label %26
26: ; preds = %22
store i32 -1, i32* %2
br label %30
27: ; preds = %22
%28 = load i32, i32* %4
%29 = call i32 @WEXITSTATUS(i32 %28)
store i32 %29, i32* %2
br label %30
30: ; preds = %27, %26, %16
%31 = load i32, i32* %2
ret i32 %31
}

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 %21
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 %21, label %18
18: ; preds = %14
%19 = load i32, i32* %2
%20 = call i32 @WEXITSTATUS(i32 %19)
br label %21
21: ; preds = %7, %14, %18
%22 = phi i32 [ %20, %18 ], [ -1, %14 ], [ -1, %7 ]
ret i32 %22
}