#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 getppid();
int /*<<< orphan*/ printf(char *, char *, int, int, unsigned int);
int /*<<< orphan*/ sleep(int);
void multiprocesses() {
  int i;
  pid_t child;
  char space[16] = {'\0'};
  unsigned int slot = 0;
  if ((child = fork()) == 0) {
    space[0] = '-';
    goto loop;
  }
  if ((child = fork()) == 0) {
    space[0] = '-';
    for (i = 0; i < 3; i++) {
      if ((child = fork()) == 0) {
        space[1] = '-';
        if (i == 1)
          if (fork() == 0)
            space[2] = '-';
        goto loop;
      }
    }
  }
loop:
  while (1) {
    printf("%sI am process %d from process %d, Now it's %d.\n", space, getpid(),
           getppid(), slot);
    slot++;
    sleep(20);
  }
}


x86 assembly (clang, -O0):
<multiprocesses>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    119d <multiprocesses+0x3d>
movb   $0x2d,-0x20(%rbp)
jmp    1226 <multiprocesses+0xc6>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1221 <multiprocesses+0xc1>
movb   $0x2d,-0x20(%rbp)
movl   $0x0,-0x4(%rbp)
cmpl   $0x3,-0x4(%rbp)
jge    121c <multiprocesses+0xbc>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1209 <multiprocesses+0xa9>
movb   $0x2d,-0x1f(%rbp)
cmpl   $0x1,-0x4(%rbp)
jne    1204 <multiprocesses+0xa4>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11ff <multiprocesses+0x9f>
movb   $0x2d,-0x1e(%rbp)
jmp    1204 <multiprocesses+0xa4>
jmp    1226 <multiprocesses+0xc6>
jmp    120e <multiprocesses+0xae>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11bd <multiprocesses+0x5d>
jmp    1221 <multiprocesses+0xc1>
jmp    1226 <multiprocesses+0xc6>
jmp    122b <multiprocesses+0xcb>
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
mov    -0x24(%rbp),%r8d
lea    0xda8(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    %eax,-0x24(%rbp)
mov    $0x14,%edi
call   1070 <sleep@plt>
jmp    122b <multiprocesses+0xcb>

x86 assembly (clang, -O2):
<multiprocesses>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11a8 <multiprocesses+0x58>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11ac <multiprocesses+0x5c>
movb   $0x2d,(%rsp)
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11a1 <multiprocesses+0x51>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     11f1 <multiprocesses+0xa1>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11ac <multiprocesses+0x5c>
movb   $0x2d,0x1(%rsp)
jmp    11ac <multiprocesses+0x5c>
movb   $0x2d,(%rsp)
lea    0xe4d(%rip),%r14
mov    %rsp,%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %eax,%ecx
mov    %ebp,%r8d
call   1040 <printf@plt>
add    $0x1,%ebp
mov    $0x14,%edi
call   1060 <sleep@plt>
jmp    11c0 <multiprocesses+0x70>
movb   $0x2d,0x1(%rsp)
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11ac <multiprocesses+0x5c>
movb   $0x2d,0x2(%rsp)
jmp    11ac <multiprocesses+0x5c>

LLVM IR (-O0):
@.str = [49 x i8] c"%sI am process %d from process %d, Now it's %d.\0A\00"
define void @multiprocesses() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca [16 x i8]
%4 = alloca i32
%5 = bitcast [16 x i8]* %3 to i8*
store i32 0, i32* %4
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
store i8 45, i8* %9
br label %38
10: ; preds = %0
%11 = call i64 (...) @fork()
store i64 %11, i64* %2
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %10
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
store i8 45, i8* %14
store i32 0, i32* %1
br label %15
15: ; preds = %33, %13
%16 = load i32, i32* %1
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %36
18: ; preds = %15
%19 = call i64 (...) @fork()
store i64 %19, i64* %2
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 1
store i8 45, i8* %22
%23 = load i32, i32* %1
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = call i64 (...) @fork()
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 2
store i8 45, i8* %29
br label %30
30: ; preds = %28, %25
br label %31
31: ; preds = %30, %21
br label %38
32: ; preds = %18
br label %33
33: ; preds = %32
%34 = load i32, i32* %1
%35 = add nsw i32 %34, 1
store i32 %35, i32* %1
br label %15
36: ; preds = %15
br label %37
37: ; preds = %36, %10
br label %38
38: ; preds = %37, %31, %8
br label %39
39: ; preds = %38, %39
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%41 = call i32 (...) @getpid()
%42 = call i32 (...) @getppid()
%43 = load i32, i32* %4
%44 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %40, i32 %41, i32 %42, i32 %43)
%45 = load i32, i32* %4
%46 = add i32 %45, 1
store i32 %46, i32* %4
%47 = call i32 @sleep(i32 20)
br label %39
}

LLVM IR (-O2):
@.str = [49 x i8] c"%sI am process %d from process %d, Now it's %d.\0A\00"
define void @multiprocesses() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %6
5: ; preds = %0
store i8 45, i8* %2
br label %26
6: ; preds = %0
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %6
store i8 45, i8* %2
%10 = tail call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %23, %9
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 1
store i8 45, i8* %13
br label %26
14: ; preds = %20
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 1
store i8 45, i8* %15
%16 = tail call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 2
store i8 45, i8* %19
br label %26
20: ; preds = %9
%21 = tail call i64 (...) @fork()
%22 = icmp eq i64 %21, 0
br i1 %22, label %14, label %23
23: ; preds = %20
%24 = tail call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %12, label %26
26: ; preds = %23, %12, %6, %18, %14, %5
br label %27
27: ; preds = %26, %27
%28 = phi i32 [ %32, %27 ], [ 0, %26 ]
%29 = call i32 (...) @getpid()
%30 = call i32 (...) @getppid()
%31 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %29, i32 %30, i32 %28)
%32 = add i32 %28, 1
%33 = call i32 @sleep(i32 20)
br label %27
}