#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;
int WEXITSTATUS(int);
scalar_t__ WIFCONTINUED(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WTERMSIG(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
int /*<<< orphan*/ xerror(char *);
int wait_for_child(pid_t child_pid) {
  while (1) {
    int status;
    pid_t pid = wait(&status);
    if (pid < 0) {
      if (errno == EINTR)
        continue;
      xerror("wait");
    }
    if (pid != child_pid) {
      fprintf(stderr, "unknown child process %d terminated with status %d\n",
              pid, status);
      exit(1);
    }
    if (WIFSTOPPED(status)) {
      fprintf(stderr, "executor process %d received SIGSTOP\n", pid);
      continue;
    }
    if (WIFCONTINUED(status)) {
      fprintf(stderr, "executor process %d received SIGCONT\n", pid);
      continue;
    }
    if (WIFEXITED(status))
      return WEXITSTATUS(status);
    if (WIFSIGNALED(status))
      return 128 + WTERMSIG(status);
    fprintf(stderr, "wait: unknown status: %d\n", status);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<wait_for_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ef <wait_for_child+0x4f>
mov    0x2e0d(%rip),%rax
mov    (%rax),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <wait_for_child+0x43>
jmp    11ac <wait_for_child+0xc>
lea    0xe16(%rip),%rdi
call   1030 <xerror@plt>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     1227 <wait_for_child+0x87>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     125b <wait_for_child+0xbb>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11ac <wait_for_child+0xc>
mov    -0x14(%rbp),%edi
call   1060 <WIFCONTINUED@plt>
cmp    $0x0,%rax
je     128f <wait_for_child+0xef>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11ac <wait_for_child+0xc>
mov    -0x14(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12b1 <wait_for_child+0x111>
mov    -0x14(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    12fc <wait_for_child+0x15c>
mov    -0x14(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12d8 <wait_for_child+0x138>
mov    -0x14(%rbp),%edi
call   1080 <WTERMSIG@plt>
add    $0x80,%eax
mov    %eax,-0x4(%rbp)
jmp    12fc <wait_for_child+0x15c>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd9a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_child>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x4(%rsp),%r14
mov    0x2e16(%rip),%r12
mov    0x2e1f(%rip),%r15
lea    0xe71(%rip),%r13
jmp    11df <wait_for_child+0x3f>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   10c0 <wait@plt>
mov    %rax,%rbp
test   %rax,%rax
js     1220 <wait_for_child+0x80>
cmp    %rbx,%rbp
jne    1241 <wait_for_child+0xa1>
mov    0x4(%rsp),%edi
call   1070 <WIFSTOPPED@plt>
test   %rax,%rax
jne    11d0 <wait_for_child+0x30>
mov    0x4(%rsp),%edi
call   1060 <WIFCONTINUED@plt>
test   %rax,%rax
je     1262 <wait_for_child+0xc2>
mov    (%r15),%edi
lea    0xe45(%rip),%rsi
jmp    11d6 <wait_for_child+0x36>
nopl   0x0(%rax)
mov    0x2db1(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     11df <wait_for_child+0x3f>
lea    0xdc9(%rip),%rdi
call   1030 <xerror@plt>
cmp    %rbx,%rbp
je     11f4 <wait_for_child+0x54>
mov    (%r15),%edi
mov    0x4(%rsp),%ecx
lea    0xdb6(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x4(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     127b <wait_for_child+0xdb>
call   1090 <WEXITSTATUS@plt>
jmp    1291 <wait_for_child+0xf1>
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     12a0 <wait_for_child+0x100>
mov    0x4(%rsp),%edi
call   1080 <WTERMSIG@plt>
sub    $0xffffff80,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%edi
mov    0x4(%rsp),%edx
lea    0xdd7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"wait\00"
@stderr = global i32 0
@.str.1 = [52 x i8] c"unknown child process %d terminated with status %d\0A\00"
@.str.2 = [38 x i8] c"executor process %d received SIGSTOP\0A\00"
@.str.3 = [38 x i8] c"executor process %d received SIGCONT\0A\00"
@.str.4 = [26 x i8] c"wait: unknown status: %d\0A\00"
define i32 @wait_for_child(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
store i64 %0, i64* %3
br label %6
6: ; preds = %1, %14, %32, %41
%7 = call i64 @wait(i32* %4)
store i64 %7, i64* %5
%8 = load i64, i64* %5
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %6
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
br label %6
15: ; preds = %10
%16 = call i32 @xerror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %6
%18 = load i64, i64* %5
%19 = load i64, i64* %3
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i64, i64* %5
%24 = trunc i64 %23 to i32
%25 = load i32, i32* %4
%26 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
%29 = load i32, i32* %4
%30 = call i64 @WIFSTOPPED(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i64, i64* %5
%35 = trunc i64 %34 to i32
%36 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %6
37: ; preds = %28
%38 = load i32, i32* %4
%39 = call i64 @WIFCONTINUED(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i64, i64* %5
%44 = trunc i64 %43 to i32
%45 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %44)
br label %6
46: ; preds = %37
%47 = load i32, i32* %4
%48 = call i64 @WIFEXITED(i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i32, i32* %4
%52 = call i32 @WEXITSTATUS(i32 %51)
store i32 %52, i32* %2
br label %66
53: ; preds = %46
%54 = load i32, i32* %4
%55 = call i64 @WIFSIGNALED(i32 %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %53
%58 = load i32, i32* %4
%59 = call i32 @WTERMSIG(i32 %58)
%60 = add nsw i32 128, %59
store i32 %60, i32* %2
br label %66
61: ; preds = %53
%62 = load i32, i32* @stderr
%63 = load i32, i32* %4
%64 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %57, %50
%67 = load i32, i32* %2
ret i32 %67
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"wait\00"
@stderr = global i32 0
@.str.1 = [52 x i8] c"unknown child process %d terminated with status %d\0A\00"
@.str.2 = [38 x i8] c"executor process %d received SIGSTOP\0A\00"
@.str.3 = [38 x i8] c"executor process %d received SIGCONT\0A\00"
@.str.4 = [26 x i8] c"wait: unknown status: %d\0A\00"
define i32 @wait_for_child(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = trunc i64 %0 to i32
%5 = trunc i64 %0 to i32
br label %6
6: ; preds = %56, %1
%7 = call i64 @wait(i32* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINTR
%12 = icmp eq i64 %10, %11
br i1 %12, label %56, label %13
13: ; preds = %9
%14 = call i32 @xerror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %6
%16 = icmp eq i64 %7, %0
br i1 %16, label %23, label %17
17: ; preds = %15
%18 = load i32, i32* @stderr
%19 = trunc i64 %7 to i32
%20 = load i32, i32* %2
%21 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = load i32, i32* %2
%25 = call i64 @WIFSTOPPED(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %4)
br label %56
30: ; preds = %23
%31 = load i32, i32* %2
%32 = call i64 @WIFCONTINUED(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %5)
br label %56
37: ; preds = %30
%38 = load i32, i32* %2
%39 = call i64 @WIFEXITED(i32 %38)
%40 = icmp eq i64 %39, 0
%41 = load i32, i32* %2
br i1 %40, label %44, label %42
42: ; preds = %37
%43 = call i32 @WEXITSTATUS(i32 %41)
br label %57
44: ; preds = %37
%45 = call i64 @WIFSIGNALED(i32 %41)
%46 = icmp eq i64 %45, 0
br i1 %46, label %51, label %47
47: ; preds = %44
%48 = load i32, i32* %2
%49 = call i32 @WTERMSIG(i32 %48)
%50 = add nsw i32 %49, 128
br label %57
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = load i32, i32* %2
%54 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %9, %34, %27
br label %6
57: ; preds = %42, %47
%58 = phi i32 [ %50, %47 ], [ %43, %42 ]
ret i32 %58
}