#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ fork();
int getpid();
scalar_t__ isPIDFilho(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ testaPID(int /*<<< orphan*/ *);
int main() {
  pid_t pid, pid2;
  pid = fork();
  testaPID(&pid);
  if (isPIDFilho(&pid)) {
    for (int i = 100; i < 200; i++) {
      printf("PID: %d - i: %d\n", getpid(), i);
      sleep(1);
    }
    printf("Filho 1 vai morrer.\n");
  } else {
    pid2 = fork();
    testaPID(&pid2);
    if (isPIDFilho(&pid2)) {
      for (int i = 200; i < 300; i++) {
        printf("PID: %d - i: %d\n", getpid(), i);
        sleep(1);
      }
      printf("Filho 2 vai morrer.\n");
    } else {
      for (int i = 1; i < 51; i++) {
        printf("PID: %d - i: %d\n", getpid(), i);
        sleep(2);
      }
      printf("Processo pai vai morrer.\n");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x8(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <testaPID@plt>
lea    -0x8(%rbp),%rdi
call   1060 <isPIDFilho@plt>
cmp    $0x0,%rax
je     11ee <main+0x8e>
movl   $0x64,-0x10(%rbp)
cmpl   $0xc8,-0x10(%rbp)
jge    11db <main+0x7b>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x10(%rbp),%edx
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    119c <main+0x3c>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c3 <main+0x163>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <testaPID@plt>
lea    -0xc(%rbp),%rdi
call   1060 <isPIDFilho@plt>
cmp    $0x0,%rax
je     126d <main+0x10d>
movl   $0xc8,-0x14(%rbp)
cmpl   $0x12c,-0x14(%rbp)
jge    125a <main+0xfa>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121b <main+0xbb>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12be <main+0x15e>
movl   $0x1,-0x18(%rbp)
cmpl   $0x33,-0x18(%rbp)
jge    12b0 <main+0x150>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x18(%rbp),%edx
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   1070 <sleep@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1274 <main+0x114>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c3 <main+0x163>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <testaPID@plt>
mov    %rbx,%rdi
call   1070 <isPIDFilho@plt>
test   %rax,%rax
je     11df <main+0x6f>
mov    $0x64,%ebp
lea    0xe57(%rip),%rbx
lea    0xe8e(%rip),%r14
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
add    $0x1,%ebp
cmp    $0xc8,%ebp
jne    11b0 <main+0x40>
jmp    1287 <main+0x117>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <testaPID@plt>
mov    %rbx,%rdi
call   1070 <isPIDFilho@plt>
test   %rax,%rax
je     124c <main+0xdc>
mov    $0xc8,%ebp
lea    0xdf0(%rip),%rbx
lea    0xe13(%rip),%r14
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
add    $0x1,%ebp
cmp    $0x12c,%ebp
jne    1220 <main+0xb0>
jmp    1287 <main+0x117>
mov    $0x1,%ebp
lea    0xda8(%rip),%rbx
lea    0xdb2(%rip),%r14
nop
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
add    $0x1,%ebp
cmp    $0x33,%ebp
jne    1260 <main+0xf0>
mov    %r14,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"PID: %d - i: %d\0A\00"
@.str.1 = [21 x i8] c"Filho 1 vai morrer.\0A\00"
@.str.2 = [21 x i8] c"Filho 2 vai morrer.\0A\00"
@.str.3 = [26 x i8] c"Processo pai vai morrer.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = call i32 @testaPID(i32* %2)
%9 = call i64 @isPIDFilho(i32* %2)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %0
store i32 100, i32* %4
br label %12
12: ; preds = %20, %11
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 200
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = load i32, i32* %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
%19 = call i32 @sleep(i32 1)
br label %20
20: ; preds = %15
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %12
23: ; preds = %12
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %59
25: ; preds = %0
%26 = call i32 (...) @fork()
store i32 %26, i32* %3
%27 = call i32 @testaPID(i32* %3)
%28 = call i64 @isPIDFilho(i32* %3)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %25
store i32 200, i32* %5
br label %31
31: ; preds = %39, %30
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 300
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = call i32 (...) @getpid()
%36 = load i32, i32* %5
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36)
%38 = call i32 @sleep(i32 1)
br label %39
39: ; preds = %34
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %31
42: ; preds = %31
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %58
44: ; preds = %25
store i32 1, i32* %6
br label %45
45: ; preds = %53, %44
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 51
br i1 %47, label %48, label %56
48: ; preds = %45
%49 = call i32 (...) @getpid()
%50 = load i32, i32* %6
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %49, i32 %50)
%52 = call i32 @sleep(i32 2)
br label %53
53: ; preds = %48
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %45
56: ; preds = %45
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %42
br label %59
59: ; preds = %58, %23
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"PID: %d - i: %d\0A\00"
@str = [25 x i8] c"Processo pai vai morrer.\00"
@str.4 = [20 x i8] c"Filho 2 vai morrer.\00"
@str.5 = [20 x i8] c"Filho 1 vai morrer.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @fork()
store i32 %5, i32* %1
%6 = call i32 @testaPID(i32* %1)
%7 = call i64 @isPIDFilho(i32* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %0, %9
%10 = phi i32 [ %14, %9 ], [ 100, %0 ]
%11 = call i32 (...) @getpid()
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %10)
%13 = call i32 @sleep(i32 1)
%14 = add nuw nsw i32 %10, 1
%15 = icmp eq i32 %14, 200
br i1 %15, label %35, label %9
16: ; preds = %0
%17 = call i32 (...) @fork()
store i32 %17, i32* %2
%18 = call i32 @testaPID(i32* %2)
%19 = call i64 @isPIDFilho(i32* %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %28, label %21
21: ; preds = %16, %21
%22 = phi i32 [ %26, %21 ], [ 200, %16 ]
%23 = call i32 (...) @getpid()
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %22)
%25 = call i32 @sleep(i32 1)
%26 = add nuw nsw i32 %22, 1
%27 = icmp eq i32 %26, 300
br i1 %27, label %35, label %21
28: ; preds = %16, %28
%29 = phi i32 [ %33, %28 ], [ 1, %16 ]
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %29)
%32 = call i32 @sleep(i32 2)
%33 = add nuw nsw i32 %29, 1
%34 = icmp eq i32 %33, 51
br i1 %34, label %35, label %28
35: ; preds = %9, %21, %28
%36 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0), %28 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0), %21 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0), %9 ]
%37 = call i32 @puts(i8* dereferenceable(1) %36)
ret i32 0
}