#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int waitpid(int, int *, int /*<<< orphan*/);
int main() {
  int i = 0, pid_son1 = 9, pid_son2 = 9, pid_son3 = 9;
  pid_son1 = fork();
  i++;
  pid_son2 = fork();
  i++;
  pid_son3 = fork();
  i++;
  if (pid_son1 == 0) {
    _exit(EXIT_SUCCESS);
  } else if (pid_son2 == 0) {
    printf("I'm son 2\n");
    _exit(EXIT_SUCCESS);
  } else if (pid_son3 == 0) {
    printf("I'm son 3\n");
    _exit(EXIT_SUCCESS);
  } else {
    int status;
    printf("I'm father!\n");
    pid_son1 = waitpid(pid_son1, &status, WUNTRACED);
    pid_son2 = waitpid(pid_son2, &status, WUNTRACED);
    sleep(1);
    exit(EXIT_SUCCESS);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x9,-0xc(%rbp)
movl   $0x9,-0x10(%rbp)
movl   $0x9,-0x14(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11dc <main+0x7c>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1200 <main+0xa0>
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1224 <main+0xc4>
lea    0xdfa(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %ebp,%ebp
je     11a1 <main+0x41>
test   %ebx,%ebx
jne    1191 <main+0x31>
lea    0xe87(%rip),%rdi
jmp    119c <main+0x3c>
test   %eax,%eax
jne    11af <main+0x4f>
lea    0xe70(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
lea    0xe4a(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e1e(%rip),%r15
mov    (%r15),%edx
lea    0x4(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <waitpid@plt>
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <waitpid@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
@.str = [11 x i8] c"I'm son 2\0A\00"
@.str.1 = [11 x i8] c"I'm son 3\0A\00"
@.str.2 = [13 x i8] c"I'm father!\0A\00"
@WUNTRACED = global i32 0
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
store i32 0, i32* %2
store i32 9, i32* %3
store i32 9, i32* %4
store i32 9, i32* %5
%7 = call i32 (...) @fork()
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = add nsw i32 %8, 1
store i32 %9, i32* %2
%10 = call i32 (...) @fork()
store i32 %10, i32* %4
%11 = load i32, i32* %2
%12 = add nsw i32 %11, 1
store i32 %12, i32* %2
%13 = call i32 (...) @fork()
store i32 %13, i32* %5
%14 = load i32, i32* %2
%15 = add nsw i32 %14, 1
store i32 %15, i32* %2
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @_exit(i32 %19)
unreachable
21: ; preds = %0
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_SUCCESS
%27 = call i32 @_exit(i32 %26)
unreachable
28: ; preds = %21
%29 = load i32, i32* %5
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_SUCCESS
%34 = call i32 @_exit(i32 %33)
unreachable
35: ; preds = %28
%36 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* %3
%38 = load i32, i32* @WUNTRACED
%39 = call i32 @waitpid(i32 %37, i32* %6, i32 %38)
store i32 %39, i32* %3
%40 = load i32, i32* %4
%41 = load i32, i32* @WUNTRACED
%42 = call i32 @waitpid(i32 %40, i32* %6, i32 %41)
store i32 %42, i32* %4
%43 = call i32 @sleep(i32 1)
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
@WUNTRACED = global i32 0
@str = [12 x i8] c"I'm father!\00"
@str.3 = [10 x i8] c"I'm son 3\00"
@str.4 = [10 x i8] c"I'm son 2\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 (...) @fork()
%3 = tail call i32 (...) @fork()
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %2, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_SUCCESS
%8 = tail call i32 @_exit(i32 %7)
unreachable
9: ; preds = %0
%10 = icmp eq i32 %3, 0
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
%13 = load i32, i32* @EXIT_SUCCESS
%14 = tail call i32 @_exit(i32 %13)
unreachable
15: ; preds = %9
%16 = icmp eq i32 %4, 0
br i1 %16, label %17, label %21
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.3, i64 0, i64 0))
%19 = load i32, i32* @EXIT_SUCCESS
%20 = tail call i32 @_exit(i32 %19)
unreachable
21: ; preds = %15
%22 = bitcast i32* %1 to i8*
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%24 = load i32, i32* @WUNTRACED
%25 = call i32 @waitpid(i32 %2, i32* %1, i32 %24)
%26 = load i32, i32* @WUNTRACED
%27 = call i32 @waitpid(i32 %3, i32* %1, i32 %26)
%28 = call i32 @sleep(i32 1)
%29 = load i32, i32* @EXIT_SUCCESS
%30 = call i32 @exit(i32 %29)
unreachable
}