#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*/ waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main() {
  printf("this is fork+exec demo\n");
  int count = 0;
  for (; count < 3; ++count) {
    sleep(1);
    int pid = fork();
    switch (pid) {
    case -1:
      perror("fork");
      goto err;
      break;
    case 0:
      printf("child: pid(%d), ppid(%d)\n", getpid(), getppid());
      execlp("sh", "sh", "-c", "date", NULL);
      break;
    default:
      break;
    }
  }
  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    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    125a <main+0xda>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11e6 <main+0x66>
jmp    11d6 <main+0x56>
mov    -0x10(%rbp),%eax
test   %eax,%eax
je     11f7 <main+0x77>
jmp    1242 <main+0xc2>
lea    0xe2b(%rip),%rdi
call   1060 <perror@plt>
jmp    12a0 <main+0x120>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1070 <getppid@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe15(%rip),%rsi
lea    0xe11(%rip),%rdx
lea    0xe0d(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1080 <execlp@plt>
jmp    1247 <main+0xc7>
jmp    1247 <main+0xc7>
jmp    124c <main+0xcc>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a4 <main+0x24>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1286 <main+0x106>
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    1261 <main+0xe1>
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <main+0x127>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11bc <main+0x2c>
cmp    $0xffffffff,%eax
jne    11fe <main+0x6e>
jmp    1276 <main+0xe6>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe32(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe3a(%rip),%rdi
lea    0xe36(%rip),%rdx
lea    0xe32(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
call   1090 <execlp@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     121a <main+0x8a>
cmp    $0xffffffff,%eax
jne    125c <main+0xcc>
jmp    1276 <main+0xe6>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xdd4(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xddc(%rip),%rdi
lea    0xdd8(%rip),%rdx
lea    0xdd4(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
call   1090 <execlp@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1289 <main+0xf9>
cmp    $0xffffffff,%eax
jne    12cb <main+0x13b>
lea    0xd83(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12fa <main+0x16a>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xd65(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd6d(%rip),%rdi
lea    0xd69(%rip),%rdx
lea    0xd65(%rip),%rcx
mov    %rdi,%rsi
xor    %r8d,%r8d
call   1090 <execlp@plt>
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    0xd4c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
pop    %rbx
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 = [16 x i8] c"all child done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %5
5: ; preds = %21, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 3
br i1 %7, label %8, label %24
8: ; preds = %5
%9 = call i32 @sleep(i32 1)
%10 = call i32 (...) @fork()
store i32 %10, i32* %3
%11 = load i32, i32* %3
switch i32 %11, label %19 [
i32 -1, label %12
i32 0, label %14
]
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %35
14: ; preds = %8
%15 = call i32 (...) @getpid()
%16 = call i32 (...) @getppid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16)
%18 = 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 %20
19: ; preds = %8
br label %20
20: ; preds = %19, %14
br label %21
21: ; preds = %20
%22 = load i32, i32* %2
%23 = add nsw i32 %22, 1
store i32 %23, i32* %2
br label %5
24: ; preds = %5
store i32 0, i32* %2
br label %25
25: ; preds = %30, %24
%26 = load i32, i32* %2
%27 = icmp slt i32 %26, 3
br i1 %27, label %28, label %33
28: ; preds = %25
%29 = call i32 @waitpid(i32 0, i32* null, i32 0)
br label %30
30: ; preds = %28
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %25
33: ; preds = %25
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %1
br label %36
35: ; preds = %12
store i32 -1, i32* %1
br label %36
36: ; preds = %35, %33
%37 = load i32, i32* %1
ret i32 %37
}

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 = [23 x i8] c"this is fork+exec demo\00"
@str.7 = [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))
%2 = tail call i32 @sleep(i32 1)
%3 = tail call i32 (...) @fork()
switch i32 %3, label %9 [
i32 -1, label %30
i32 0, label %4
]
4: ; preds = %0
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (...) @getppid()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %5, i32 %6)
%8 = 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 %9
9: ; preds = %0, %4
%10 = tail call i32 @sleep(i32 1)
%11 = tail call i32 (...) @fork()
switch i32 %11, label %17 [
i32 -1, label %30
i32 0, label %12
]
12: ; preds = %9
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (...) @getppid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14)
%16 = 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 %17
17: ; preds = %12, %9
%18 = tail call i32 @sleep(i32 1)
%19 = tail call i32 (...) @fork()
switch i32 %19, label %25 [
i32 -1, label %30
i32 0, label %20
]
20: ; preds = %17
%21 = tail call i32 (...) @getpid()
%22 = tail call i32 (...) @getppid()
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
%24 = 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 %25
25: ; preds = %20, %17
%26 = tail call i32 @waitpid(i32 0, i32* null, i32 0)
%27 = tail call i32 @waitpid(i32 0, i32* null, i32 0)
%28 = tail call i32 @waitpid(i32 0, i32* null, i32 0)
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0))
br label %32
30: ; preds = %17, %9, %0
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
%33 = phi i32 [ -1, %30 ], [ 0, %25 ]
ret i32 %33
}