#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__ ESRCH;
int atoi(char *);
scalar_t__ errno;
scalar_t__ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int waitpid_main(int argc, char **argv) {
  int pids[15];
  int numpids, pidsleft;
  int i;
  if (argc > 15) {
    printf("too many pids, max %d\n", 15);
    return 1;
  }
  numpids = 0;
  for (i = 0; i < 15; i++)
    pids[i] = -1;
  for (argc--, argv++; argc-- > 0; numpids++, argv++) {
    pids[numpids] = atoi(argv[0]);
    if (pids[numpids] == 0) {
      printf("is arg '%s' not a pid?\n", argv[0]);
      return 1;
    }
  }
  printf("waiting for %d pids: ", numpids);
  for (i = 0; i < numpids; i++)
    printf("%d ", pids[i]);
  printf("\n");
  pidsleft = numpids;
  while (pidsleft > 0) {
    sleep(2);
    for (i = 0; i < numpids; i++) {
      if (pids[i] == -1)
        continue;
      if ((kill(pids[i], 0) < 0) && (errno == ESRCH)) {
        printf("waitpid: pid %d died, %d left\n", pids[i], --pidsleft);
        pids[i] = -1;
      }
    }
  }
  printf("waitpid: all named pids have exited\n");
  return 0;
}


x86 assembly (clang, -O0):
<waitpid_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0xf,-0x8(%rbp)
jle    1178 <waitpid_main+0x38>
lea    0xea0(%rip),%rdi
mov    $0xf,%esi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <waitpid_main+0x21a>
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0xf,-0x5c(%rbp)
jge    11aa <waitpid_main+0x6a>
movslq -0x5c(%rbp),%rax
movl   $0xffffffff,-0x50(%rbp,%rax,4)
ff
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1186 <waitpid_main+0x46>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
jle    1238 <waitpid_main+0xf8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,%ecx
movslq -0x54(%rbp),%rax
mov    %ecx,-0x50(%rbp,%rax,4)
movslq -0x54(%rbp),%rax
cmpl   $0x0,-0x50(%rbp,%rax,4)
jne    1219 <waitpid_main+0xd9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    135a <waitpid_main+0x21a>
jmp    121e <waitpid_main+0xde>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    11bf <waitpid_main+0x7f>
mov    -0x54(%rbp),%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    1280 <waitpid_main+0x140>
movslq -0x5c(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1250 <waitpid_main+0x110>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jle    1345 <waitpid_main+0x205>
mov    $0x2,%edi
call   1060 <sleep@plt>
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x54(%rbp),%eax
jge    1340 <waitpid_main+0x200>
movslq -0x5c(%rbp),%rax
cmpl   $0xffffffff,-0x50(%rbp,%rax,4)
jne    12cf <waitpid_main+0x18f>
jmp    1332 <waitpid_main+0x1f2>
movslq -0x5c(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%edi
xor    %esi,%esi
call   1040 <kill@plt>
cmp    $0x0,%rax
jge    132d <waitpid_main+0x1ed>
mov    0x2cf1(%rip),%rax
mov    (%rax),%rax
mov    0x2cd7(%rip),%rcx
cmp    (%rcx),%rax
jne    132d <waitpid_main+0x1ed>
movslq -0x5c(%rbp),%rax
mov    -0x50(%rbp,%rax,4),%esi
mov    -0x58(%rbp),%edx
add    $0xffffffff,%edx
mov    %edx,-0x58(%rbp)
lea    0xd2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movslq -0x5c(%rbp),%rax
movl   $0xffffffff,-0x50(%rbp,%rax,4)
ff
jmp    1332 <waitpid_main+0x1f2>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    12af <waitpid_main+0x16f>
jmp    1294 <waitpid_main+0x154>
lea    0xd1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitpid_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
cmp    $0xf,%edi
jle    118b <waitpid_main+0x2b>
lea    0xe86(%rip),%rdi
mov    $0xf,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12f5 <waitpid_main+0x195>
mov    %edi,%r14d
pcmpeqd %xmm0,%xmm0
movdqu %xmm0,0x2c(%rsp)
movdqa %xmm0,0x20(%rsp)
movdqa %xmm0,0x10(%rsp)
movdqa %xmm0,(%rsp)
cmp    $0x1,%edi
jle    12ba <waitpid_main+0x15a>
mov    %rsi,%r15
lea    -0x1(%r14),%r12d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    0x8(%r15,%rbx,8),%rbp
mov    %rbp,%rdi
call   1070 <atoi@plt>
mov    %eax,(%rsp,%rbx,4)
test   %eax,%eax
je     12e4 <waitpid_main+0x184>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11c0 <waitpid_main+0x60>
lea    0xe47(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r12d,%r12d
je     12ca <waitpid_main+0x16a>
lea    0xe43(%rip),%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp,%rbx,4),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1210 <waitpid_main+0xb0>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    $0x2,%r14d
jl     12d4 <waitpid_main+0x174>
mov    %r12d,%ebx
mov    0x2d9c(%rip),%r15
mov    0x2d85(%rip),%r13
jmp    1255 <waitpid_main+0xf5>
nopl   (%rax)
test   %r12d,%r12d
jle    12d4 <waitpid_main+0x174>
mov    $0x2,%edi
call   1080 <sleep@plt>
xor    %r14d,%r14d
jmp    1279 <waitpid_main+0x119>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r14
cmp    %r14,%rbx
je     1250 <waitpid_main+0xf0>
mov    (%rsp,%r14,4),%ebp
cmp    $0xffffffff,%ebp
je     1270 <waitpid_main+0x110>
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <kill@plt>
test   %rax,%rax
jns    1270 <waitpid_main+0x110>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
jne    1270 <waitpid_main+0x110>
add    $0xffffffff,%r12d
lea    0xda5(%rip),%rdi
mov    %ebp,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0xffffffff,(%rsp,%r14,4)
ff
jmp    1270 <waitpid_main+0x110>
lea    0xd6e(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xd8d(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    12fa <waitpid_main+0x19a>
lea    0xd2c(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"too many pids, max %d\0A\00"
@.str.1 = [24 x i8] c"is arg '%s' not a pid?\0A\00"
@.str.2 = [22 x i8] c"waiting for %d pids: \00"
@.str.3 = [4 x i8] c"%d \00"
@.str.4 = [2 x i8] c"\0A\00"
@errno = global i64 0
@ESRCH = global i64 0
@.str.5 = [31 x i8] c"waitpid: pid %d died, %d left\0A\00"
@.str.6 = [37 x i8] c"waitpid: all named pids have exited\0A\00"
define i32 @waitpid_main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [15 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 15
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 15)
store i32 1, i32* %3
br label %122
14: ; preds = %2
store i32 0, i32* %7
store i32 0, i32* %9
br label %15
15: ; preds = %22, %14
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 15
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %20
store i32 -1, i32* %21
br label %22
22: ; preds = %18
%23 = load i32, i32* %9
%24 = add nsw i32 %23, 1
store i32 %24, i32* %9
br label %15
25: ; preds = %15
%26 = load i32, i32* %4
%27 = add nsw i32 %26, -1
store i32 %27, i32* %4
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i32 1
store i8** %29, i8*** %5
br label %30
30: ; preds = %53, %25
%31 = load i32, i32* %4
%32 = add nsw i32 %31, -1
store i32 %32, i32* %4
%33 = icmp sgt i32 %31, 0
br i1 %33, label %34, label %58
34: ; preds = %30
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %40
store i32 %38, i32* %41
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %34
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 0
%50 = load i8*, i8** %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %50)
store i32 1, i32* %3
br label %122
52: ; preds = %34
br label %53
53: ; preds = %52
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
%56 = load i8**, i8*** %5
%57 = getelementptr inbounds i8*, i8** %56, i32 1
store i8** %57, i8*** %5
br label %30
58: ; preds = %30
%59 = load i32, i32* %7
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %59)
store i32 0, i32* %9
br label %61
61: ; preds = %71, %58
%62 = load i32, i32* %9
%63 = load i32, i32* %7
%64 = icmp slt i32 %62, %63
br i1 %64, label %65, label %74
65: ; preds = %61
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %67
%69 = load i32, i32* %68
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %65
%72 = load i32, i32* %9
%73 = add nsw i32 %72, 1
store i32 %73, i32* %9
br label %61
74: ; preds = %61
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%76 = load i32, i32* %7
store i32 %76, i32* %8
br label %77
77: ; preds = %119, %74
%78 = load i32, i32* %8
%79 = icmp sgt i32 %78, 0
br i1 %79, label %80, label %120
80: ; preds = %77
%81 = call i32 @sleep(i32 2)
store i32 0, i32* %9
br label %82
82: ; preds = %116, %80
%83 = load i32, i32* %9
%84 = load i32, i32* %7
%85 = icmp slt i32 %83, %84
br i1 %85, label %86, label %119
86: ; preds = %82
%87 = load i32, i32* %9
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %88
%90 = load i32, i32* %89
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %93
92: ; preds = %86
br label %116
93: ; preds = %86
%94 = load i32, i32* %9
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %95
%97 = load i32, i32* %96
%98 = call i64 @kill(i32 %97, i32 0)
%99 = icmp slt i64 %98, 0
br i1 %99, label %100, label %115
100: ; preds = %93
%101 = load i64, i64* @errno
%102 = load i64, i64* @ESRCH
%103 = icmp eq i64 %101, %102
br i1 %103, label %104, label %115
104: ; preds = %100
%105 = load i32, i32* %9
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %106
%108 = load i32, i32* %107
%109 = load i32, i32* %8
%110 = add nsw i32 %109, -1
store i32 %110, i32* %8
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %108, i32 %110)
%112 = load i32, i32* %9
%113 = sext i32 %112 to i64
%114 = getelementptr inbounds [15 x i32], [15 x i32]* %6, i64 0, i64 %113
store i32 -1, i32* %114
br label %115
115: ; preds = %104, %100, %93
br label %116
116: ; preds = %115, %92
%117 = load i32, i32* %9
%118 = add nsw i32 %117, 1
store i32 %118, i32* %9
br label %82
119: ; preds = %82
br label %77
120: ; preds = %77
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %122
122: ; preds = %120, %47, %12
%123 = load i32, i32* %3
ret i32 %123
}

LLVM IR (-O2):
@.str = [23 x i8] c"too many pids, max %d\0A\00"
@.str.1 = [24 x i8] c"is arg '%s' not a pid?\0A\00"
@.str.2 = [22 x i8] c"waiting for %d pids: \00"
@.str.3 = [4 x i8] c"%d \00"
@errno = global i64 0
@ESRCH = global i64 0
@.str.5 = [31 x i8] c"waitpid: pid %d died, %d left\0A\00"
@str = [36 x i8] c"waitpid: all named pids have exited\00"
define i32 @waitpid_main(i32 %0, i8** nocapture %1) {
%3 = alloca [15 x i32]
%4 = bitcast [15 x i32]* %3 to i8*
%5 = icmp sgt i32 %0, 15
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = icmp sgt i32 %0, 1
br i1 %7, label %12, label %8
8: ; preds = %6
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %40
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 15)
br label %74
12: ; preds = %6
%13 = add nsw i32 %0, -1
%14 = zext i32 %13 to i64
br label %15
15: ; preds = %12, %25
%16 = phi i64 [ 0, %12 ], [ %26, %25 ]
%17 = phi i8** [ %1, %12 ], [ %18, %25 ]
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = getelementptr inbounds [15 x i32], [15 x i32]* %3, i64 0, i64 %16
store i32 %20, i32* %21
%22 = icmp eq i32 %20, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %19)
br label %74
25: ; preds = %15
%26 = add nuw nsw i64 %16, 1
%27 = icmp eq i64 %26, %14
br i1 %27, label %28, label %15
28: ; preds = %25
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%30 = icmp eq i32 %13, 0
br i1 %30, label %40, label %31
31: ; preds = %28
%32 = zext i32 %13 to i64
br label %33
33: ; preds = %31, %33
%34 = phi i64 [ 0, %31 ], [ %38, %33 ]
%35 = getelementptr inbounds [15 x i32], [15 x i32]* %3, i64 0, i64 %34
%36 = load i32, i32* %35
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = add nuw nsw i64 %34, 1
%39 = icmp eq i64 %38, %32
br i1 %39, label %42, label %33
40: ; preds = %8, %28
%41 = tail call i32 @putchar(i32 10)
br label %72
42: ; preds = %33
%43 = tail call i32 @putchar(i32 10)
%44 = icmp sgt i32 %0, 1
br i1 %44, label %45, label %72
45: ; preds = %42
%46 = zext i32 %13 to i64
br label %49
47: ; preds = %68
%48 = icmp sgt i32 %69, 0
br i1 %48, label %49, label %72
49: ; preds = %47, %45
%50 = phi i32 [ %13, %45 ], [ %69, %47 ]
%51 = tail call i32 @sleep(i32 2)
br label %52
52: ; preds = %49, %68
%53 = phi i64 [ 0, %49 ], [ %70, %68 ]
%54 = phi i32 [ %50, %49 ], [ %69, %68 ]
%55 = getelementptr inbounds [15 x i32], [15 x i32]* %3, i64 0, i64 %53
%56 = load i32, i32* %55
%57 = icmp eq i32 %56, -1
br i1 %57, label %68, label %58
58: ; preds = %52
%59 = tail call i64 @kill(i32 %56, i32 0)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %58
%62 = load i64, i64* @errno
%63 = load i64, i64* @ESRCH
%64 = icmp eq i64 %62, %63
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = add nsw i32 %54, -1
%67 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %56, i32 %66)
store i32 -1, i32* %55
br label %68
68: ; preds = %58, %61, %65, %52
%69 = phi i32 [ %54, %52 ], [ %66, %65 ], [ %54, %61 ], [ %54, %58 ]
%70 = add nuw nsw i64 %53, 1
%71 = icmp eq i64 %70, %46
br i1 %71, label %47, label %52
72: ; preds = %47, %40, %42
%73 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %74
74: ; preds = %72, %23, %10
%75 = phi i32 [ 1, %10 ], [ 1, %23 ], [ 0, %72 ]
ret i32 %75
}