#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;
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  pid_t pid1, pid2;
  if ((pid1 = fork()) < 0)
    printf("fork error");
  else if (pid1 == 0) {
    printf("child, %ld\n", (long)getpid());
    sleep(5);
  } else {
    do {
      pid2 = waitpid(pid1, NULL, WNOHANG);
      if (pid2 == 0) {
        printf("No child exited\n");
        sleep(1);
      }
    } while (pid2 == 0);
    if (pid2 == pid1)
      printf("successfully get child %ld\n", (long)pid2);
    else
      printf("some error occured\n");
  }
  return 0;
}


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)
cmp    $0x0,%rax
jge    1187 <main+0x37>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1248 <main+0xf8>
cmpq   $0x0,-0x10(%rbp)
jne    11b9 <main+0x69>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1060 <sleep@plt>
jmp    1243 <main+0xf3>
jmp    11be <main+0x6e>
mov    -0x10(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1050 <waitpid@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fb <main+0xab>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    1200 <main+0xb0>
cmpq   $0x0,-0x18(%rbp)
je     11be <main+0x6e>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1230 <main+0xe0>
mov    -0x18(%rbp),%rsi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123e <main+0xee>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1243 <main+0xf3>
jmp    1248 <main+0xf8>
xor    %eax,%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>
test   %rax,%rax
js     11dc <main+0x7c>
je     11ec <main+0x8c>
mov    %rax,%rbx
mov    0x2e6b(%rip),%r15
mov    (%r15),%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1060 <waitpid@plt>
test   %rax,%rax
jne    11c4 <main+0x64>
lea    0xeb0(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <waitpid@plt>
test   %rax,%rax
je     11a0 <main+0x40>
cmp    %rbx,%rax
jne    1210 <main+0xb0>
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    121c <main+0xbc>
lea    0xe1d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    121c <main+0xbc>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe11(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
jmp    121c <main+0xbc>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"fork error\00"
@.str.1 = [12 x i8] c"child, %ld\0A\00"
@WNOHANG = global i32 0
@.str.3 = [28 x i8] c"successfully get child %ld\0A\00"
@str = [19 x i8] c"some error occured\00"
@str.5 = [16 x i8] c"No child exited\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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %28
5: ; preds = %0
%6 = icmp eq i64 %1, 0
br i1 %6, label %11, label %7
7: ; preds = %5
%8 = load i32, i32* @WNOHANG
%9 = tail call i64 @waitpid(i64 %1, i32* null, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %21
11: ; preds = %5
%12 = tail call i64 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i64 %12)
%14 = tail call i32 @sleep(i32 5)
br label %28
15: ; preds = %7, %15
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
%17 = tail call i32 @sleep(i32 1)
%18 = load i32, i32* @WNOHANG
%19 = tail call i64 @waitpid(i64 %1, i32* null, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %15, label %21
21: ; preds = %15, %7
%22 = phi i64 [ %9, %7 ], [ %19, %15 ]
%23 = icmp eq i64 %22, %1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %1)
br label %28
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %28
28: ; preds = %11, %26, %24, %3
ret i32 0
}