#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 WEXITSTATUS(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int sleep(int);
int waitpid(int, int *, int /*<<< orphan*/);
int main(void) {
  pid_t pid;
  int i, n, status;
  for (i = 1; i <= 3; i++) {
    pid = fork();
    if (pid == 0) {
      printf("CHILD no. %d (PID=%d) working ... \n", i, getpid());
      sleep(i * 7);
      printf("CHILD no. %d (PID=%d) exiting ... \n", i, getpid());
      exit(0);
    }
  }
  for (i = 1; i <= 4; i++) {
    printf("PARENT: working hard (task no. %d) ...\n", i);
    n = 20;
    while ((n = sleep(n)) != 0)
      ;
    printf("PARENT: end of task no. %d\n", i);
    printf("PARENT: waiting for child no. %d ...\n", i);
    while ((pid = waitpid(-1, &status, WNOHANG))) {
      if (pid > 0) {
        printf("PARENT: child with PID=%d terminated with exit code %d\n", pid,
               WEXITSTATUS(status));
      } else {
        break;
      }
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jg     1207 <main+0x97>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11f4 <main+0x84>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
imul   $0x7,-0xc(%rbp),%edi
call   1080 <sleep@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11f9 <main+0x89>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1186 <main+0x16>
movl   $0x1,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jg     12db <main+0x16b>
mov    -0xc(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x14,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1080 <sleep@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     1249 <main+0xd9>
jmp    1230 <main+0xc0>
mov    -0xc(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     12c8 <main+0x158>
cmpl   $0x0,-0x8(%rbp)
jle    12be <main+0x14e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c3 <main+0x153>
jmp    12c8 <main+0x158>
jmp    126b <main+0xfb>
jmp    12cd <main+0x15d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    120e <main+0x9e>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0x1,%ebx
test   %eax,%eax
je     11ab <main+0x3b>
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0x2,%ebx
test   %eax,%eax
je     11ab <main+0x3b>
xor    %eax,%eax
call   1090 <fork@plt>
mov    $0x3,%ebx
test   %eax,%eax
jne    11f2 <main+0x82>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe47(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x0(,%rbx,8),%edi
sub    %ebx,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe44(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    $0x1,%r13d
lea    0xe8d(%rip),%r12
mov    0x2de2(%rip),%r15
lea    0x4(%rsp),%rbx
lea    0xea0(%rip),%rbp
jmp    122e <main+0xbe>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r13d
cmp    $0x5,%r13d
je     12c4 <main+0x154>
lea    0xe13(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x14,%eax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%edi
call   1080 <sleep@plt>
test   %eax,%eax
jne    1250 <main+0xe0>
lea    0xe0e(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1060 <waitpid@plt>
test   %eax,%eax
jle    1220 <main+0xb0>
mov    %eax,%r14d
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1060 <waitpid@plt>
mov    %eax,%r14d
test   %eax,%eax
jg     1290 <main+0x120>
jmp    1220 <main+0xb0>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"CHILD no. %d (PID=%d) working ... \0A\00"
@.str.1 = [36 x i8] c"CHILD no. %d (PID=%d) exiting ... \0A\00"
@.str.2 = [40 x i8] c"PARENT: working hard (task no. %d) ...\0A\00"
@.str.3 = [28 x i8] c"PARENT: end of task no. %d\0A\00"
@.str.4 = [38 x i8] c"PARENT: waiting for child no. %d ...\0A\00"
@WNOHANG = global i32 0
@.str.5 = [56 x i8] c"PARENT: child with PID=%d terminated with exit code %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %3
br label %6
6: ; preds = %25, %0
%7 = load i32, i32* %3
%8 = icmp sle i32 %7, 3
br i1 %8, label %9, label %28
9: ; preds = %6
%10 = call i32 (...) @fork()
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %9
%14 = load i32, i32* %3
%15 = call i32 (...) @getpid()
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* %3
%18 = mul nsw i32 %17, 7
%19 = call i32 @sleep(i32 %18)
%20 = load i32, i32* %3
%21 = call i32 (...) @getpid()
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %9
br label %25
25: ; preds = %24
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
br label %6
28: ; preds = %6
store i32 1, i32* %3
br label %29
29: ; preds = %60, %28
%30 = load i32, i32* %3
%31 = icmp sle i32 %30, 4
br i1 %31, label %32, label %63
32: ; preds = %29
%33 = load i32, i32* %3
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 20, i32* %4
br label %35
35: ; preds = %39, %32
%36 = load i32, i32* %4
%37 = call i32 @sleep(i32 %36)
store i32 %37, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
br label %35
40: ; preds = %35
%41 = load i32, i32* %3
%42 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %3
%44 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %58, %40
%46 = load i32, i32* @WNOHANG
%47 = call i32 @waitpid(i32 -1, i32* %5, i32 %46)
store i32 %47, i32* %2
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %45
%50 = load i32, i32* %2
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = load i32, i32* %2
%54 = load i32, i32* %5
%55 = call i32 @WEXITSTATUS(i32 %54)
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i32 %53, i32 %55)
br label %58
57: ; preds = %49
br label %59
58: ; preds = %52
br label %45
59: ; preds = %57, %45
br label %60
60: ; preds = %59
%61 = load i32, i32* %3
%62 = add nsw i32 %61, 1
store i32 %62, i32* %3
br label %29
63: ; preds = %29
%64 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [36 x i8] c"CHILD no. %d (PID=%d) working ... \0A\00"
@.str.1 = [36 x i8] c"CHILD no. %d (PID=%d) exiting ... \0A\00"
@.str.2 = [40 x i8] c"PARENT: working hard (task no. %d) ...\0A\00"
@.str.3 = [28 x i8] c"PARENT: end of task no. %d\0A\00"
@.str.4 = [38 x i8] c"PARENT: waiting for child no. %d ...\0A\00"
@WNOHANG = global i32 0
@.str.5 = [56 x i8] c"PARENT: child with PID=%d terminated with exit code %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %17, %14, %0
%6 = phi i32 [ 1, %0 ], [ 2, %14 ], [ 3, %17 ]
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = mul nuw nsw i32 %6, 7
%10 = tail call i32 @sleep(i32 %9)
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %11)
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = tail call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %5, label %17
17: ; preds = %14
%18 = tail call i32 (...) @fork()
%19 = icmp eq i32 %18, 0
br i1 %19, label %5, label %20
20: ; preds = %17, %41
%21 = phi i32 [ %42, %41 ], [ 1, %17 ]
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %23, %20
%24 = phi i32 [ 20, %20 ], [ %25, %23 ]
%25 = call i32 @sleep(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %23
27: ; preds = %23
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%30 = load i32, i32* @WNOHANG
%31 = call i32 @waitpid(i32 -1, i32* %1, i32 %30)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %27, %33
%34 = phi i32 [ %39, %33 ], [ %31, %27 ]
%35 = load i32, i32* %1
%36 = call i32 @WEXITSTATUS(i32 %35)
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.5, i64 0, i64 0), i32 %34, i32 %36)
%38 = load i32, i32* @WNOHANG
%39 = call i32 @waitpid(i32 -1, i32* %1, i32 %38)
%40 = icmp sgt i32 %39, 0
br i1 %40, label %33, label %41
41: ; preds = %33, %27
%42 = add nuw nsw i32 %21, 1
%43 = icmp eq i32 %42, 5
br i1 %43, label %44, label %20
44: ; preds = %41
%45 = call i32 @exit(i32 0)
unreachable
}