#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 errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
char *strerror(int);
int /*<<< orphan*/ system(char *);
int main() {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    printf("fork error errno = %d:%s", errno, strerror(errno));
    exit(1);
  } else if (pid == 0) {
    printf("child  %d : I am the child process...\n", getpid());
    printf("child  %d : pid =  %d, ppid = %d\n", getpid(), getpid(), getppid());
    printf("child process is exited [zombie]....\n");
  } else {
    printf("parent %d : I am the parent process...\n", getpid());
    printf("parent %d : pid =  %d, ppid = %d\n", getpid(), getpid(), getppid());
    printf("parent %d : I will sleep 5 seconds...\n", getpid());
    sleep(5);
    system("ps -o pid,ppid,state,tty,command");
    printf("father process is  exited.\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11dd <main+0x5d>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rdx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1243 <main+0xc3>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    %eax,%ecx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12c6 <main+0x146>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xe46(%rip),%rdi
call   1040 <system@plt>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12cb <main+0x14b>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1277 <main+0xe7>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     1225 <main+0x95>
lea    0xea9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xea8(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeaf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
lea    0xebc(%rip),%rdi
call   1050 <system@plt>
lea    0xed1(%rip),%rdi
jmp    1269 <main+0xd9>
lea    0xded(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xdeb(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d62(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xd72(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"fork error errno = %d:%s\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"child %d : I am the child process...\0A\00"
@.str.2 = [34 x i8] c"child %d : pid = %d, ppid = %d\0A\00"
@.str.3 = [38 x i8] c"child process is exited [zombie]....\0A\00"
@.str.4 = [40 x i8] c"parent %d : I am the parent process...\0A\00"
@.str.5 = [34 x i8] c"parent %d : pid = %d, ppid = %d\0A\00"
@.str.6 = [39 x i8] c"parent %d : I will sleep 5 seconds...\0A\00"
@.str.7 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@.str.8 = [28 x i8] c"father process is exited.\0A\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 = load i64, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = load i32, i32* @errno
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %9)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i64, i64* %2
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32 (...) @getpid()
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getppid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19, i32 %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %35
23: ; preds = %12
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = call i32 (...) @getpid()
%27 = call i32 (...) @getpid()
%28 = call i32 (...) @getppid()
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %26, i32 %27, i32 %28)
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%32 = call i32 @sleep(i32 5)
%33 = call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %35
35: ; preds = %23, %15
br label %36
36: ; preds = %35
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"fork error errno = %d:%s\00"
@errno = global i32 0
@.str.1 = [39 x i8] c"child %d : I am the child process...\0A\00"
@.str.2 = [34 x i8] c"child %d : pid = %d, ppid = %d\0A\00"
@.str.4 = [40 x i8] c"parent %d : I am the parent process...\0A\00"
@.str.5 = [34 x i8] c"parent %d : pid = %d, ppid = %d\0A\00"
@.str.6 = [39 x i8] c"parent %d : I will sleep 5 seconds...\0A\00"
@.str.7 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@str = [27 x i8] c"father process is exited.\00"
@str.9 = [37 x i8] c"child process is exited [zombie]....\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %8
3: ; preds = %0
%4 = load i32, i32* @errno
%5 = tail call i8* @strerror(i32 %4)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %4, i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %1, 0
%10 = tail call i32 (...) @getpid()
br i1 %9, label %11, label %17
11: ; preds = %8
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (...) @getppid()
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14, i32 %15)
br label %27
17: ; preds = %8
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %10)
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (...) @getppid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %19, i32 %20, i32 %21)
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %23)
%25 = tail call i32 @sleep(i32 5)
%26 = tail call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %27
27: ; preds = %11, %17
%28 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0), %11 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0), %17 ]
%29 = tail call i32 @puts(i8* dereferenceable(1) %28)
ret i32 0
}