#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 /*<<< orphan*/ getpid();
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  pid_t pid_filho1, pid_filho2;
  int i;
  pid_filho1 = fork();
  if (pid_filho1 < 0) {
    printf("Erro ao criar processo\n");
    return 1;
  } else if (pid_filho1 == 0) {
    printf("\nFILHO 1 foi criado\n");
    for (i = 100; i <= 199; i++) {
      printf("[PID=%D;PID_DO_PAI=%d] %d\n", getpid(), getppid(), i);
      sleep(1);
    }
    printf("\nFILHO 1 vai morrer\n");
  } else {
    pid_filho2 = fork();
    if (pid_filho2 == 0) {
      printf("\nFILHO 2 foi criado\n");
      for (i = 200; i <= 299; i++) {
        printf("[PID=%D;PID_DO_PAI=%d] %d\n", getpid(), getppid(), i);
        sleep(1);
      }
      printf("\nFILHO 2 vai morrer\n");
    } else {
      for (i = 1; i <= 50; i++) {
        printf("[PID=%D;PID_DO_PAI=%d] %d\n", getpid(), getppid(), i);
        sleep(2);
      }
      printf("\nProcesso PAI vai morrer\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    118f <main+0x3f>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1307 <main+0x1b7>
cmpq   $0x0,-0x10(%rbp)
jne    120e <main+0xbe>
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x64,-0x1c(%rbp)
cmpl   $0xc7,-0x1c(%rbp)
jg     11fb <main+0xab>
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
mov    -0x1c(%rbp),%ecx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11af <main+0x5f>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fb <main+0x1ab>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1298 <main+0x148>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xc8,-0x1c(%rbp)
cmpl   $0x12b,-0x1c(%rbp)
jg     1285 <main+0x135>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
mov    -0x1c(%rbp),%ecx
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1239 <main+0xe9>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f6 <main+0x1a6>
movl   $0x1,-0x1c(%rbp)
cmpl   $0x32,-0x1c(%rbp)
jg     12e8 <main+0x198>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
mov    -0x1c(%rbp),%ecx
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1060 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    129f <main+0x14f>
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12fb <main+0x1ab>
jmp    1300 <main+0x1b0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11d7 <main+0x77>
je     11e9 <main+0x89>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1249 <main+0xe9>
mov    $0x1,%ebx
lea    0xe6b(%rip),%r15
lea    0xe7f(%rip),%r14
xor    %r13d,%r13d
nop
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
add    $0x1,%ebx
cmp    $0x33,%ebx
jne    11a0 <main+0x40>
jmp    12a7 <main+0x147>
mov    $0x1,%r13d
lea    0xea0(%rip),%r14
jmp    12a7 <main+0x147>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x64,%ebx
lea    0xdff(%rip),%r15
lea    0xe68(%rip),%r14
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %r15,%rdi
mov    %r12d,%esi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
add    $0x1,%ebx
cmp    $0xc8,%ebx
jne    1210 <main+0xb0>
jmp    12a7 <main+0x147>
lea    0xde4(%rip),%rdi
call   1030 <puts@plt>
mov    $0xc8,%ebx
lea    0xd9f(%rip),%r15
lea    0xde0(%rip),%r14
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %r15,%rdi
mov    %r12d,%esi
mov    %eax,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
add    $0x1,%ebx
cmp    $0x12c,%ebx
jne    1270 <main+0x110>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Erro ao criar processo\0A\00"
@.str.1 = [21 x i8] c"\0AFILHO 1 foi criado\0A\00"
@.str.2 = [27 x i8] c"[PID=%D;PID_DO_PAI=%d] %d\0A\00"
@.str.3 = [21 x i8] c"\0AFILHO 1 vai morrer\0A\00"
@.str.4 = [21 x i8] c"\0AFILHO 2 foi criado\0A\00"
@.str.5 = [21 x i8] c"\0AFILHO 2 vai morrer\0A\00"
@.str.6 = [26 x i8] c"\0AProcesso PAI vai morrer\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %67
10: ; preds = %0
%11 = load i64, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 100, i32* %4
br label %15
15: ; preds = %24, %13
%16 = load i32, i32* %4
%17 = icmp sle i32 %16, 199
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = load i32, i32* %4
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20, i32 %21)
%23 = call i32 @sleep(i32 1)
br label %24
24: ; preds = %18
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %15
27: ; preds = %15
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %65
29: ; preds = %10
%30 = call i64 (...) @fork()
store i64 %30, i64* %3
%31 = load i64, i64* %3
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %49
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 200, i32* %4
br label %35
35: ; preds = %44, %33
%36 = load i32, i32* %4
%37 = icmp sle i32 %36, 299
br i1 %37, label %38, label %47
38: ; preds = %35
%39 = call i32 (...) @getpid()
%40 = call i32 (...) @getppid()
%41 = load i32, i32* %4
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %40, i32 %41)
%43 = call i32 @sleep(i32 1)
br label %44
44: ; preds = %38
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %35
47: ; preds = %35
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %64
49: ; preds = %29
store i32 1, i32* %4
br label %50
50: ; preds = %59, %49
%51 = load i32, i32* %4
%52 = icmp sle i32 %51, 50
br i1 %52, label %53, label %62
53: ; preds = %50
%54 = call i32 (...) @getpid()
%55 = call i32 (...) @getppid()
%56 = load i32, i32* %4
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %54, i32 %55, i32 %56)
%58 = call i32 @sleep(i32 2)
br label %59
59: ; preds = %53
%60 = load i32, i32* %4
%61 = add nsw i32 %60, 1
store i32 %61, i32* %4
br label %50
62: ; preds = %50
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %64
64: ; preds = %62, %47
br label %65
65: ; preds = %64, %27
br label %66
66: ; preds = %65
store i32 0, i32* %1
br label %67
67: ; preds = %66, %8
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str.2 = [27 x i8] c"[PID=%D;PID_DO_PAI=%d] %d\0A\00"
@str = [25 x i8] c"\0AProcesso PAI vai morrer\00"
@str.7 = [20 x i8] c"\0AFILHO 2 foi criado\00"
@str.8 = [20 x i8] c"\0AFILHO 2 vai morrer\00"
@str.9 = [20 x i8] c"\0AFILHO 1 foi criado\00"
@str.10 = [20 x i8] c"\0AFILHO 1 vai morrer\00"
@str.11 = [23 x i8] c"Erro ao criar processo\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %36, label %3
3: ; preds = %0
%4 = icmp eq i64 %1, 0
br i1 %4, label %5, label %15
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ 100, %5 ], [ %13, %7 ]
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (...) @getppid()
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 %10, i32 %8)
%12 = tail call i32 @sleep(i32 1)
%13 = add nuw nsw i32 %8, 1
%14 = icmp eq i32 %13, 200
br i1 %14, label %36, label %7
15: ; preds = %3
%16 = tail call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
br label %20
20: ; preds = %18, %20
%21 = phi i32 [ 200, %18 ], [ %26, %20 ]
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 (...) @getppid()
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %23, i32 %21)
%25 = tail call i32 @sleep(i32 1)
%26 = add nuw nsw i32 %21, 1
%27 = icmp eq i32 %26, 300
br i1 %27, label %36, label %20
28: ; preds = %15, %28
%29 = phi i32 [ %34, %28 ], [ 1, %15 ]
%30 = tail call i32 (...) @getpid()
%31 = tail call i32 (...) @getppid()
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %30, i32 %31, i32 %29)
%33 = tail call i32 @sleep(i32 2)
%34 = add nuw nsw i32 %29, 1
%35 = icmp eq i32 %34, 51
br i1 %35, label %36, label %28
36: ; preds = %28, %20, %7, %0
%37 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0), %0 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0), %7 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.8, i64 0, i64 0), %20 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0), %28 ]
%38 = phi i32 [ 1, %0 ], [ 0, %7 ], [ 0, %20 ], [ 0, %28 ]
%39 = tail call i32 @puts(i8* dereferenceable(1) %37)
ret i32 %38
}