#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++) {
    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    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    127f <main+0x12f>
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,%r13d
test   %eax,%eax
je     11b7 <main+0x67>
xor    %eax,%eax
call   1070 <fork@plt>
mov    $0x0,%ebp
test   %eax,%eax
je     11b7 <main+0x67>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
jne    118d <main+0x3d>
xor    %r15d,%r15d
mov    %ebx,%ebp
jmp    11b7 <main+0x67>
mov    %eax,%r15d
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fork@plt>
test   %eax,%eax
jne    11a2 <main+0x52>
mov    %ebx,%ebp
jmp    11b7 <main+0x67>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <fork@plt>
mov    %eax,%r12d
mov    %ebp,%r14d
mov    %ebx,%ebp
test   %eax,%eax
jne    11d1 <main+0x81>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe3b(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebp,%ebx
xor    %r12d,%r12d
mov    %r13d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1202 <main+0xb2>
lea    0xe27(%rip),%rdi
mov    %r13d,%esi
call   1040 <printf@plt>
lea    0xe3f(%rip),%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1231 <main+0xe1>
lea    0xdf7(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0xe10(%rip),%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    %r15d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1262 <main+0x112>
lea    0xdc7(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
lea    0xddf(%rip),%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    1293 <main+0x143>
lea    0xd96(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
lea    0xdae(%rip),%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    %r12d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    12c4 <main+0x174>
lea    0xd65(%rip),%rdi
mov    %r12d,%esi
call   1040 <printf@plt>
lea    0xd7d(%rip),%rdi
mov    $0x1,%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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 5, i32* %6
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %7
%10 = alloca i32, i64 5
%11 = call i32 (...) @fork()
%12 = getelementptr inbounds i32, i32* %10, i64 0
store i32 %11, i32* %12
switch i32 %11, label %16 [
i32 0, label %13
]
13: ; preds = %2
%14 = call i32 (...) @getpid()
%15 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %44
16: ; preds = %2
%17 = call i32 (...) @fork()
%18 = getelementptr inbounds i32, i32* %10, i64 1
store i32 %17, i32* %18
switch i32 %17, label %22 [
i32 0, label %19
]
19: ; preds = %16
%20 = call i32 (...) @getpid()
%21 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %43
22: ; preds = %16
%23 = call i32 (...) @fork()
%24 = getelementptr inbounds i32, i32* %10, i64 2
store i32 %23, i32* %24
switch i32 %23, label %28 [
i32 0, label %25
]
25: ; preds = %22
%26 = call i32 (...) @getpid()
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %42
28: ; preds = %22
%29 = call i32 (...) @fork()
%30 = getelementptr inbounds i32, i32* %10, i64 3
store i32 %29, i32* %30
switch i32 %29, label %34 [
i32 0, label %31
]
31: ; preds = %28
%32 = call i32 (...) @getpid()
%33 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %32)
br label %41
34: ; preds = %28
%35 = call i32 (...) @fork()
%36 = getelementptr inbounds i32, i32* %10, i64 4
store i32 %35, i32* %36
switch i32 %35, label %40 [
i32 0, label %37
]
37: ; preds = %34
%38 = call i32 (...) @getpid()
%39 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %34, %37
br label %41
41: ; preds = %40, %31
br label %42
42: ; preds = %41, %25
br label %43
43: ; preds = %42, %19
br label %44
44: ; preds = %43, %13
store i32 0, i32* %8
br label %45
45: ; preds = %65, %44
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 5
br i1 %47, label %48, label %68
48: ; preds = %45
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %10, i64 %50
%52 = load i32, i32* %51
%53 = call i64 @waitpid(i32 %52, i32 0, i32 0)
%54 = icmp sgt i64 %53, 0
br i1 %54, label %55, label %64
55: ; preds = %48
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %10, i64 %57
%59 = load i32, i32* %58
%60 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %8
%62 = sub nsw i32 5, %61
%63 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %55, %48
br label %65
65: ; preds = %64
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %45
68: ; preds = %45
%69 = 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 %26, 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 %26)
%38 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %39
39: ; preds = %36, %33
%40 = tail call i64 @waitpid(i32 %25, 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 %25)
%44 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %45
45: ; preds = %42, %39
%46 = tail call i64 @waitpid(i32 %24, 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 %24)
%50 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %51
51: ; preds = %48, %45
%52 = tail call i64 @waitpid(i32 %27, 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 %27)
%56 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %57
57: ; preds = %54, %51
%58 = tail call i32 @exit(i32 0)
unreachable
}