#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 /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main() {
  pid_t pid;
  if ((pid = fork()) < 0) {
    perror("fork error:");
    exit(-1);
  } else if (pid == 0) {
    printf("first child  %d : pid = %d, ppid = %d\n", getpid(), getpid(),
           getppid());
    if ((pid = fork()) < 0) {
      perror("fork error:");
      exit(-1);
    } else if (pid > 0) {
      printf("first child %d : is exited.\n", getpid());
      exit(0);
    } else {
      printf("second child %d : pid = %d, ppid = %d\n", getpid(), getpid(),
             getppid());
      printf("second child %d : I will sleep 3 second...\n", getpid());
      sleep(3);
      printf("\nsecond child %d : pid = %d, ppid = %d\n\n", getpid(), getpid(),
             getppid());
      system("ps -o pid,ppid,state,tty,command");
      exit(0);
    }
  }
  if (waitpid(pid, NULL, 0) != pid) {
    perror("waitepid error:");
    exit(1);
  }
  exit(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11ca <main+0x3a>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12f0 <main+0x160>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1231 <main+0xa1>
lea    0xdde(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    125a <main+0xca>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xde8(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jmp    12f5 <main+0x165>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     1324 <main+0x194>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    11b5 <main+0x25>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jne    128e <main+0xfe>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe31(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     119f <main+0xf>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
test   %rbx,%rbx
jne    12bf <main+0x12f>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe30(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe3c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe35(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4a(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    %rbx,%rax
jne    12a9 <main+0x119>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd6d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork error:\00"
@.str.1 = [39 x i8] c"first child %d : pid = %d, ppid = %d\0A\00"
@.str.2 = [29 x i8] c"first child %d : is exited.\0A\00"
@.str.3 = [39 x i8] c"second child %d : pid = %d, ppid = %d\0A\00"
@.str.4 = [44 x i8] c"second child %d : I will sleep 3 second...\0A\00"
@.str.5 = [41 x i8] c"\0Asecond child %d : pid = %d, ppid = %d\0A\0A\00"
@.str.6 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@.str.7 = [16 x i8] c"waitepid error:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = load i64, i64* %2
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %42
11: ; preds = %8
%12 = call i32 (...) @getpid()
%13 = call i32 (...) @getpid()
%14 = call i32 (...) @getppid()
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %13, i32 %14)
%16 = call i64 (...) @fork()
store i64 %16, i64* %2
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %11
%22 = load i64, i64* %2
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %21
%29 = call i32 (...) @getpid()
%30 = call i32 (...) @getpid()
%31 = call i32 (...) @getppid()
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %29, i32 %30, i32 %31)
%33 = call i32 (...) @getpid()
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = call i32 @sleep(i32 3)
%36 = call i32 (...) @getpid()
%37 = call i32 (...) @getpid()
%38 = call i32 (...) @getppid()
%39 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %36, i32 %37, i32 %38)
%40 = call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %8
br label %43
43: ; preds = %42
%44 = load i64, i64* %2
%45 = call i64 @waitpid(i64 %44, i32* null, i32 0)
%46 = load i64, i64* %2
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %43
%52 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork error:\00"
@.str.1 = [39 x i8] c"first child %d : pid = %d, ppid = %d\0A\00"
@.str.2 = [29 x i8] c"first child %d : is exited.\0A\00"
@.str.3 = [39 x i8] c"second child %d : pid = %d, ppid = %d\0A\00"
@.str.4 = [44 x i8] c"second child %d : I will sleep 3 second...\0A\00"
@.str.5 = [41 x i8] c"\0Asecond child %d : pid = %d, ppid = %d\0A\0A\00"
@.str.6 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@.str.7 = [16 x i8] c"waitepid error:\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 -1)
unreachable
6: ; preds = %0
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %37
8: ; preds = %6
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (...) @getppid()
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %9, i32 %10, i32 %11)
%13 = tail call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 -1)
unreachable
18: ; preds = %8
%19 = icmp eq i64 %13, 0
%20 = tail call i32 (...) @getpid()
br i1 %19, label %24, label %21
21: ; preds = %18
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %18
%25 = tail call i32 (...) @getpid()
%26 = tail call i32 (...) @getppid()
%27 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %20, i32 %25, i32 %26)
%28 = tail call i32 (...) @getpid()
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = tail call i32 @sleep(i32 3)
%31 = tail call i32 (...) @getpid()
%32 = tail call i32 (...) @getpid()
%33 = tail call i32 (...) @getppid()
%34 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0), i32 %31, i32 %32, i32 %33)
%35 = tail call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%36 = tail call i32 @exit(i32 0)
unreachable
37: ; preds = %6
%38 = tail call i64 @waitpid(i64 %1, i32* null, i32 0)
%39 = icmp eq i64 %38, %1
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %37
%44 = tail call i32 @exit(i32 0)
unreachable
}