#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 */
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pid1, pid2;
  int i;
  for (i = 0; i < 2; i++) {
    pid1 = fork();
    if (pid1 > 0) {
      pid2 = fork();
      wait(NULL);
    }
    if (pid1 > 0 && pid2 > 0) {
      break;
    }
    printf("Processo %d filho de %d\n", getpid(), getppid());
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    11ee <main+0x9e>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jle    119a <main+0x4a>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
cmpq   $0x0,-0x10(%rbp)
jle    11b5 <main+0x65>
cmpq   $0x0,-0x18(%rbp)
jle    11b5 <main+0x65>
jmp    11ee <main+0x9e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <wait@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1166 <main+0x16>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jle    1173 <main+0x23>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbx
xor    %edi,%edi
call   1060 <wait@plt>
test   %rbx,%rbx
jg     11e3 <main+0x93>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe76(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jle    11bc <main+0x6c>
xor    %eax,%eax
call   1070 <fork@plt>
mov    %rax,%rbx
xor    %edi,%edi
call   1060 <wait@plt>
test   %rbx,%rbx
jg     11e3 <main+0x93>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe2d(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <wait@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Processo %d filho de %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
br label %5
5: ; preds = %27, %0
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 2
br i1 %7, label %8, label %30
8: ; preds = %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %2
%10 = load i64, i64* %2
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i64 (...) @fork()
store i64 %13, i64* %3
%14 = call i32 @wait(i32* null)
br label %15
15: ; preds = %12, %8
%16 = load i64, i64* %2
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* %3
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
br label %30
22: ; preds = %18, %15
%23 = call i32 (...) @getpid()
%24 = call i32 (...) @getppid()
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %24)
%26 = call i32 @wait(i32* null)
br label %27
27: ; preds = %22
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %5
30: ; preds = %21, %5
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"Processo %d filho de %d\0A\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp sgt i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = tail call i32 @wait(i32* null)
%6 = icmp sgt i64 %4, 0
br i1 %6, label %23, label %7
7: ; preds = %0, %3
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (...) @getppid()
%10 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = tail call i32 @wait(i32* null)
%12 = tail call i64 (...) @fork()
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = tail call i64 (...) @fork()
%16 = tail call i32 @wait(i32* null)
%17 = icmp sgt i64 %15, 0
br i1 %17, label %23, label %18
18: ; preds = %14, %7
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (...) @getppid()
%21 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20)
%22 = tail call i32 @wait(i32* null)
br label %23
23: ; preds = %18, %14, %3
ret i32 0
}