#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;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ errno;
int waitpid(int /*<<< orphan*/, 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 (!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    11a9 <wait_for_pid+0x79>
mov    -0xc(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    119e <wait_for_pid+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <wait_for_pid+0x79>
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
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    116c <wait_for_pid+0x3c>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1150 <wait_for_pid+0x20>
jmp    1184 <wait_for_pid+0x54>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %eax,%eax
je     1184 <wait_for_pid+0x54>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
jmp    1189 <wait_for_pid+0x59>
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 = %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 %25
17: ; preds = %6
%18 = load i32, i32* %4
%19 = call i32 @WIFEXITED(i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %17
store i32 -1, i32* %2
br label %25
22: ; preds = %17
%23 = load i32, i32* %4
%24 = call i32 @WEXITSTATUS(i32 %23)
store i32 %24, i32* %2
br label %25
25: ; preds = %22, %21, %16
%26 = load i32, i32* %2
ret i32 %26
}

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 = %7, %1
%5 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
br i1 %10, label %4, label %18
11: ; preds = %4
%12 = load i32, i32* %2
%13 = call i32 @WIFEXITED(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = load i32, i32* %2
%17 = call i32 @WEXITSTATUS(i32 %16)
br label %18
18: ; preds = %7, %11, %15
%19 = phi i32 [ %17, %15 ], [ -1, %11 ], [ -1, %7 ]
ret i32 %19
}