#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  pid_t pid;
  int x = 5;
  pid = fork();
  x++;
  if (pid == -1) {
    printf("Process Creation Error. \n");
    exit(-1);
  } else if (!pid) {
    printf("Child Process : \n");
    printf("Process ID (Child) is %d \n", getpid());
    printf("Value of x is %d \n", x);
    printf("Process ID of Parent is %d \n", getppid());
  } else {
    printf("Parent Process : \n");
    printf("Process ID (Parent) is %d \n", getpid());
    printf("Value of x is %d \n", x);
    printf("Process ID of Shell is %d \n", getppid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x5,-0xc(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    119b <main+0x4b>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11f7 <main+0xa7>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1244 <main+0xf4>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getppid@plt>
mov    %eax,%esi
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1249 <main+0xf9>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     118b <main+0x2b>
cmp    $0xffffffff,%eax
je     11e8 <main+0x88>
lea    0xeec(%rip),%r14
lea    0xec9(%rip),%rbx
lea    0xf0b(%rip),%rdi
jmp    11a0 <main+0x40>
lea    0xe9c(%rip),%r14
lea    0xe67(%rip),%rbx
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe5c(%rip),%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <getppid@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Process Creation Error. \0A\00"
@.str.1 = [18 x i8] c"Child Process : \0A\00"
@.str.2 = [27 x i8] c"Process ID (Child) is %d \0A\00"
@.str.3 = [19 x i8] c"Value of x is %d \0A\00"
@.str.4 = [29 x i8] c"Process ID of Parent is %d \0A\00"
@.str.5 = [19 x i8] c"Parent Process : \0A\00"
@.str.6 = [28 x i8] c"Process ID (Parent) is %d \0A\00"
@.str.7 = [28 x i8] c"Process ID of Shell is %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 5, i32* %3
%4 = call i32 (...) @fork()
store i32 %4, i32* %2
%5 = load i32, i32* %3
%6 = add nsw i32 %5, 1
store i32 %6, i32* %3
%7 = load i32, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = load i32, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = call i32 (...) @getppid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %21)
br label %31
23: ; preds = %12
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 (...) @getpid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = call i32 (...) @getppid()
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %23, %15
br label %32
32: ; preds = %31
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [27 x i8] c"Process ID (Child) is %d \0A\00"
@.str.3 = [19 x i8] c"Value of x is %d \0A\00"
@.str.4 = [29 x i8] c"Process ID of Parent is %d \0A\00"
@.str.6 = [28 x i8] c"Process ID (Parent) is %d \0A\00"
@.str.7 = [28 x i8] c"Process ID of Shell is %d \0A\00"
@str = [17 x i8] c"Child Process : \00"
@str.8 = [18 x i8] c"Parent Process : \00"
@str.9 = [25 x i8] c"Process Creation Error. \00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %5 [
i32 -1, label %2
i32 0, label %6
]
2: ; preds = %0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%4 = tail call i32 @exit(i32 -1)
unreachable
5: ; preds = %0
br label %6
6: ; preds = %0, %5
%7 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0), %5 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0), %0 ]
%8 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), %5 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), %0 ]
%9 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), %5 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), %0 ]
%10 = tail call i32 @puts(i8* dereferenceable(1) %7)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %8, i32 %11)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 6)
%14 = tail call i32 (...) @getppid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %9, i32 %14)
ret i32 0
}