#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*/ WNOHANG;
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);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  pid_t pid, _pid;
  int status;
  if ((pid = fork()) < 0) {
    perror("fork error");
    return -1;
  } else if (0 == pid) {
    printf("Child process\n");
    pr_ids("child");
    printf("Child process running\n");
    sleep(5);
    exit(EXIT_SUCCESS);
  } else {
    printf("Parent Process before waiting\n");
    while (1) {
      _pid = waitpid(pid, &status, WNOHANG);
      if (_pid < 0) {
        perror("wait error");
      } else if (0 == _pid) {
        sleep(1);
      } else {
        break;
      }
    }
    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    $0x30,%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    1297 <main+0x127>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11fb <main+0x8b>
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
lea    0xe48(%rip),%rdi
call   1040 <pr_ids@plt>
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe35(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    123d <main+0xcd>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
jmp    1262 <main+0xf2>
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1258 <main+0xe8>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    125d <main+0xed>
jmp    1267 <main+0xf7>
jmp    1262 <main+0xf2>
jmp    1207 <main+0x97>
lea    0xdf3(%rip),%rdi
call   1030 <printf@plt>
lea    0xe05(%rip),%rdi
call   1030 <printf@plt>
lea    0xe09(%rip),%rdi
call   1040 <pr_ids@plt>
jmp    1290 <main+0x120>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1214 <main+0xa4>
je     1227 <main+0xb7>
mov    %rax,%rbx
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e44(%rip),%r12
lea    0x4(%rsp),%r14
lea    0xe61(%rip),%r15
jmp    11c0 <main+0x50>
mov    %r15,%rdi
call   1060 <perror@plt>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <waitpid@plt>
test   %rax,%rax
js     11b2 <main+0x42>
jne    11e2 <main+0x72>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    11c0 <main+0x50>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1b(%rip),%rdi
call   1040 <pr_ids@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xde5(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    1208 <main+0x98>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
lea    0xdd1(%rip),%rdi
call   1040 <pr_ids@plt>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    0x2d7c(%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"
@.str.3 = [23 x i8] c"Child process running\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.4 = [31 x i8] c"Parent Process before waiting\0A\00"
@WNOHANG = global i32 0
@.str.5 = [11 x i8] c"wait error\00"
@.str.6 = [30 x i8] c"Parent Process After waiting\0A\00"
@.str.7 = [16 x i8] c"Parent process\0A\00"
@.str.8 = [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 i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @fork()
store i64 %9, i64* %6
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %47
13: ; preds = %2
%14 = load i64, i64* %6
%15 = icmp eq i64 0, %14
br i1 %15, label %16, label %23
16: ; preds = %13
%17 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @pr_ids(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @sleep(i32 5)
%21 = load i32, i32* @EXIT_SUCCESS
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %13
%24 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23, %40
%26 = load i64, i64* %6
%27 = load i32, i32* @WNOHANG
%28 = call i64 @waitpid(i64 %26, i32* %8, i32 %27)
store i64 %28, i64* %7
%29 = load i64, i64* %7
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %40
33: ; preds = %25
%34 = load i64, i64* %7
%35 = icmp eq i64 0, %34
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @sleep(i32 1)
br label %39
38: ; preds = %33
br label %41
39: ; preds = %36
br label %40
40: ; preds = %39, %31
br label %25
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%43 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
%44 = call i32 @pr_ids(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %45
45: ; preds = %41
br label %46
46: ; preds = %45
store i32 0, i32* %3
br label %47
47: ; preds = %46, %11
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.2 = [6 x i8] c"child\00"
@EXIT_SUCCESS = global i32 0
@WNOHANG = global i32 0
@.str.5 = [11 x i8] c"wait error\00"
@.str.8 = [7 x i8] c"Parent\00"
@str = [30 x i8] c"Parent Process before waiting\00"
@str.9 = [29 x i8] c"Parent Process After waiting\00"
@str.10 = [15 x i8] c"Parent process\00"
@str.11 = [14 x i8] c"Child process\00"
@str.12 = [22 x i8] c"Child process running\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 %35
9: ; preds = %2
%10 = icmp eq i64 %5, 0
br i1 %10, label %11, label %18
11: ; preds = %9
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.11, 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 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.12, i64 0, i64 0))
%15 = tail call i32 @sleep(i32 5)
%16 = load i32, i32* @EXIT_SUCCESS
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %9
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %30, %18
%21 = load i32, i32* @WNOHANG
%22 = call i64 @waitpid(i64 %5, i32* %3, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %30
26: ; preds = %20
%27 = icmp eq i64 %22, 0
br i1 %27, label %28, label %31
28: ; preds = %26
%29 = call i32 @sleep(i32 1)
br label %30
30: ; preds = %28, %24
br label %20
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.9, i64 0, i64 0))
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
%34 = call i32 @pr_ids(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %35
35: ; preds = %31, %7
%36 = phi i32 [ -1, %7 ], [ 0, %31 ]
ret i32 %36
}