#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 /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main() {
  pid_t pid;
  if ((pid = fork()) < 0) {
    printf("Error : fork \n");
  } else if (pid == 0) {
    if ((pid = fork()) < 0)
      printf("Error : fork \n");
    else if (pid > 0)
      exit(0);
    else {
      sleep(2);
      printf("\nSecond forked process. PID = %d, Parent's PID = %d\n", getpid(),
             getppid());
      exit(0);
    }
  } else {
    waitpid(pid, NULL, 0);
    system("ps -o pid,ppid,state,tty,comm");
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11b7 <main+0x37>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    125d <main+0xdd>
cmpq   $0x0,-0x10(%rbp)
jne    1236 <main+0xb6>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    11ea <main+0x6a>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1231 <main+0xb1>
cmpq   $0x0,-0x10(%rbp)
jle    11fc <main+0x7c>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
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    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1258 <main+0xd8>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xdf8(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    125d <main+0xdd>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11ab <main+0x1b>
jne    11bb <main+0x2b>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jns    11da <main+0x4a>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <waitpid@plt>
lea    0xe67(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>
jne    1208 <main+0x78>
mov    $0x2,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe03(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Error : fork \0A\00"
@.str.1 = [53 x i8] c"\0ASecond forked process. PID = %d, Parent's PID = %d\0A\00"
@.str.2 = [30 x i8] c"ps -o pid,ppid,state,tty,comm\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 %7
5: ; preds = %0
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %33
7: ; preds = %0
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %7
%11 = call i64 (...) @fork()
store i64 %11, i64* %2
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %26
15: ; preds = %10
%16 = load i64, i64* %2
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %15
%21 = call i32 @sleep(i32 2)
%22 = call i32 (...) @getpid()
%23 = call i32 (...) @getppid()
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %13
br label %32
27: ; preds = %7
%28 = load i64, i64* %2
%29 = call i32 @waitpid(i64 %28, i32* null, i32 0)
%30 = call i32 @system(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %26
br label %33
33: ; preds = %32, %5
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [53 x i8] c"\0ASecond forked process. PID = %d, Parent's PID = %d\0A\00"
@.str.2 = [30 x i8] c"ps -o pid,ppid,state,tty,comm\00"
@str.3 = [14 x i8] c"Error : fork \00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %22, label %3
3: ; preds = %0
%4 = icmp eq i64 %1, 0
br i1 %4, label %5, label %18
5: ; preds = %3
%6 = tail call i64 (...) @fork()
%7 = icmp slt i64 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %5
%9 = icmp eq i64 %6, 0
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %8
%13 = tail call i32 @sleep(i32 2)
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (...) @getppid()
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %15)
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %3
%19 = tail call i32 @waitpid(i64 %1, i32* null, i32 0)
%20 = tail call i32 @system(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %5, %0
%23 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.3, i64 0, i64 0), %0 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.3, i64 0, i64 0), %5 ]
%24 = tail call i32 @puts(i8* dereferenceable(1) %23)
ret i32 0
}