#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 /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char const *argv[]) {
  for (size_t i = 0; i < 4; i++) {
    pid_t pid = fork();
    if (pid > 0) {
      printf("Processo pai %i criou %i\n", getpid(), pid);
      wait(NULL);
    } else {
      printf("Processo filho %i\n", getpid());
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x4,-0x18(%rbp)
jae    11da <main+0x9a>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    11a8 <main+0x68>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <wait@plt>
jmp    11c4 <main+0x84>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11da <main+0x9a>
jmp    11c9 <main+0x89>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    115e <main+0x1e>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    120b <main+0xcb>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe9e(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <wait@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    120b <main+0xcb>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe6a(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <wait@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    120b <main+0xcb>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe3a(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <wait@plt>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
jle    120b <main+0xcb>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe0a(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <wait@plt>
jmp    1222 <main+0xe2>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe01(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

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

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