#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);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int do_wait(pid_t pids[], int num) {
  int i;
  int status = 0;
  for (i = 0; i < num; i++) {
    waitpid(pids[i], &status, 0);
    if (WIFEXITED(status)) {
      printf("child %d exited with status %d\n", pids[i], WEXITSTATUS(status));
      return (WEXITSTATUS(status));
    }
    if (WIFSIGNALED(status)) {
      fprintf(stderr, "Child %d terminated by signal #%d\n", pids[i],
              WTERMSIG(status));
      return (WTERMSIG(status));
    } else {
      perror("waitpid");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    127f <do_wait+0xff>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1208 <do_wait+0x88>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe0d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1286 <do_wait+0x106>
mov    -0x1c(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1260 <do_wait+0xe0>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xdd5(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,-0x4(%rbp)
jmp    1286 <do_wait+0x106>
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
jmp    1271 <do_wait+0xf1>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119d <do_wait+0x1d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_wait>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
test   %esi,%esi
jle    11e6 <do_wait+0x66>
mov    %rdi,%rbx
mov    %esi,%r12d
xor    %ebp,%ebp
lea    0xc(%rsp),%r14
lea    0xe97(%rip),%r15
mov    (%rbx,%rbp,4),%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0xc(%rsp),%edi
call   10a0 <WIFEXITED@plt>
test   %rax,%rax
jne    1229 <do_wait+0xa9>
mov    0xc(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
jne    11ea <do_wait+0x6a>
mov    %r15,%rdi
call   1090 <perror@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    11ac <do_wait+0x2c>
xor    %eax,%eax
jmp    121c <do_wait+0x9c>
mov    0x2def(%rip),%rax
mov    (%rax),%r14d
mov    (%rbx,%rbp,4),%ebx
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xe19(%rip),%rsi
mov    %r14d,%edi
mov    %ebx,%edx
mov    %eax,%ecx
call   1050 <fprintf@plt>
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx,%rbp,4),%ebx
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xdc4(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <printf@plt>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
jmp    121c <do_wait+0x9c>

LLVM IR (-O0):
@.str = [32 x i8] c"child %d exited with status %d\0A\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"Child %d terminated by signal #%d\0A\00"
@.str.2 = [8 x i8] c"waitpid\00"
define i32 @do_wait(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
store i32 0, i32* %7
store i32 0, i32* %6
br label %8
8: ; preds = %52, %2
%9 = load i32, i32* %6
%10 = load i32, i32* %5
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %55
12: ; preds = %8
%13 = load i32*, i32** %4
%14 = load i32, i32* %6
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i32, i32* %13, i64 %15
%17 = load i32, i32* %16
%18 = call i32 @waitpid(i32 %17, i32* %7, i32 0)
%19 = load i32, i32* %7
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %12
%23 = load i32*, i32** %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = load i32, i32* %7
%29 = call i32 @WEXITSTATUS(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %29)
%31 = load i32, i32* %7
%32 = call i32 @WEXITSTATUS(i32 %31)
store i32 %32, i32* %3
br label %56
33: ; preds = %12
%34 = load i32, i32* %7
%35 = call i64 @WIFSIGNALED(i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i32*, i32** %4
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = load i32, i32* %7
%45 = call i32 @WTERMSIG(i32 %44)
%46 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %45)
%47 = load i32, i32* %7
%48 = call i32 @WTERMSIG(i32 %47)
store i32 %48, i32* %3
br label %56
49: ; preds = %33
%50 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49
br label %52
52: ; preds = %51
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %8
55: ; preds = %8
store i32 0, i32* %3
br label %56
56: ; preds = %55, %37, %22
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [32 x i8] c"child %d exited with status %d\0A\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"Child %d terminated by signal #%d\0A\00"
@.str.2 = [8 x i8] c"waitpid\00"
define i32 @do_wait(i32* nocapture %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %43
6: ; preds = %2
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %39
%9 = phi i64 [ 0, %6 ], [ %41, %39 ]
%10 = getelementptr inbounds i32, i32* %0, i64 %9
%11 = load i32, i32* %10
%12 = call i32 @waitpid(i32 %11, i32* %3, i32 0)
%13 = load i32, i32* %3
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %8
%17 = and i64 %9, 4294967295
%18 = getelementptr inbounds i32, i32* %0, i64 %17
%19 = load i32, i32* %18
%20 = load i32, i32* %3
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @WEXITSTATUS(i32 %23)
br label %43
25: ; preds = %8
%26 = load i32, i32* %3
%27 = call i64 @WIFSIGNALED(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %39, label %29
29: ; preds = %25
%30 = and i64 %9, 4294967295
%31 = getelementptr inbounds i32, i32* %0, i64 %30
%32 = load i32, i32* @stderr
%33 = load i32, i32* %31
%34 = load i32, i32* %3
%35 = call i32 @WTERMSIG(i32 %34)
%36 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %35)
%37 = load i32, i32* %3
%38 = call i32 @WTERMSIG(i32 %37)
br label %43
39: ; preds = %25
%40 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%41 = add nuw nsw i64 %9, 1
%42 = icmp eq i64 %41, %7
br i1 %42, label %43, label %8
43: ; preds = %39, %2, %29, %16
%44 = phi i32 [ %24, %16 ], [ %38, %29 ], [ 0, %2 ], [ 0, %39 ]
ret i32 %44
}