#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;
  while (1) {
    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\n");
      exit(0);
    } 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\n", getpid());
      sleep(5);
      system("ps -o pid,ppid,state,tty,command");
    }
  }
  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    1245 <main+0xc5>
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>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe34(%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    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xe46(%rip),%rdi
call   1040 <system@plt>
jmp    12bf <main+0x13f>
jmp    118f <main+0xf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1249 <main+0xb9>
mov    %rax,%rbx
lea    0xead(%rip),%r14
lea    0xece(%rip),%r15
lea    0xee9(%rip),%r12
lea    0xf0a(%rip),%r13
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     1276 <main+0xe6>
mov    %r14,%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>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    %r13,%rdi
call   1050 <system@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11d0 <main+0x40>
mov    0x2d90(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xda0(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd9c(%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    0xd9a(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%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 = [39 x i8] c"child process is exited [zombie]....\0A\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 = [40 x i8] c"parent %d : I will sleep 5 seconds...\0A\0A\00"
@.str.7 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
br label %3
3: ; preds = %0, %37
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %3
%8 = load i32, i32* @errno
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %8, i8* %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %3
%14 = load i64, i64* %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %25
16: ; preds = %13
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 (...) @getpid()
%20 = call i32 (...) @getpid()
%21 = call i32 (...) @getppid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20, i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %13
%26 = call i32 (...) @getpid()
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = call i32 (...) @getpid()
%29 = call i32 (...) @getpid()
%30 = call i32 (...) @getppid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32 %29, i32 %30)
%32 = call i32 (...) @getpid()
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %32)
%34 = call i32 @sleep(i32 5)
%35 = call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %36
36: ; preds = %25
br label %37
37: ; preds = %36
br label %3
}

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