#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);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ realloc(int *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void processChildren(pid_t new) {
  static int *array = NULL;
  static int nbprocessus = 0;
  static int freespace = 5;
  int i, son_status, pid;
  if (array == NULL)
    array = (int *)malloc(5 * sizeof(int));
  if (new == 1) {
    free(array);
    return;
  }
  if (new != 0) {
    if (freespace == 0) {
      array = (int *)realloc(array, 5 * sizeof(int));
      freespace = 5;
    }
    array[nbprocessus] = new;
    nbprocessus++;
    freespace--;
  }
  for (i = 0; i < nbprocessus; i++) {
    if ((pid = waitpid(array[i], &son_status, WNOHANG)) == array[i]) {
      printf("\nProcess %d terminated with code %d\n", pid,
             WEXITSTATUS(son_status));
      array[i] = array[nbprocessus - 1];
      array[nbprocessus - 1] = 0;
      nbprocessus--;
      freespace++;
    } else if (pid == -1) {
      perror("waitpid failed. ");
    }
  }
}


x86 assembly (clang, -O0):
<processChildren>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
cmpq   $0x0,0x2ee5(%rip)
jne    119a <processChildren+0x2a>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,0x2ece(%rip)
cmpl   $0x1,-0x4(%rbp)
jne    11b5 <processChildren+0x45>
mov    0x2ebd(%rip),%rdi
call   1030 <free@plt>
jmp    131b <processChildren+0x1ab>
cmpl   $0x0,-0x4(%rbp)
je     1220 <processChildren+0xb0>
cmpl   $0x0,0x2e92(%rip)
jne    11ee <processChildren+0x7e>
mov    0x2e95(%rip),%rdi
mov    $0x14,%esi
call   1060 <realloc@plt>
mov    %rax,0x2e84(%rip)
movl   $0x5,0x2e6a(%rip)
mov    -0x4(%rbp),%edx
mov    0x2e70(%rip),%rax
movslq 0x2e71(%rip),%rcx
mov    %edx,(%rax,%rcx,4)
mov    0x2e68(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e5f(%rip)
mov    0x2e41(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2e38(%rip)
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    0x2e40(%rip),%eax
jge    131b <processChildren+0x1ab>
mov    0x2e2b(%rip),%rax
movslq -0x8(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1080 <waitpid@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e08(%rip),%rcx
movslq -0x8(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jne    12ed <processChildren+0x17d>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xd79(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dd5(%rip),%rax
mov    0x2dd7(%rip),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    0x2dbf(%rip),%rax
movslq -0x8(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    0x2db1(%rip),%rax
mov    0x2db3(%rip),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2da0(%rip),%eax
add    $0xffffffff,%eax
mov    %eax,0x2d97(%rip)
mov    0x2d79(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d70(%rip)
jmp    1308 <processChildren+0x198>
cmpl   $0xffffffff,-0x10(%rbp)
jne    1303 <processChildren+0x193>
lea    0xd27(%rip),%rdi
call   1090 <perror@plt>
jmp    1308 <processChildren+0x198>
jmp    130d <processChildren+0x19d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1227 <processChildren+0xb7>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<processChildren>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ee4(%rip),%rax
test   %rax,%rax
je     11a1 <processChildren+0x31>
cmp    $0x1,%ebx
je     11b7 <processChildren+0x47>
test   %ebx,%ebx
jne    11cd <processChildren+0x5d>
mov    0x2ed8(%rip),%ecx
test   %ecx,%ecx
jg     1216 <processChildren+0xa6>
jmp    12c6 <processChildren+0x156>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,0x2eb6(%rip)
cmp    $0x1,%ebx
jne    118e <processChildren+0x1e>
mov    %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
cmpl   $0x0,0x2e84(%rip)
jne    11f4 <processChildren+0x84>
mov    %rax,%rdi
mov    $0x14,%esi
call   1060 <realloc@plt>
mov    %rax,0x2e7e(%rip)
movl   $0x5,0x2e64(%rip)
movslq 0x2e75(%rip),%rcx
mov    %ebx,(%rax,%rcx,4)
add    $0x1,%ecx
mov    %ecx,0x2e69(%rip)
addl   $0xffffffff,0x2e4a(%rip)
test   %ecx,%ecx
jle    12c6 <processChildren+0x156>
xor    %ebx,%ebx
mov    0x2dc9(%rip),%r13
lea    0x4(%rsp),%r12
lea    0xdd5(%rip),%r14
lea    0xdf3(%rip),%r15
jmp    1293 <processChildren+0x123>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1040 <printf@plt>
mov    0x2e0c(%rip),%rax
movslq 0x2e0d(%rip),%rcx
lea    -0x1(%rcx),%rdx
mov    -0x4(%rax,%rcx,4),%esi
mov    %esi,(%rax,%rbx,4)
movl   $0x0,-0x4(%rax,%rcx,4)
mov    %edx,0x2df4(%rip)
addl   $0x1,0x2dd5(%rip)
add    $0x1,%rbx
movslq 0x2de2(%rip),%rcx
cmp    %rcx,%rbx
jge    12c6 <processChildren+0x156>
mov    (%rax,%rbx,4),%edi
mov    0x0(%r13),%edx
mov    %r12,%rsi
call   1080 <waitpid@plt>
mov    %eax,%ebp
mov    0x2dbd(%rip),%rax
cmp    (%rax,%rbx,4),%ebp
je     1240 <processChildren+0xd0>
cmp    $0xffffffff,%ebp
jne    1283 <processChildren+0x113>
mov    %r15,%rdi
call   1090 <perror@plt>
mov    0x2da4(%rip),%rax
jmp    1283 <processChildren+0x113>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@processChildren.array = internal global i32* null
@processChildren.nbprocessus = internal global i32 0
@processChildren.freespace = internal global i32 5
@WNOHANG = global i32 0
@.str = [37 x i8] c"\0AProcess %d terminated with code %d\0A\00"
@.str.1 = [17 x i8] c"waitpid failed. \00"
define void @processChildren(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32*, i32** @processChildren.array
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i64 @malloc(i32 20)
%10 = inttoptr i64 %9 to i32*
store i32* %10, i32** @processChildren.array
br label %11
11: ; preds = %8, %1
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32*, i32** @processChildren.array
%16 = call i32 @free(i32* %15)
br label %90
17: ; preds = %11
%18 = load i32, i32* %2
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
%21 = load i32, i32* @processChildren.freespace
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32*, i32** @processChildren.array
%25 = call i64 @realloc(i32* %24, i32 20)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** @processChildren.array
store i32 5, i32* @processChildren.freespace
br label %27
27: ; preds = %23, %20
%28 = load i32, i32* %2
%29 = load i32*, i32** @processChildren.array
%30 = load i32, i32* @processChildren.nbprocessus
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
store i32 %28, i32* %32
%33 = load i32, i32* @processChildren.nbprocessus
%34 = add nsw i32 %33, 1
store i32 %34, i32* @processChildren.nbprocessus
%35 = load i32, i32* @processChildren.freespace
%36 = add nsw i32 %35, -1
store i32 %36, i32* @processChildren.freespace
br label %37
37: ; preds = %27, %17
store i32 0, i32* %3
br label %38
38: ; preds = %87, %37
%39 = load i32, i32* %3
%40 = load i32, i32* @processChildren.nbprocessus
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %90
42: ; preds = %38
%43 = load i32*, i32** @processChildren.array
%44 = load i32, i32* %3
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = load i32, i32* @WNOHANG
%49 = call i32 @waitpid(i32 %47, i32* %4, i32 %48)
store i32 %49, i32* %5
%50 = load i32*, i32** @processChildren.array
%51 = load i32, i32* %3
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %53
%55 = icmp eq i32 %49, %54
br i1 %55, label %56, label %80
56: ; preds = %42
%57 = load i32, i32* %5
%58 = load i32, i32* %4
%59 = call i32 @WEXITSTATUS(i32 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %57, i32 %59)
%61 = load i32*, i32** @processChildren.array
%62 = load i32, i32* @processChildren.nbprocessus
%63 = sub nsw i32 %62, 1
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i32, i32* %61, i64 %64
%66 = load i32, i32* %65
%67 = load i32*, i32** @processChildren.array
%68 = load i32, i32* %3
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i32, i32* %67, i64 %69
store i32 %66, i32* %70
%71 = load i32*, i32** @processChildren.array
%72 = load i32, i32* @processChildren.nbprocessus
%73 = sub nsw i32 %72, 1
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i32, i32* %71, i64 %74
store i32 0, i32* %75
%76 = load i32, i32* @processChildren.nbprocessus
%77 = add nsw i32 %76, -1
store i32 %77, i32* @processChildren.nbprocessus
%78 = load i32, i32* @processChildren.freespace
%79 = add nsw i32 %78, 1
store i32 %79, i32* @processChildren.freespace
br label %86
80: ; preds = %42
%81 = load i32, i32* %5
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %85
83: ; preds = %80
%84 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %85
85: ; preds = %83, %80
br label %86
86: ; preds = %85, %56
br label %87
87: ; preds = %86
%88 = load i32, i32* %3
%89 = add nsw i32 %88, 1
store i32 %89, i32* %3
br label %38
90: ; preds = %14, %38
ret void
}

LLVM IR (-O2):
@processChildren.array = internal unnamed_addr global i32* null
@processChildren.nbprocessus = internal unnamed_addr global i32 0
@processChildren.freespace = internal unnamed_addr global i32 5
@WNOHANG = global i32 0
@.str = [37 x i8] c"\0AProcess %d terminated with code %d\0A\00"
@.str.1 = [17 x i8] c"waitpid failed. \00"
define void @processChildren(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32*, i32** @processChildren.array
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i64 @malloc(i32 20)
%8 = inttoptr i64 %7 to i32*
store i32* %8, i32** @processChildren.array
br label %9
9: ; preds = %6, %1
%10 = phi i32* [ %8, %6 ], [ %4, %1 ]
switch i32 %0, label %15 [
i32 1, label %13
i32 0, label %11
]
11: ; preds = %9
%12 = load i32, i32* @processChildren.nbprocessus
br label %29
13: ; preds = %9
%14 = tail call i32 @free(i32* %10)
br label %68
15: ; preds = %9
%16 = load i32, i32* @processChildren.freespace
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i64 @realloc(i32* %10, i32 20)
%20 = inttoptr i64 %19 to i32*
store i32* %20, i32** @processChildren.array
store i32 5, i32* @processChildren.freespace
br label %21
21: ; preds = %18, %15
%22 = phi i32* [ %20, %18 ], [ %10, %15 ]
%23 = load i32, i32* @processChildren.nbprocessus
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
store i32 %0, i32* %25
%26 = add nsw i32 %23, 1
store i32 %26, i32* @processChildren.nbprocessus
%27 = load i32, i32* @processChildren.freespace
%28 = add nsw i32 %27, -1
store i32 %28, i32* @processChildren.freespace
br label %29
29: ; preds = %11, %21
%30 = phi i32* [ %10, %11 ], [ %22, %21 ]
%31 = phi i32 [ %12, %11 ], [ %26, %21 ]
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %68
33: ; preds = %29, %62
%34 = phi i32* [ %63, %62 ], [ %30, %29 ]
%35 = phi i64 [ %64, %62 ], [ 0, %29 ]
%36 = getelementptr inbounds i32, i32* %34, i64 %35
%37 = load i32, i32* %36
%38 = load i32, i32* @WNOHANG
%39 = call i32 @waitpid(i32 %37, i32* %2, i32 %38)
%40 = load i32*, i32** @processChildren.array
%41 = getelementptr inbounds i32, i32* %40, i64 %35
%42 = load i32, i32* %41
%43 = icmp eq i32 %39, %42
br i1 %43, label %44, label %57
44: ; preds = %33
%45 = load i32, i32* %2
%46 = call i32 @WEXITSTATUS(i32 %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %39, i32 %46)
%48 = load i32*, i32** @processChildren.array
%49 = load i32, i32* @processChildren.nbprocessus
%50 = add nsw i32 %49, -1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %48, i64 %51
%53 = load i32, i32* %52
%54 = getelementptr inbounds i32, i32* %48, i64 %35
store i32 %53, i32* %54
store i32 0, i32* %52
store i32 %50, i32* @processChildren.nbprocessus
%55 = load i32, i32* @processChildren.freespace
%56 = add nsw i32 %55, 1
store i32 %56, i32* @processChildren.freespace
br label %62
57: ; preds = %33
%58 = icmp eq i32 %39, -1
br i1 %58, label %59, label %62
59: ; preds = %57
%60 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%61 = load i32*, i32** @processChildren.array
br label %62
62: ; preds = %44, %59, %57
%63 = phi i32* [ %48, %44 ], [ %61, %59 ], [ %40, %57 ]
%64 = add nuw nsw i64 %35, 1
%65 = load i32, i32* @processChildren.nbprocessus
%66 = sext i32 %65 to i64
%67 = icmp slt i64 %64, %66
br i1 %67, label %33, label %68
68: ; preds = %62, %29, %13
ret void
}