#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 */

/* Variables and functions */
scalar_t__ WEXITSTATUS(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WTERMSIG(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
int /*<<< orphan*/ warnx(char *, int, scalar_t__);
void dowait(int nowait, int pid) {
  int x;
  if (pid < 0) {
    return;
  }
  if (pid == 0) {
    exit(0);
  }
  if (!nowait) {
    if (waitpid(pid, &x, 0) < 0) {
      warn("waitpid");
    } else if (WIFSIGNALED(x)) {
      warnx("pid %d: signal %d", pid, WTERMSIG(x));
    } else if (WEXITSTATUS(x) != 0) {
      warnx("pid %d: exit %d", pid, WEXITSTATUS(x));
    }
  }
}


x86 assembly (clang, -O0):
<dowait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118d <dowait+0x1d>
jmp    1249 <dowait+0xd9>
cmpl   $0x0,-0x8(%rbp)
jne    119e <dowait+0x2e>
xor    %edi,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jne    1249 <dowait+0xd9>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
cmp    $0x0,%rax
jge    11d1 <dowait+0x61>
lea    0xe39(%rip),%rdi
call   1030 <warn@plt>
jmp    1244 <dowait+0xd4>
mov    -0xc(%rbp),%edi
call   1050 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1208 <dowait+0x98>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x10(%rbp),%esi
mov    %rax,%rdx
lea    0xe0a(%rip),%rdi
call   1040 <warnx@plt>
jmp    123f <dowait+0xcf>
mov    -0xc(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
cmp    $0x0,%rax
je     123a <dowait+0xca>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rdx
lea    0xde5(%rip),%rdi
call   1040 <warnx@plt>
jmp    123f <dowait+0xcf>
jmp    1244 <dowait+0xd4>
jmp    1249 <dowait+0xd9>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dowait>:
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
js     11e6 <dowait+0x76>
je     11ec <dowait+0x7c>
test   %edi,%edi
jne    11e6 <dowait+0x76>
mov    %esi,%ebx
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
test   %rax,%rax
js     11b4 <dowait+0x44>
mov    0xc(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     11c2 <dowait+0x52>
call   1060 <WTERMSIG@plt>
lea    0xe56(%rip),%rdi
jmp    11dc <dowait+0x6c>
lea    0xe45(%rip),%rdi
call   1030 <warn@plt>
jmp    11e6 <dowait+0x76>
call   1070 <WEXITSTATUS@plt>
test   %rax,%rax
je     11e6 <dowait+0x76>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe3e(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1040 <warnx@plt>
add    $0x10,%rsp
pop    %rbx
ret
xor    %edi,%edi
call   1090 <exit@plt>

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

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