#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*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ usleep(int);
scalar_t__ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                   int /*<<< orphan*/);
int main() {
  printf("this is fork+exec demo\n");
  int count = 0;
  int child_dead_num;
  for (child_dead_num = 0; child_dead_num < 30;) {
    for (; count < 3; ++count) {
      usleep(1500000);
      int pid = fork();
      switch (pid) {
      case -1:
        perror("fork");
        goto err;
        break;
      case 0:
        printf("child: pid(%d), ppid(%d)\n", getpid(), getppid());
        sleep(3);
        execlp("sh", "sh", "-c", "date", NULL);
        break;
      default:
        break;
      }
    }
    if (waitpid(0, NULL, 0) < 0) {
      perror("waitpid");
    } else {
      ++child_dead_num;
      --count;
      printf("child_dea_num:%d, count:%d\n", child_dead_num, count);
    }
  }
  for (count = 0; count < 3; ++count) {
    waitpid(0, NULL, 0);
  }
  printf("all child done\n");
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x1e,-0xc(%rbp)
jge    12dd <main+0x14d>
jmp    11ca <main+0x3a>
cmpl   $0x3,-0x8(%rbp)
jge    128a <main+0xfa>
mov    $0x16e360,%edi
call   10b0 <usleep@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xffffffff,%eax
je     120c <main+0x7c>
jmp    11fc <main+0x6c>
mov    -0x14(%rbp),%eax
test   %eax,%eax
je     121d <main+0x8d>
jmp    1272 <main+0xe2>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
jmp    1323 <main+0x193>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
lea    0xde5(%rip),%rsi
lea    0xde1(%rip),%rdx
lea    0xddd(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1080 <execlp@plt>
jmp    1277 <main+0xe7>
jmp    1277 <main+0xe7>
jmp    127c <main+0xec>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ca <main+0x3a>
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%esi
mov    %edx,%edi
call   1050 <waitpid@plt>
cmp    $0x0,%rax
jge    12b2 <main+0x122>
lea    0xd9a(%rip),%rdi
call   1060 <perror@plt>
jmp    12d8 <main+0x148>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11bb <main+0x2b>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1309 <main+0x179>
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%esi
mov    %edx,%edi
call   1050 <waitpid@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12e4 <main+0x154>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    132a <main+0x19a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
lea    0xe61(%rip),%rbx
lea    0xe5d(%rip),%r15
lea    0xe59(%rip),%r12
xor    %r13d,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0x2,%ebp
ja     1265 <main+0xc5>
add    $0xfffffffd,%ebp
jmp    1236 <main+0x96>
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xdfd(%rip),%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1090 <execlp@plt>
inc    %ebp
je     1260 <main+0xc0>
mov    $0x16e360,%edi
call   10c0 <usleep@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11f0 <main+0x50>
cmp    $0xffffffff,%eax
jne    1232 <main+0x92>
jmp    12e2 <main+0x142>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x3,%ebp
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
js     129b <main+0xfb>
add    $0x1,%r13d
add    $0xffffffff,%ebp
lea    0xdaf(%rip),%rdi
mov    %r13d,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1e,%r13d
jl     11e0 <main+0x40>
jmp    12b1 <main+0x111>
lea    0xd88(%rip),%rdi
call   1070 <perror@plt>
cmp    $0x1e,%r13d
jl     11e0 <main+0x40>
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xd8a(%rip),%rdi
call   1030 <puts@plt>
jmp    12f3 <main+0x153>
lea    0xd17(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%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"this is fork+exec demo\0A\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [26 x i8] c"child: pid(%d), ppid(%d)\0A\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [5 x i8] c"date\00"
@.str.6 = [8 x i8] c"waitpid\00"
@.str.7 = [28 x i8] c"child_dea_num:%d, count:%d\0A\00"
@.str.8 = [16 x i8] c"all child done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
store i32 0, i32* %3
br label %6
6: ; preds = %43, %0
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 30
br i1 %8, label %9, label %44
9: ; preds = %6
br label %10
10: ; preds = %27, %9
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %30
13: ; preds = %10
%14 = call i32 @usleep(i32 1500000)
%15 = call i32 (...) @fork()
store i32 %15, i32* %4
%16 = load i32, i32* %4
switch i32 %16, label %25 [
i32 -1, label %17
i32 0, label %19
]
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %55
19: ; preds = %13
%20 = call i32 (...) @getpid()
%21 = call i32 (...) @getppid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
%23 = call i32 @sleep(i32 3)
%24 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
br label %26
25: ; preds = %13
br label %26
26: ; preds = %25, %19
br label %27
27: ; preds = %26
%28 = load i32, i32* %2
%29 = add nsw i32 %28, 1
store i32 %29, i32* %2
br label %10
30: ; preds = %10
%31 = call i64 @waitpid(i32 0, i32* null, i32 0)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %43
35: ; preds = %30
%36 = load i32, i32* %3
%37 = add nsw i32 %36, 1
store i32 %37, i32* %3
%38 = load i32, i32* %2
%39 = add nsw i32 %38, -1
store i32 %39, i32* %2
%40 = load i32, i32* %3
%41 = load i32, i32* %2
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %40, i32 %41)
br label %43
43: ; preds = %35, %33
br label %6
44: ; preds = %6
store i32 0, i32* %2
br label %45
45: ; preds = %50, %44
%46 = load i32, i32* %2
%47 = icmp slt i32 %46, 3
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = call i64 @waitpid(i32 0, i32* null, i32 0)
br label %50
50: ; preds = %48
%51 = load i32, i32* %2
%52 = add nsw i32 %51, 1
store i32 %52, i32* %2
br label %45
53: ; preds = %45
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* %1
br label %56
55: ; preds = %17
store i32 -1, i32* %1
br label %56
56: ; preds = %55, %53
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [26 x i8] c"child: pid(%d), ppid(%d)\0A\00"
@.str.3 = [3 x i8] c"sh\00"
@.str.4 = [3 x i8] c"-c\00"
@.str.5 = [5 x i8] c"date\00"
@.str.6 = [8 x i8] c"waitpid\00"
@.str.7 = [28 x i8] c"child_dea_num:%d, count:%d\0A\00"
@str = [23 x i8] c"this is fork+exec demo\00"
@str.9 = [15 x i8] c"all child done\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %2
2: ; preds = %0, %31
%3 = phi i32 [ 0, %0 ], [ %33, %31 ]
%4 = phi i32 [ 0, %0 ], [ %32, %31 ]
%5 = icmp ult i32 %4, 3
br i1 %5, label %6, label %21
6: ; preds = %2, %18
%7 = phi i32 [ %19, %18 ], [ %4, %2 ]
%8 = tail call i32 @usleep(i32 1500000)
%9 = tail call i32 (...) @fork()
switch i32 %9, label %18 [
i32 -1, label %16
i32 0, label %10
]
10: ; preds = %6
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (...) @getppid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %12)
%14 = tail call i32 @sleep(i32 3)
%15 = tail call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32* null)
br label %18
16: ; preds = %6
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %40
18: ; preds = %6, %10
%19 = add i32 %7, 1
%20 = icmp eq i32 %19, 3
br i1 %20, label %21, label %6
21: ; preds = %18, %2
%22 = phi i32 [ %4, %2 ], [ 3, %18 ]
%23 = tail call i64 @waitpid(i32 0, i32* null, i32 0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %31
27: ; preds = %21
%28 = add nsw i32 %3, 1
%29 = add nsw i32 %22, -1
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %28, i32 %29)
br label %31
31: ; preds = %27, %25
%32 = phi i32 [ %22, %25 ], [ %29, %27 ]
%33 = phi i32 [ %3, %25 ], [ %28, %27 ]
%34 = icmp slt i32 %33, 30
br i1 %34, label %2, label %35
35: ; preds = %31
%36 = tail call i64 @waitpid(i32 0, i32* null, i32 0)
%37 = tail call i64 @waitpid(i32 0, i32* null, i32 0)
%38 = tail call i64 @waitpid(i32 0, i32* null, i32 0)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
br label %40
40: ; preds = %16, %35
%41 = phi i32 [ 0, %35 ], [ -1, %16 ]
ret i32 %41
}