#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_SUCCESS;
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pr_ids(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  pid_t pid;
  int status;
  if ((pid = fork()) < 0) {
    perror("fork error");
    return -1;
  } else if (0 == pid) {
    printf("Child process\n");
    pr_ids("child");
    sleep(5);
    exit(EXIT_SUCCESS);
  } else {
    printf("Parent Process before waiting\n");
    waitpid(pid, &status, WUNTRACED);
    printf("Parent Process After waiting\n");
    printf("Parent process\n");
    pr_ids("Parent");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11b3 <main+0x43>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <main+0xd1>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11ef <main+0x7f>
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
lea    0xe48(%rip),%rdi
call   1040 <pr_ids@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe2a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rsi
call   1050 <waitpid@plt>
lea    0xe27(%rip),%rdi
call   1030 <printf@plt>
lea    0xe39(%rip),%rdi
call   1030 <printf@plt>
lea    0xe3d(%rip),%rdi
call   1040 <pr_ids@plt>
jmp    123a <main+0xca>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11d4 <main+0x64>
je     11e7 <main+0x77>
mov    %rax,%rbx
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <waitpid@plt>
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4a(%rip),%rdi
call   1040 <pr_ids@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    11ce <main+0x5e>
lea    0xe74(%rip),%rdi
call   1030 <puts@plt>
lea    0xe11(%rip),%rdi
call   1040 <pr_ids@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [15 x i8] c"Child process\0A\00"
@.str.2 = [6 x i8] c"child\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [31 x i8] c"Parent Process before waiting\0A\00"
@WUNTRACED = global i32 0
@.str.4 = [30 x i8] c"Parent Process After waiting\0A\00"
@.str.5 = [16 x i8] c"Parent process\0A\00"
@.str.6 = [7 x i8] c"Parent\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %6
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp eq i64 0, %13
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @pr_ids(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @sleep(i32 5)
%19 = load i32, i32* @EXIT_SUCCESS
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%23 = load i64, i64* %6
%24 = load i32, i32* @WUNTRACED
%25 = call i32 @waitpid(i64 %23, i32* %7, i32 %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @pr_ids(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %21
br label %30
30: ; preds = %29
store i32 0, i32* %3
br label %31
31: ; preds = %30, %10
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.2 = [6 x i8] c"child\00"
@EXIT_SUCCESS = global i32 0
@WUNTRACED = global i32 0
@.str.6 = [7 x i8] c"Parent\00"
@str = [30 x i8] c"Parent Process before waiting\00"
@str.7 = [29 x i8] c"Parent Process After waiting\00"
@str.8 = [15 x i8] c"Parent process\00"
@str.9 = [14 x i8] c"Child process\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 (...) @fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %24
9: ; preds = %2
%10 = icmp eq i64 %5, 0
br i1 %10, label %11, label %17
11: ; preds = %9
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%13 = tail call i32 @pr_ids(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 5)
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %9
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%19 = load i32, i32* @WUNTRACED
%20 = call i32 @waitpid(i64 %5, i32* %3, i32 %19)
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.7, i64 0, i64 0))
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
%23 = call i32 @pr_ids(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %24
24: ; preds = %17, %7
%25 = phi i32 [ -1, %7 ], [ 0, %17 ]
ret i32 %25
}