#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*/ WNOHANG;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
main() {
  pid_t pc, pr;
  pc = fork();
  if (pc < 0)
    printf("Error occured on forking.\n");
  else if (pc == 0) {
    sleep(4);
    exit(0);
  }
  do {
    pr = waitpid(pc, NULL, WNOHANG);
    if (pr == 0) {
      printf("No child exited\n");
      sleep(1);
    }
  } while (pr == 0);
  if (pr == pc)
    printf("successfully release child %d\n", pr);
  else
    printf("some error occured\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1188 <main+0x38>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11a9 <main+0x59>
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <main+0x54>
mov    $0x4,%edi
call   1060 <sleep@plt>
xor    %edi,%edi
call   1050 <exit@plt>
jmp    11a9 <main+0x59>
jmp    11ae <main+0x5e>
mov    -0x10(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <waitpid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11eb <main+0x9b>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11f0 <main+0xa0>
cmpq   $0x0,-0x18(%rbp)
je     11ae <main+0x5e>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1220 <main+0xd0>
mov    -0x18(%rbp),%rsi
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    122e <main+0xde>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1187 <main+0x27>
jne    1193 <main+0x33>
mov    $0x4,%edi
call   1070 <sleep@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e4e(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <waitpid@plt>
test   %rax,%rax
jne    11e4 <main+0x84>
lea    0xe7f(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <waitpid@plt>
test   %rax,%rax
je     11c0 <main+0x60>
cmp    %rbx,%rax
jne    11fc <main+0x9c>
lea    0xe10(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1208 <main+0xa8>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Error occured on forking.\0A\00"
@WNOHANG = global i32 0
@.str.1 = [17 x i8] c"No child exited\0A\00"
@.str.2 = [31 x i8] c"successfully release child %d\0A\00"
@.str.3 = [20 x i8] c"some error occured\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
store i32 0, i32* %1
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %16
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = call i32 @sleep(i32 4)
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %9
br label %16
16: ; preds = %15, %7
br label %17
17: ; preds = %27, %16
%18 = load i64, i64* %2
%19 = load i32, i32* @WNOHANG
%20 = call i64 @waitpid(i64 %18, i32* null, i32 %19)
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @sleep(i32 1)
br label %26
26: ; preds = %23, %17
br label %27
27: ; preds = %26
%28 = load i64, i64* %3
%29 = icmp eq i64 %28, 0
br i1 %29, label %17, label %30
30: ; preds = %27
%31 = load i64, i64* %3
%32 = load i64, i64* %2
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i64, i64* %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %35)
br label %39
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str.2 = [31 x i8] c"successfully release child %d\0A\00"
@str = [19 x i8] c"some error occured\00"
@str.4 = [16 x i8] c"No child exited\00"
@str.5 = [26 x i8] c"Error occured on forking.\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.5, i64 0, i64 0))
br label %10
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %7, label %10
7: ; preds = %5
%8 = tail call i32 @sleep(i32 4)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %5, %3
%11 = load i32, i32* @WNOHANG
%12 = tail call i64 @waitpid(i64 %1, i32* null, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %10, %14
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
%16 = tail call i32 @sleep(i32 1)
%17 = load i32, i32* @WNOHANG
%18 = tail call i64 @waitpid(i64 %1, i32* null, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %14, label %20
20: ; preds = %14, %10
%21 = phi i64 [ %12, %10 ], [ %18, %14 ]
%22 = icmp eq i64 %21, %1
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %1)
br label %27
25: ; preds = %20
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %25, %23
ret i32 0
}