#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  %d : I will sleep five seconds.\n", getpid());
    sleep(5);
    printf("pid =  %d, ppid = %d\n", getpid(), getppid());
    system("ps -o pid,ppid,state,tty,command");
    printf("child process is exited.\n");
  } else {
    printf("parent %d : I am the parent process...\n", getpid());
    printf("parent %d : pid =  %d, ppid = %d\n", getpid(), getpid(), getppid());
    sleep(1);
    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    1294 <main+0x114>
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,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe0e(%rip),%rdi
call   1040 <system@plt>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f4 <main+0x174>
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,-0x28(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getppid@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f9 <main+0x179>
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     12a6 <main+0x116>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
test   %rbx,%rbx
je     1205 <main+0x75>
lea    0xef2(%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    0xef1(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xef5(%rip),%rdi
jmp    1298 <main+0x108>
lea    0xe0d(%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    0xe0b(%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    0xe12(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <getppid@plt>
lea    0xdd2(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdfe(%rip),%rdi
call   1050 <system@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2d33(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xd43(%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 = [40 x i8] c"child %d : I will sleep five seconds.\0A\00"
@.str.4 = [22 x i8] c"pid = %d, ppid = %d\0A\00"
@.str.5 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@.str.6 = [26 x i8] c"child process is exited.\0A\00"
@.str.7 = [40 x i8] c"parent %d : I am the parent process...\0A\00"
@.str.8 = [34 x i8] c"parent %d : pid = %d, ppid = %d\0A\00"
@.str.9 = [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 %30
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 (...) @getpid()
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i32 @sleep(i32 5)
%25 = call i32 (...) @getpid()
%26 = call i32 (...) @getppid()
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %25, i32 %26)
%28 = call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %39
30: ; preds = %12
%31 = call i32 (...) @getpid()
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %31)
%33 = call i32 (...) @getpid()
%34 = call i32 (...) @getpid()
%35 = call i32 (...) @getppid()
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 %33, i32 %34, i32 %35)
%37 = call i32 @sleep(i32 1)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %39
39: ; preds = %30, %15
br label %40
40: ; preds = %39
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.3 = [40 x i8] c"child %d : I will sleep five seconds.\0A\00"
@.str.4 = [22 x i8] c"pid = %d, ppid = %d\0A\00"
@.str.5 = [33 x i8] c"ps -o pid,ppid,state,tty,command\00"
@.str.7 = [40 x i8] c"parent %d : I am the parent process...\0A\00"
@.str.8 = [34 x i8] c"parent %d : pid = %d, ppid = %d\0A\00"
@str = [27 x i8] c"father process is exited.\00"
@str.10 = [25 x i8] c"child process is exited.\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 %24
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)
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = tail call i32 @sleep(i32 5)
%20 = tail call i32 (...) @getpid()
%21 = tail call i32 (...) @getppid()
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %20, i32 %21)
%23 = tail call i32 @system(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %31
24: ; preds = %8
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0), i32 %10)
%26 = tail call i32 (...) @getpid()
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 (...) @getppid()
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i32 %26, i32 %27, i32 %28)
%30 = tail call i32 @sleep(i32 1)
br label %31
31: ; preds = %11, %24
%32 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @str.10, i64 0, i64 0), %11 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0), %24 ]
%33 = tail call i32 @puts(i8* dereferenceable(1) %32)
ret i32 0
}