#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, int const);
scalar_t__ waitpid(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  const int NUM_HIJOS = 5;
  pid_t pid[NUM_HIJOS];
  switch (pid[0] = fork()) {
  case 0:
    printf("Soy el hijo %d\n", getpid());
    break;
  default:
    switch (pid[1] = fork()) {
    case 0:
      printf("Soy el hijo %d\n", getpid());
      break;
    default:
      switch (pid[2] = fork()) {
      case 0:
        printf("Soy el hijo %d\n", getpid());
        break;
      default:
        switch (pid[3] = fork()) {
        case 0:
          printf("Soy el hijo %d\n", getpid());
          break;
        default:
          switch (pid[4] = fork()) {
          case 0:
            printf("Soy el hijo %d\n", getpid());
            break;
          }
          break;
        }
        break;
      }
      break;
    }
    break;
  }
  for (int i = 0; i < NUM_HIJOS; i = i + 2) {
    if (waitpid(pid[i], 0, 0) > 0) {
      printf("Acaba de finalizar mi hijo con PID:%d\n", pid[i]);
      printf("Solo me quedan %d hijos vivos\n", NUM_HIJOS - i);
    }
  }
  for (int i = 1; i < NUM_HIJOS; i = i + 2) {
    if (waitpid(pid[i], 0, 0) > 0) {
      printf("Acaba de finalizar mi hijo con PID:%d\n", pid[i]);
      printf("Solo me quedan %d hijos vivos\n", NUM_HIJOS - i);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x5,-0x14(%rbp)
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x40(%rbp)
test   %eax,%eax
jne    11a5 <main+0x55>
jmp    118b <main+0x3b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
jmp    1278 <main+0x128>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x3c(%rbp)
test   %eax,%eax
jne    11d6 <main+0x86>
jmp    11bc <main+0x6c>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe34(%rip),%rdi
call   1040 <printf@plt>
jmp    1273 <main+0x123>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x38(%rbp)
test   %eax,%eax
jne    1207 <main+0xb7>
jmp    11ed <main+0x9d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe03(%rip),%rdi
call   1040 <printf@plt>
jmp    126e <main+0x11e>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x34(%rbp)
test   %eax,%eax
jne    1238 <main+0xe8>
jmp    121e <main+0xce>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdd2(%rip),%rdi
call   1040 <printf@plt>
jmp    1269 <main+0x119>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,-0x30(%rbp)
test   %eax,%eax
jne    1264 <main+0x114>
jmp    124f <main+0xff>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xda1(%rip),%rdi
call   1040 <printf@plt>
jmp    1269 <main+0x119>
jmp    126e <main+0x11e>
jmp    1273 <main+0x123>
jmp    1278 <main+0x128>
movl   $0x0,-0x24(%rbp)
cmpl   $0x5,-0x24(%rbp)
jge    12df <main+0x18f>
movslq -0x24(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jle    12cc <main+0x17c>
movslq -0x24(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%esi
lea    0xd5d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%esi
sub    -0x24(%rbp),%esi
lea    0xd70(%rip),%rdi
call   1040 <printf@plt>
jmp    12d1 <main+0x181>
mov    -0x24(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x24(%rbp)
jmp    127f <main+0x12f>
movl   $0x1,-0x28(%rbp)
cmpl   $0x5,-0x28(%rbp)
jge    1346 <main+0x1f6>
movslq -0x28(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jle    1333 <main+0x1e3>
movslq -0x28(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%esi
lea    0xcf6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x5,%esi
sub    -0x28(%rbp),%esi
lea    0xd09(%rip),%rdi
call   1040 <printf@plt>
jmp    1338 <main+0x1e8>
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    12e6 <main+0x196>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11ba <main+0x6a>
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x0,%ebp
test   %eax,%eax
je     11ba <main+0x6a>
mov    %eax,%r12d
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
jne    118e <main+0x3e>
xor    %r15d,%r15d
mov    %r12d,%ebp
jmp    11ba <main+0x6a>
mov    %eax,%r15d
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
jne    11a4 <main+0x54>
mov    %r12d,%ebp
jmp    11ba <main+0x6a>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%r13d
mov    %ebp,%r14d
mov    %r12d,%ebp
test   %eax,%eax
jne    11d5 <main+0x85>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe38(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebp,%r12d
xor    %r13d,%r13d
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1204 <main+0xb4>
lea    0xe24(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0xe3d(%rip),%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1235 <main+0xe5>
lea    0xdf4(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
lea    0xe0c(%rip),%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1266 <main+0x116>
lea    0xdc3(%rip),%rdi
mov    %r13d,%esi
call   1040 <printf@plt>
lea    0xddb(%rip),%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1297 <main+0x147>
lea    0xd92(%rip),%rdi
mov    %r12d,%esi
call   1040 <printf@plt>
lea    0xdaa(%rip),%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    12c8 <main+0x178>
lea    0xd61(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
lea    0xd79(%rip),%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Soy el hijo %d\0A\00"
@.str.1 = [39 x i8] c"Acaba de finalizar mi hijo con PID:%d\0A\00"
@.str.2 = [31 x i8] c"Solo me quedan %d hijos vivos\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 5, i32* %6
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %7
%11 = alloca i32, i64 5
%12 = call i32 (...) @fork()
%13 = getelementptr inbounds i32, i32* %11, i64 0
store i32 %12, i32* %13
switch i32 %12, label %17 [
i32 0, label %14
]
14: ; preds = %2
%15 = call i32 (...) @getpid()
%16 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %45
17: ; preds = %2
%18 = call i32 (...) @fork()
%19 = getelementptr inbounds i32, i32* %11, i64 1
store i32 %18, i32* %19
switch i32 %18, label %23 [
i32 0, label %20
]
20: ; preds = %17
%21 = call i32 (...) @getpid()
%22 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %44
23: ; preds = %17
%24 = call i32 (...) @fork()
%25 = getelementptr inbounds i32, i32* %11, i64 2
store i32 %24, i32* %25
switch i32 %24, label %29 [
i32 0, label %26
]
26: ; preds = %23
%27 = call i32 (...) @getpid()
%28 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %43
29: ; preds = %23
%30 = call i32 (...) @fork()
%31 = getelementptr inbounds i32, i32* %11, i64 3
store i32 %30, i32* %31
switch i32 %30, label %35 [
i32 0, label %32
]
32: ; preds = %29
%33 = call i32 (...) @getpid()
%34 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %33)
br label %42
35: ; preds = %29
%36 = call i32 (...) @fork()
%37 = getelementptr inbounds i32, i32* %11, i64 4
store i32 %36, i32* %37
switch i32 %36, label %41 [
i32 0, label %38
]
38: ; preds = %35
%39 = call i32 (...) @getpid()
%40 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %35, %38
br label %42
42: ; preds = %41, %32
br label %43
43: ; preds = %42, %26
br label %44
44: ; preds = %43, %20
br label %45
45: ; preds = %44, %14
store i32 0, i32* %8
br label %46
46: ; preds = %66, %45
%47 = load i32, i32* %8
%48 = icmp slt i32 %47, 5
br i1 %48, label %49, label %69
49: ; preds = %46
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %11, i64 %51
%53 = load i32, i32* %52
%54 = call i64 @waitpid(i32 %53, i32 0, i32 0)
%55 = icmp sgt i64 %54, 0
br i1 %55, label %56, label %65
56: ; preds = %49
%57 = load i32, i32* %8
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i32, i32* %11, i64 %58
%60 = load i32, i32* %59
%61 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %8
%63 = sub nsw i32 5, %62
%64 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %56, %49
br label %66
66: ; preds = %65
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 2
store i32 %68, i32* %8
br label %46
69: ; preds = %46
store i32 1, i32* %9
br label %70
70: ; preds = %90, %69
%71 = load i32, i32* %9
%72 = icmp slt i32 %71, 5
br i1 %72, label %73, label %93
73: ; preds = %70
%74 = load i32, i32* %9
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i32, i32* %11, i64 %75
%77 = load i32, i32* %76
%78 = call i64 @waitpid(i32 %77, i32 0, i32 0)
%79 = icmp sgt i64 %78, 0
br i1 %79, label %80, label %89
80: ; preds = %73
%81 = load i32, i32* %9
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i32, i32* %11, i64 %82
%84 = load i32, i32* %83
%85 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %84)
%86 = load i32, i32* %9
%87 = sub nsw i32 5, %86
%88 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %80, %73
br label %90
90: ; preds = %89
%91 = load i32, i32* %9
%92 = add nsw i32 %91, 2
store i32 %92, i32* %9
br label %70
93: ; preds = %70
%94 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [16 x i8] c"Soy el hijo %d\0A\00"
@.str.1 = [39 x i8] c"Acaba de finalizar mi hijo con PID:%d\0A\00"
@.str.2 = [31 x i8] c"Solo me quedan %d hijos vivos\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %2
%6 = tail call i32 (...) @fork()
%7 = icmp eq i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = tail call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = tail call i32 (...) @fork()
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %11
%15 = tail call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %14, %11, %8, %5, %2
%18 = phi i32 [ undef, %2 ], [ undef, %5 ], [ undef, %8 ], [ 0, %11 ], [ %12, %14 ]
%19 = phi i32 [ undef, %2 ], [ undef, %5 ], [ 0, %8 ], [ %9, %11 ], [ %9, %14 ]
%20 = phi i32 [ undef, %2 ], [ 0, %5 ], [ %6, %8 ], [ %6, %11 ], [ %6, %14 ]
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %17, %14
%24 = phi i32 [ %12, %14 ], [ %18, %17 ]
%25 = phi i32 [ %9, %14 ], [ %19, %17 ]
%26 = phi i32 [ %6, %14 ], [ %20, %17 ]
%27 = phi i32 [ %15, %14 ], [ 0, %17 ]
%28 = tail call i64 @waitpid(i32 %3, i32 0, i32 0)
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %23
%31 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%32 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 5)
br label %33
33: ; preds = %23, %30
%34 = tail call i64 @waitpid(i32 %25, i32 0, i32 0)
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %39
39: ; preds = %36, %33
%40 = tail call i64 @waitpid(i32 %27, i32 0, i32 0)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%44 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %45
45: ; preds = %42, %39
%46 = tail call i64 @waitpid(i32 %26, i32 0, i32 0)
%47 = icmp sgt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%50 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %51
51: ; preds = %45, %48
%52 = tail call i64 @waitpid(i32 %24, i32 0, i32 0)
%53 = icmp sgt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = tail call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%56 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %57
57: ; preds = %54, %51
%58 = tail call i32 @exit(i32 0)
unreachable
}