#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pid_terminado, pid_do_filho1, pid_do_filho_filho1, pid_do_filho2,
      pid_do_filho_filho2;
  printf("O pai iniciou sua execucao (PID=%d)\n", getpid());
  pid_do_filho1 = fork();
  if (pid_do_filho1 < 0) {
    printf("Erro ao criar processo\n");
    return 1;
  } else if (pid_do_filho1 == 0) {
    printf("Esta é a execução do filho1 (PID=%d)\n", getpid());
    pid_do_filho_filho1 = fork();
    if (pid_do_filho_filho1 == 0) {
      printf("Esta é a execucao do filho do filho1 (PID=%d)\n", getpid());
      printf("Filho do Filho1 terminou!\n");
      exit(0);
    } else {
      printf("Filho1 terminou!\n");
      exit(0);
    }
  } else {
    pid_do_filho2 = fork();
    if (pid_do_filho2 == 0) {
      printf("Esta é a execução do filho2 (PID=%d)\n", getpid());
      pid_do_filho_filho2 = fork();
      if (pid_do_filho_filho2 == 0) {
        printf("Esta é a execucao do filho do filho2 (PID=%d)\n", getpid());
        printf("Filho do Filho2 terminou!\n");
        exit(0);
      } else {
        printf("Filho2 terminou!\n");
        exit(0);
      }
    } else {
      wait(NULL);
      printf("Pelo menos 1 processos-filho finalizou\n");
      printf("Pai terminou!\n");
      exit(0);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11a5 <main+0x55>
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret
cmpq   $0x0,-0x18(%rbp)
jne    121e <main+0xce>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1209 <main+0xb9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a2 <main+0x152>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    128d <main+0x13d>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe91(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jns    1197 <main+0x37>
lea    0xfb5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rcx
ret
jne    11dc <main+0x7c>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe7e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    122b <main+0xcb>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe84(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf4c(%rip),%rdi
jmp    1257 <main+0xf7>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    1234 <main+0xd4>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe88(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    1250 <main+0xf0>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xee3(%rip),%rdi
jmp    1257 <main+0xf7>
lea    0xefd(%rip),%rdi
jmp    1257 <main+0xf7>
xor    %edi,%edi
call   1070 <wait@plt>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb0(%rip),%rdi
jmp    1257 <main+0xf7>
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"O pai iniciou sua execucao (PID=%d)\0A\00"
@.str.1 = [24 x i8] c"Erro ao criar processo\0A\00"
@.str.2 = [41 x i8] c"Esta \C3\A9 a execu\C3\A7\C3\A3o do filho1 (PID=%d)\0A\00"
@.str.3 = [48 x i8] c"Esta \C3\A9 a execucao do filho do filho1 (PID=%d)\0A\00"
@.str.4 = [27 x i8] c"Filho do Filho1 terminou!\0A\00"
@.str.5 = [18 x i8] c"Filho1 terminou!\0A\00"
@.str.6 = [41 x i8] c"Esta \C3\A9 a execu\C3\A7\C3\A3o do filho2 (PID=%d)\0A\00"
@.str.7 = [48 x i8] c"Esta \C3\A9 a execucao do filho do filho2 (PID=%d)\0A\00"
@.str.8 = [27 x i8] c"Filho do Filho2 terminou!\0A\00"
@.str.9 = [18 x i8] c"Filho2 terminou!\0A\00"
@.str.10 = [40 x i8] c"Pelo menos 1 processos-filho finalizou\0A\00"
@.str.11 = [15 x i8] c"Pai terminou!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i64 (...) @fork()
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
14: ; preds = %0
%15 = load i64, i64* %3
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %14
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = call i64 (...) @fork()
store i64 %20, i64* %4
%21 = load i64, i64* %4
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %17
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
%30 = call i32 @exit(i32 0)
unreachable
31: ; preds = %14
%32 = call i64 (...) @fork()
store i64 %32, i64* %5
%33 = load i64, i64* %5
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %49
35: ; preds = %31
%36 = call i32 (...) @getpid()
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i32 %36)
%38 = call i64 (...) @fork()
store i64 %38, i64* %6
%39 = load i64, i64* %6
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = call i32 (...) @getpid()
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0))
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %35
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %31
%50 = call i32 @wait(i32* null)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.10, i64 0, i64 0))
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
%53 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [37 x i8] c"O pai iniciou sua execucao (PID=%d)\0A\00"
@.str.2 = [41 x i8] c"Esta \C3\A9 a execu\C3\A7\C3\A3o do filho1 (PID=%d)\0A\00"
@.str.3 = [48 x i8] c"Esta \C3\A9 a execucao do filho do filho1 (PID=%d)\0A\00"
@.str.6 = [41 x i8] c"Esta \C3\A9 a execu\C3\A7\C3\A3o do filho2 (PID=%d)\0A\00"
@.str.7 = [48 x i8] c"Esta \C3\A9 a execucao do filho do filho2 (PID=%d)\0A\00"
@str = [39 x i8] c"Pelo menos 1 processos-filho finalizou\00"
@str.12 = [14 x i8] c"Pai terminou!\00"
@str.13 = [17 x i8] c"Filho2 terminou!\00"
@str.14 = [26 x i8] c"Filho do Filho2 terminou!\00"
@str.15 = [17 x i8] c"Filho1 terminou!\00"
@str.16 = [26 x i8] c"Filho do Filho1 terminou!\00"
@str.17 = [23 x i8] c"Erro ao criar processo\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.17, i64 0, i64 0))
ret i32 1
7: ; preds = %0
%8 = icmp eq i64 %3, 0
br i1 %8, label %9, label %22
9: ; preds = %7
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = tail call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.16, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %9
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.15, i64 0, i64 0))
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %7
%23 = tail call i64 (...) @fork()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %22
%26 = tail call i32 (...) @getpid()
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i32 %26)
%28 = tail call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = tail call i32 (...) @getpid()
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i32 %31)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.14, i64 0, i64 0))
%34 = tail call i32 @exit(i32 0)
unreachable
35: ; preds = %25
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.13, i64 0, i64 0))
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %22
%39 = tail call i32 @wait(i32* null)
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str, i64 0, i64 0))
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.12, i64 0, i64 0))
%42 = tail call i32 @exit(i32 0)
unreachable
}