#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 */
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ complain(char *);
int /*<<< orphan*/ complainx(char *, int, int);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int dowait(int guy, pid_t pid) {
  int status, result;
  result = waitpid(pid, &status, 0);
  if (result < 0) {
    complain("waitpid");
    return -1;
  }
  if (WIFSIGNALED(status)) {
    complainx("proc %d: signal %d", guy, WTERMSIG(status));
    return -1;
  }
  assert(WIFEXITED(status));
  status = WEXITSTATUS(status);
  if (status) {
    complainx("proc %d: exit %d", guy, status);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<dowait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11c1 <dowait+0x41>
lea    0xe50(%rip),%rdi
call   10a0 <complain@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <dowait+0xc7>
mov    -0x10(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11fe <dowait+0x7e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe1b(%rip),%rdi
call   1030 <complainx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <dowait+0xc7>
mov    -0x10(%rbp),%edi
call   1090 <WIFEXITED@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1240 <dowait+0xc0>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xdec(%rip),%rdi
call   1030 <complainx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <dowait+0xc7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dowait>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%eax
mov    %edi,%ebx
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %eax,%eax
js     11bb <dowait+0x3b>
mov    0xc(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     11c9 <dowait+0x49>
call   1060 <WTERMSIG@plt>
lea    0xe4f(%rip),%rdi
jmp    11ed <dowait+0x6d>
lea    0xe3e(%rip),%rdi
call   10a0 <complain@plt>
jmp    11f6 <dowait+0x76>
call   1090 <WIFEXITED@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     1201 <dowait+0x81>
lea    0xe2e(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <complainx@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
jmp    11fb <dowait+0x7b>

LLVM IR (-O0):
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [19 x i8] c"proc %d: signal %d\00"
@.str.2 = [17 x i8] c"proc %d: exit %d\00"
define i32 @dowait(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = call i32 @waitpid(i32 %8, i32* %6, i32 0)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @complain(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
14: ; preds = %2
%15 = load i32, i32* %6
%16 = call i64 @WIFSIGNALED(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* %4
%20 = load i32, i32* %6
%21 = call i32 @WTERMSIG(i32 %20)
%22 = call i32 @complainx(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %21)
store i32 -1, i32* %3
br label %36
23: ; preds = %14
%24 = load i32, i32* %6
%25 = call i32 @WIFEXITED(i32 %24)
%26 = call i32 @assert(i32 %25)
%27 = load i32, i32* %6
%28 = call i32 @WEXITSTATUS(i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %23
%32 = load i32, i32* %4
%33 = load i32, i32* %6
%34 = call i32 @complainx(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
store i32 -1, i32* %3
br label %36
35: ; preds = %23
store i32 0, i32* %3
br label %36
36: ; preds = %35, %31, %18, %12
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [19 x i8] c"proc %d: signal %d\00"
@.str.2 = [17 x i8] c"proc %d: exit %d\00"
define i32 @dowait(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @waitpid(i32 %1, i32* %3, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @complain(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %25
9: ; preds = %2
%10 = load i32, i32* %3
%11 = call i64 @WIFSIGNALED(i32 %10)
%12 = icmp eq i64 %11, 0
%13 = load i32, i32* %3
br i1 %12, label %17, label %14
14: ; preds = %9
%15 = call i32 @WTERMSIG(i32 %13)
%16 = call i32 @complainx(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %15)
br label %25
17: ; preds = %9
%18 = call i32 @WIFEXITED(i32 %13)
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %3
%21 = call i32 @WEXITSTATUS(i32 %20)
store i32 %21, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = call i32 @complainx(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %21)
br label %25
25: ; preds = %17, %23, %14, %7
%26 = phi i32 [ -1, %7 ], [ -1, %14 ], [ -1, %23 ], [ 0, %17 ]
ret i32 %26
}