#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 */
int WEXITSTATUS(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
int check_waitpid(pid_t child_pid) {
  int status = 0;
  pid_t ret = waitpid((pid_t)-1, &status, 0);
  if (ret != child_pid) {
    fprintf(stderr, "ERROR: waitpid() returned %d, expected %d\n", ret,
            child_pid);
    return -1;
  }
  return WEXITSTATUS(status);
}


x86 assembly (clang, -O0):
<check_waitpid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1188 <check_waitpid+0x58>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe89(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1193 <check_waitpid+0x63>
mov    -0xc(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_waitpid>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
movl   $0x0,0x4(%rsp)
mov    $0xffffffff,%ebx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    %ebp,%eax
jne    1164 <check_waitpid+0x34>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%ebx
jmp    117d <check_waitpid+0x4d>
mov    0x2e75(%rip),%rcx
mov    (%rcx),%edi
lea    0xe8c(%rip),%rsi
mov    %eax,%edx
mov    %ebp,%ecx
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"ERROR: waitpid() returned %d, expected %d\0A\00"
define i32 @check_waitpid(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %4
%6 = call i32 @waitpid(i32 -1, i32* %4, i32 0)
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = load i32, i32* %3
%9 = icmp ne i32 %7, %8
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* %5
%13 = load i32, i32* %3
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
store i32 -1, i32* %2
br label %18
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i32 @WEXITSTATUS(i32 %16)
store i32 %17, i32* %2
br label %18
18: ; preds = %15, %10
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"ERROR: waitpid() returned %d, expected %d\0A\00"
define i32 @check_waitpid(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = call i32 @waitpid(i32 -1, i32* %2, i32 0)
%5 = icmp eq i32 %4, %0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %0)
br label %12
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i32 @WEXITSTATUS(i32 %10)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ -1, %6 ], [ %11, %9 ]
ret i32 %13
}