#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void testfork() {
  printf("*********************************************\n");
  printf("Testing fork, waitpid, sleep system calls \n \n");
  char *c = (char *)malloc(20);
  strcpy(c, "shashi");
  printf("parent::string is : %s \n", c);
  printf("I am in parent \n \n");
  int pid = fork();
  if (pid == 0) {
    printf("I am in child \n");
    printf("child::string is : %s \n", c);
    strcpy(c, "ranjan");
    printf("child::after modification: %s \n", c);
    char *d = (char *)malloc(20);
    strcpy(d, "ashish");
    printf("child::string d is : %s \n", d);
    c = (char *)malloc(20);
    strcpy(c, "goel");
    printf("child::string d is : %s \n", c);
    sleep(5);
    printf("child: after sleep \n");
    printf("child: Dying!!! \n");
    exit(1);
  }
  int status;
  printf("waiting for child to finish \n");
  waitpid(pid, &status, 0);
  printf("Parent: Dying!!!!! \n");
}


x86 assembly (clang, -O0):
<testfork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb0(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rsi
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    12b8 <testfork+0x148>
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xea7(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rsi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9e(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rsi
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x14,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%rsi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testfork>:
push   %rbp
push   %rbx
push   %rax
lea    0xefb(%rip),%rdi
call   1040 <puts@plt>
lea    0xf1d(%rip),%rdi
call   1040 <puts@plt>
mov    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe51(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
lea    0xe49(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xf11(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     120e <testfork+0x8e>
mov    %eax,%ebp
lea    0xf0a(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
lea    0xf0d(%rip),%rdi
call   1040 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xf0e(%rip),%rdi
call   1040 <puts@plt>
lea    0xdff(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe06(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xdfe(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe00(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
lea    0xdf8(%rip),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x14,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xdf1(%rip),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0xe7d(%rip),%rdi
call   1040 <puts@plt>
lea    0xe85(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"*********************************************\0A\00"
@.str.1 = [46 x i8] c"Testing fork, waitpid, sleep system calls \0A \0A\00"
@.str.2 = [7 x i8] c"shashi\00"
@.str.3 = [25 x i8] c"parent::string is : %s \0A\00"
@.str.4 = [19 x i8] c"I am in parent \0A \0A\00"
@.str.5 = [16 x i8] c"I am in child \0A\00"
@.str.6 = [24 x i8] c"child::string is : %s \0A\00"
@.str.7 = [7 x i8] c"ranjan\00"
@.str.8 = [32 x i8] c"child::after modification: %s \0A\00"
@.str.9 = [7 x i8] c"ashish\00"
@.str.10 = [26 x i8] c"child::string d is : %s \0A\00"
@.str.11 = [5 x i8] c"goel\00"
@.str.12 = [21 x i8] c"child: after sleep \0A\00"
@.str.13 = [18 x i8] c"child: Dying!!! \0A\00"
@.str.14 = [30 x i8] c"waiting for child to finish \0A\00"
@.str.15 = [21 x i8] c"Parent: Dying!!!!! \0A\00"
define void @testfork() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%7 = call i64 @malloc(i32 20)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %1
%9 = load i8*, i8** %1
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%11 = load i8*, i8** %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32 (...) @fork()
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %41
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%19 = load i8*, i8** %1
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %1
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%23 = load i8*, i8** %1
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i8* %23)
%25 = call i64 @malloc(i32 20)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %3
%27 = load i8*, i8** %3
%28 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%29 = load i8*, i8** %3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i8* %29)
%31 = call i64 @malloc(i32 20)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %1
%33 = load i8*, i8** %1
%34 = call i32 @strcpy(i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0))
%35 = load i8*, i8** %1
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i8* %35)
%37 = call i32 @sleep(i32 5)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.13, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.14, i64 0, i64 0))
%43 = load i32, i32* %2
%44 = call i32 @waitpid(i32 %43, i32* %4, i32 0)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.15, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.2 = [7 x i8] c"shashi\00"
@.str.3 = [25 x i8] c"parent::string is : %s \0A\00"
@.str.6 = [24 x i8] c"child::string is : %s \0A\00"
@.str.7 = [7 x i8] c"ranjan\00"
@.str.8 = [32 x i8] c"child::after modification: %s \0A\00"
@.str.9 = [7 x i8] c"ashish\00"
@.str.10 = [26 x i8] c"child::string d is : %s \0A\00"
@.str.11 = [5 x i8] c"goel\00"
@str = [46 x i8] c"*********************************************\00"
@str.16 = [45 x i8] c"Testing fork, waitpid, sleep system calls \0A \00"
@str.17 = [18 x i8] c"I am in parent \0A \00"
@str.18 = [29 x i8] c"waiting for child to finish \00"
@str.19 = [20 x i8] c"Parent: Dying!!!!! \00"
@str.20 = [15 x i8] c"I am in child \00"
@str.21 = [20 x i8] c"child: after sleep \00"
@str.22 = [17 x i8] c"child: Dying!!! \00"
define void @testfork() {
%1 = alloca i32
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.16, i64 0, i64 0))
%4 = tail call i64 @malloc(i32 20)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.17, i64 0, i64 0))
%9 = tail call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %28
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.20, i64 0, i64 0))
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %5)
%14 = tail call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i8* %5)
%16 = tail call i64 @malloc(i32 20)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i8* %17)
%20 = tail call i64 @malloc(i32 20)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0))
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.10, i64 0, i64 0), i8* %21)
%24 = tail call i32 @sleep(i32 5)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.21, i64 0, i64 0))
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.22, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %0
%29 = bitcast i32* %1 to i8*
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.18, i64 0, i64 0))
%31 = call i32 @waitpid(i32 %9, i32* %1, i32 0)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.19, i64 0, i64 0))
ret void
}