#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ system_error(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
pid_t check_waitpid(pid_t pid) {
  int stat_loc;
  pid_t res;
  do {
    res = waitpid(pid, &stat_loc, 0);
  } while ((res < 0) && (errno == EINTR));
  if (res < 0)
    system_error("Waiting for child process failed.");
  return res;
}


x86 assembly (clang, -O0):
<check_waitpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jge    1169 <check_waitpid+0x49>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    112c <check_waitpid+0xc>
cmpq   $0x0,-0x18(%rbp)
jge    118b <check_waitpid+0x6b>
lea    0xe7a(%rip),%rdi
call   1030 <system_error@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_waitpid>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0xc(%rsp),%r15
mov    0x2ea4(%rip),%r12
mov    0x2e95(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    116f <check_waitpid+0x4f>
mov    0x0(%r13),%rax
cmp    %rax,(%r12)
je     1150 <check_waitpid+0x30>
test   %rbx,%rbx
jns    1180 <check_waitpid+0x60>
lea    0xe85(%rip),%rdi
call   1030 <system_error@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [34 x i8] c"Waiting for child process failed.\00"
define i64 @check_waitpid(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
store i64 %0, i64* %2
br label %5
5: ; preds = %15, %1
%6 = load i64, i64* %2
%7 = call i64 @waitpid(i64 %6, i32* %3, i32 0)
store i64 %7, i64* %4
br label %8
8: ; preds = %5
%9 = load i64, i64* %4
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br label %15
15: ; preds = %11, %8
%16 = phi i1 [ false, %8 ], [ %14, %11 ]
br i1 %16, label %5, label %17
17: ; preds = %15
%18 = load i64, i64* %4
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @system_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i64, i64* %4
ret i64 %23
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [34 x i8] c"Waiting for child process failed.\00"
define i64 @check_waitpid(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %4, %1
%5 = call i64 @waitpid(i64 %0, i32* %2, i32 0)
%6 = icmp slt i64 %5, 0
%7 = load i64, i64* @errno
%8 = load i64, i64* @EINTR
%9 = icmp eq i64 %7, %8
%10 = select i1 %6, i1 %9, i1 false
br i1 %10, label %4, label %11
11: ; preds = %4
br i1 %6, label %12, label %14
12: ; preds = %11
%13 = call i32 @system_error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %11
ret i64 %5
}