#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 */
int atoi(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  pid_t pid;
  int i, n, t;
  if (argc < 3) {
    fprintf(stdout, "Usage: %s <nProc> <sleepTime>\n", argv[0]);
    exit(1);
  }
  n = atoi(argv[1]);
  t = atoi(argv[2]);
  fprintf(stdout, "Shell:%d, This:%d\n", getppid(), getpid());
  for (i = 0; i < n; i++) {
    pid = fork();
    if (pid) {
      pid = fork();
      if (pid)
        break;
      else if (!pid && i == (n - 1)) {
        sleep(t);
        printf("Process terminated. Father:%d, This:%d\n", getppid(), getpid());
      }
    } else if (!pid && i == (n - 1)) {
      sleep(t);
      printf("Process terminated. Father:%d, This:%d\n", getppid(), getpid());
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11c8 <main+0x48>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    %eax,%ecx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    130d <main+0x18d>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12af <main+0x12f>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     125f <main+0xdf>
jmp    130d <main+0x18d>
cmpq   $0x0,-0x18(%rbp)
jne    12a5 <main+0x125>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    12a5 <main+0x125>
mov    -0x24(%rbp),%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x30(%rbp),%esi
mov    %eax,%edx
lea    0xd92(%rip),%rdi
call   1040 <printf@plt>
jmp    12aa <main+0x12a>
jmp    12fa <main+0x17a>
cmpq   $0x0,-0x18(%rbp)
jne    12f5 <main+0x175>
mov    -0x1c(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    12f5 <main+0x175>
mov    -0x24(%rbp),%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x34(%rbp),%esi
mov    %eax,%edx
lea    0xd42(%rip),%rdi
call   1040 <printf@plt>
jmp    12fa <main+0x17a>
jmp    12ff <main+0x17f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1222 <main+0xa2>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    1247 <main+0xc7>
mov    0x8(%rbx),%rdi
call   1060 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1060 <atoi@plt>
mov    %eax,%r15d
mov    0x2e2e(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe54(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r14d,%r14d
jle    123a <main+0xba>
mov    $0x1,%ebp
sub    %r14d,%ebp
lea    0xe46(%rip),%r14
jmp    11f8 <main+0x78>
xchg   %ax,%ax
add    $0x1,%ebp
cmp    $0x1,%ebp
je     123a <main+0xba>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1210 <main+0x90>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    123a <main+0xba>
test   %ebp,%ebp
jne    11f0 <main+0x70>
mov    %r15d,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
jmp    11f0 <main+0x70>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xda6(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [31 x i8] c"Usage: %s <nProc> <sleepTime>\0A\00"
@.str.1 = [19 x i8] c"Shell:%d, This:%d\0A\00"
@.str.2 = [40 x i8] c"Process terminated. Father:%d, This:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stdout
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %8
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %9
%28 = load i32, i32* @stdout
%29 = call i32 (...) @getppid()
%30 = call i32 (...) @getpid()
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30)
store i32 0, i32* %7
br label %32
32: ; preds = %77, %19
%33 = load i32, i32* %7
%34 = load i32, i32* %8
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %80
36: ; preds = %32
%37 = call i64 (...) @fork()
store i64 %37, i64* %6
%38 = load i64, i64* %6
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %61
40: ; preds = %36
%41 = call i64 (...) @fork()
store i64 %41, i64* %6
%42 = load i64, i64* %6
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
br label %80
45: ; preds = %40
%46 = load i64, i64* %6
%47 = icmp ne i64 %46, 0
br i1 %47, label %59, label %48
48: ; preds = %45
%49 = load i32, i32* %7
%50 = load i32, i32* %8
%51 = sub nsw i32 %50, 1
%52 = icmp eq i32 %49, %51
br i1 %52, label %53, label %59
53: ; preds = %48
%54 = load i32, i32* %9
%55 = call i32 @sleep(i32 %54)
%56 = call i32 (...) @getppid()
%57 = call i32 (...) @getpid()
%58 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 %57)
br label %59
59: ; preds = %53, %48, %45
br label %60
60: ; preds = %59
br label %76
61: ; preds = %36
%62 = load i64, i64* %6
%63 = icmp ne i64 %62, 0
br i1 %63, label %75, label %64
64: ; preds = %61
%65 = load i32, i32* %7
%66 = load i32, i32* %8
%67 = sub nsw i32 %66, 1
%68 = icmp eq i32 %65, %67
br i1 %68, label %69, label %75
69: ; preds = %64
%70 = load i32, i32* %9
%71 = call i32 @sleep(i32 %70)
%72 = call i32 (...) @getppid()
%73 = call i32 (...) @getpid()
%74 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %72, i32 %73)
br label %75
75: ; preds = %69, %64, %61
br label %76
76: ; preds = %75, %60
br label %77
77: ; preds = %76
%78 = load i32, i32* %7
%79 = add nsw i32 %78, 1
store i32 %79, i32* %7
br label %32
80: ; preds = %44, %32
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [31 x i8] c"Usage: %s <nProc> <sleepTime>\0A\00"
@.str.1 = [19 x i8] c"Shell:%d, This:%d\0A\00"
@.str.2 = [40 x i8] c"Process terminated. Father:%d, This:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stdout
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = load i32, i32* @stdout
%17 = tail call i32 (...) @getppid()
%18 = tail call i32 (...) @getpid()
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
%20 = icmp sgt i32 %12, 0
br i1 %20, label %21, label %42
21: ; preds = %9
%22 = add nsw i32 %12, -1
br label %23
23: ; preds = %21, %39
%24 = phi i32 [ 0, %21 ], [ %40, %39 ]
%25 = tail call i64 (...) @fork()
%26 = icmp eq i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = tail call i64 (...) @fork()
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = icmp eq i32 %24, %22
br i1 %31, label %34, label %39
32: ; preds = %23
%33 = icmp eq i32 %24, %22
br i1 %33, label %34, label %39
34: ; preds = %32, %30
%35 = tail call i32 @sleep(i32 %15)
%36 = tail call i32 (...) @getppid()
%37 = tail call i32 (...) @getpid()
%38 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %36, i32 %37)
br label %39
39: ; preds = %34, %30, %32
%40 = add nuw nsw i32 %24, 1
%41 = icmp eq i32 %40, %12
br i1 %41, label %42, label %23
42: ; preds = %39, %27, %9
ret i32 0
}