#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 /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int getppid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[], char *env[]) {
  pid_t my_pid, parent_pid, child_pid;
  int status;
  my_pid = getpid();
  parent_pid = getppid();
  printf("\n Parent: my pid is %d\n\n", my_pid);
  printf("Parent: my parent's pid is %d\n\n", parent_pid);
  if ((child_pid = fork()) < 0) {
    perror("fork failure");
    exit(1);
  }
  if (child_pid == 0) {
    printf("\nChild: I am a new-born process!\n\n");
    my_pid = getpid();
    parent_pid = getppid();
    printf("Child: my pid is: %d\n\n", my_pid);
    printf("Child: my parent's pid is: %d\n\n", parent_pid);
    printf(
        "Child: I will sleep 3 seconds and then execute - date - command \n\n");
    sleep(3);
    printf("Child: Now, I woke up and am executing date command \n\n");
    execl("/bin/date", "date", 0, 0);
    perror("execl() failure!\n\n");
    printf("This print is after execl() and should not have been executed if "
           "execl were successful! \n\n");
    _exit(1);
  } else {
    printf("\nParent: I created a child process.\n\n");
    printf("Parent: my child's pid is: %d\n\n", child_pid);
    system("ps -acefl | grep ercal");
    printf("\n \n");
    wait(&status);
    printf("\n Parent: my child is dead. I am going to leave.\n \n ");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1229 <main+0x79>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    12d8 <main+0x128>
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe76(%rip),%rdi
lea    0xe74(%rip),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   10a0 <execl@plt>
lea    0xe69(%rip),%rdi
call   1070 <perror@plt>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xec8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xeda(%rip),%rdi
call   1050 <system@plt>
lea    0xee5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x28(%rbp),%rdi
call   10c0 <wait@plt>
lea    0xed2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
mov    %eax,%ebp
lea    0xe24(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe2d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     1259 <main+0x99>
je     126f <main+0xaf>
mov    %eax,%ebx
lea    0xefb(%rip),%rdi
call   1040 <puts@plt>
lea    0xe83(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe93(%rip),%rdi
call   1060 <system@plt>
lea    0xed0(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%rdi
call   10d0 <wait@plt>
lea    0xe88(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xeb5(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <getppid@plt>
mov    %eax,%ebp
lea    0xdb2(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdb9(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe99(%rip),%rdi
call   1040 <puts@plt>
mov    $0x3,%edi
call   10c0 <sleep@plt>
lea    0xec5(%rip),%rdi
call   1040 <puts@plt>
lea    0xda7(%rip),%rdi
lea    0xda5(%rip),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   10b0 <execl@plt>
lea    0xd9a(%rip),%rdi
call   1080 <perror@plt>
lea    0xecc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"\0A Parent: my pid is %d\0A\0A\00"
@.str.1 = [32 x i8] c"Parent: my parent's pid is %d\0A\0A\00"
@.str.2 = [13 x i8] c"fork failure\00"
@.str.3 = [35 x i8] c"\0AChild: I am a new-born process!\0A\0A\00"
@.str.4 = [23 x i8] c"Child: my pid is: %d\0A\0A\00"
@.str.5 = [32 x i8] c"Child: my parent's pid is: %d\0A\0A\00"
@.str.6 = [67 x i8] c"Child: I will sleep 3 seconds and then execute - date - command \0A\0A\00"
@.str.7 = [55 x i8] c"Child: Now, I woke up and am executing date command \0A\0A\00"
@.str.8 = [10 x i8] c"/bin/date\00"
@.str.9 = [5 x i8] c"date\00"
@.str.10 = [19 x i8] c"execl() failure!\0A\0A\00"
@.str.11 = [91 x i8] c"This print is after execl() and should not have been executed if execl were successful! \0A\0A\00"
@.str.12 = [38 x i8] c"\0AParent: I created a child process.\0A\0A\00"
@.str.13 = [32 x i8] c"Parent: my child's pid is: %d\0A\0A\00"
@.str.14 = [23 x i8] c"ps -acefl | grep ercal\00"
@.str.15 = [4 x i8] c"\0A \0A\00"
@.str.16 = [53 x i8] c"\0A Parent: my child is dead. I am going to leave.\0A \0A \00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%12 = call i32 (...) @getpid()
store i32 %12, i32* %8
%13 = call i32 (...) @getppid()
store i32 %13, i32* %9
%14 = load i32, i32* %8
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %9
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32 (...) @fork()
store i32 %18, i32* %10
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %3
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %3
%24 = load i32, i32* %10
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %41
26: ; preds = %23
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 (...) @getpid()
store i32 %28, i32* %8
%29 = call i32 (...) @getppid()
store i32 %29, i32* %9
%30 = load i32, i32* %8
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %9
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.6, i64 0, i64 0))
%35 = call i32 @sleep(i32 3)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.7, i64 0, i64 0))
%37 = call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i32 0, i32 0)
%38 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0))
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str.11, i64 0, i64 0))
%40 = call i32 @_exit(i32 1)
unreachable
41: ; preds = %23
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.12, i64 0, i64 0))
%43 = load i32, i32* %10
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.13, i64 0, i64 0), i32 %43)
%45 = call i32 @system(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.14, i64 0, i64 0))
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.15, i64 0, i64 0))
%47 = call i32 @wait(i32* %11)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.16, i64 0, i64 0))
br label %49
49: ; preds = %41
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"\0A Parent: my pid is %d\0A\0A\00"
@.str.1 = [32 x i8] c"Parent: my parent's pid is %d\0A\0A\00"
@.str.2 = [13 x i8] c"fork failure\00"
@.str.4 = [23 x i8] c"Child: my pid is: %d\0A\0A\00"
@.str.5 = [32 x i8] c"Child: my parent's pid is: %d\0A\0A\00"
@.str.8 = [10 x i8] c"/bin/date\00"
@.str.9 = [5 x i8] c"date\00"
@.str.10 = [19 x i8] c"execl() failure!\0A\0A\00"
@.str.13 = [32 x i8] c"Parent: my child's pid is: %d\0A\0A\00"
@.str.14 = [23 x i8] c"ps -acefl | grep ercal\00"
@.str.16 = [53 x i8] c"\0A Parent: my child is dead. I am going to leave.\0A \0A \00"
@str = [37 x i8] c"\0AParent: I created a child process.\0A\00"
@str.17 = [3 x i8] c"\0A \00"
@str.18 = [34 x i8] c"\0AChild: I am a new-born process!\0A\00"
@str.19 = [66 x i8] c"Child: I will sleep 3 seconds and then execute - date - command \0A\00"
@str.20 = [54 x i8] c"Child: Now, I woke up and am executing date command \0A\00"
@str.21 = [90 x i8] c"This print is after execl() and should not have been executed if execl were successful! \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (...) @getppid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %6)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%10 = tail call i32 (...) @fork()
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
%16 = icmp eq i32 %10, 0
br i1 %16, label %17, label %30
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.18, i64 0, i64 0))
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (...) @getppid()
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %20)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @str.19, i64 0, i64 0))
%24 = tail call i32 @sleep(i32 3)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.20, i64 0, i64 0))
%26 = tail call i32 @execl(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0), i32 0, i32 0)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.10, i64 0, i64 0))
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([90 x i8], [90 x i8]* @str.21, i64 0, i64 0))
%29 = tail call i32 @_exit(i32 1)
unreachable
30: ; preds = %15
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.13, i64 0, i64 0), i32 %10)
%33 = tail call i32 @system(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.14, i64 0, i64 0))
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str.17, i64 0, i64 0))
%35 = call i32 @wait(i32* %4)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.16, i64 0, i64 0))
ret i32 0
}