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

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ err(int, char *);
int errno;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int safe_waitpid(pid_t pid) {
  int save_errno;
  int status;
  save_errno = errno;
  errno = 0;
  while (waitpid(pid, &status, 0) != pid) {
    if (errno == EINTR)
      continue;
    err(1, "waitpid");
  }
  errno = save_errno;
  return (status);
}


x86 assembly (clang, -O0):
<safe_waitpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2ea1(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    -0x8(%rbp),%rax
je     1191 <safe_waitpid+0x71>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e6a(%rip),%rcx
cmp    (%rcx),%eax
jne    117b <safe_waitpid+0x5b>
jmp    1145 <safe_waitpid+0x25>
mov    $0x1,%edi
lea    0xe79(%rip),%rsi
call   1030 <err@plt>
jmp    1145 <safe_waitpid+0x25>
mov    -0xc(%rbp),%ecx
mov    0x2e45(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_waitpid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eab(%rip),%r12
mov    (%r12),%r13d
movl   $0x0,(%r12)
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    %rbx,%rax
jne    1169 <safe_waitpid+0x49>
mov    %r13d,(%r12)
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e68(%rip),%rbp
lea    0x4(%rsp),%r14
lea    0xe84(%rip),%r15
jmp    1192 <safe_waitpid+0x72>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    %rbx,%rax
je     1152 <safe_waitpid+0x32>
mov    (%r12),%eax
cmp    0x0(%rbp),%eax
je     1180 <safe_waitpid+0x60>
mov    $0x1,%edi
mov    %r15,%rsi
call   1030 <err@plt>
jmp    1180 <safe_waitpid+0x60>

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@.str = [8 x i8] c"waitpid\00"
define i32 @safe_waitpid(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i64 %0, i64* %2
%5 = load i32, i32* @errno
store i32 %5, i32* %3
store i32 0, i32* @errno
br label %6
6: ; preds = %16, %15, %1
%7 = load i64, i64* %2
%8 = call i64 @waitpid(i64 %7, i32* %4, i32 0)
%9 = load i64, i64* %2
%10 = icmp ne i64 %8, %9
br i1 %10, label %11, label %18
11: ; preds = %6
%12 = load i32, i32* @errno
%13 = load i32, i32* @EINTR
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
br label %6
16: ; preds = %11
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %6
18: ; preds = %6
%19 = load i32, i32* %3
store i32 %19, i32* @errno
%20 = load i32, i32* %4
ret i32 %20
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@.str = [8 x i8] c"waitpid\00"
define i32 @safe_waitpid(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @errno
store i32 0, i32* @errno
%5 = call i64 @waitpid(i64 %0, i32* %2, i32 0)
%6 = icmp eq i64 %5, %0
br i1 %6, label %16, label %7
7: ; preds = %1, %13
%8 = load i32, i32* @errno
%9 = load i32, i32* @EINTR
%10 = icmp eq i32 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = call i64 @waitpid(i64 %0, i32* %2, i32 0)
%15 = icmp eq i64 %14, %0
br i1 %15, label %16, label %7
16: ; preds = %13, %1
store i32 %4, i32* @errno
%17 = load i32, i32* %2
ret i32 %17
}