#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);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
int main() {
  pid_t pid;
  char *message;
  int n, code;
  printf("Iniciando a execucao do fork\n");
  pid = fork();
  printf("Processo em execução: %d\n", pid);
  switch (pid) {
  case -1:
    perror("Falha na execucao do fork! \n");
    exit(1);
  case 0:
    message = "Este eh o processo filho";
    n = 4;
    code = 37;
    break;
  default:
    message = "Este eh o processo pai";
    n = 3;
    break;
  }
  for (; n > 0; n--) {
    puts(message);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
mov    %al,-0x1d(%rbp)
call   1040 <printf@plt>
mov    -0x1d(%rbp),%al
call   1080 <fork@plt>
mov    %eax,%ecx
mov    -0x1d(%rbp),%al
mov    %ecx,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe84(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     11c3 <main+0x63>
jmp    11b3 <main+0x53>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11d9 <main+0x79>
jmp    11f7 <main+0x97>
lea    0xe70(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
lea    0xe77(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x4,-0x14(%rbp)
movl   $0x25,-0x18(%rbp)
jmp    1209 <main+0xa9>
lea    0xe72(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x3,-0x14(%rbp)
jmp    120e <main+0xae>
cmpl   $0x0,-0x14(%rbp)
jle    1239 <main+0xd9>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    120e <main+0xae>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xeff(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
lea    0xe81(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
je     119f <main+0x3f>
cmp    $0xffffffff,%ebx
je     11d3 <main+0x73>
lea    0xeba(%rip),%rbx
mov    $0x3,%ebp
jmp    11ab <main+0x4b>
lea    0xe93(%rip),%rbx
mov    $0x4,%ebp
add    $0x1,%ebp
xchg   %ax,%ax
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
ja     11b0 <main+0x50>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [30 x i8] c"Iniciando a execucao do fork\0A\00"
@.str.1 = [28 x i8] c"Processo em execu\C3\A7\C3\A3o: %d\0A\00"
@.str.2 = [29 x i8] c"Falha na execucao do fork! \0A\00"
@.str.3 = [25 x i8] c"Este eh o processo filho\00"
@.str.4 = [23 x i8] c"Este eh o processo pai\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32, i32* %2
switch i32 %10, label %15 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
store i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8** %3
store i32 4, i32* %4
store i32 37, i32* %5
br label %16
15: ; preds = %0
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8** %3
store i32 3, i32* %4
br label %16
16: ; preds = %15, %14
br label %17
17: ; preds = %24, %16
%18 = load i32, i32* %4
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i8*, i8** %3
%22 = call i32 @puts(i8* %21)
%23 = call i32 @sleep(i32 1)
br label %24
24: ; preds = %20
%25 = load i32, i32* %4
%26 = add nsw i32 %25, -1
store i32 %26, i32* %4
br label %17
27: ; preds = %17
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str.1 = [28 x i8] c"Processo em execu\C3\A7\C3\A3o: %d\0A\00"
@.str.2 = [29 x i8] c"Falha na execucao do fork! \0A\00"
@.str.3 = [25 x i8] c"Este eh o processo filho\00"
@.str.4 = [23 x i8] c"Este eh o processo pai\00"
@str = [29 x i8] c"Iniciando a execucao do fork\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %2)
switch i32 %2, label %7 [
i32 -1, label %4
i32 0, label %8
]
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %0
br label %8
8: ; preds = %0, %7
%9 = phi i32 [ 3, %7 ], [ 4, %0 ]
%10 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), %7 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), %0 ]
br label %11
11: ; preds = %8, %11
%12 = phi i32 [ %9, %8 ], [ %15, %11 ]
%13 = tail call i32 @puts(i8* dereferenceable(1) %10)
%14 = tail call i32 @sleep(i32 1)
%15 = add nsw i32 %12, -1
%16 = icmp ugt i32 %12, 1
br i1 %16, label %11, label %17
17: ; preds = %11
ret i32 0
}