#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*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int sleep(int);
int main(void) {
  pid_t pid;
  int i, n, status;
  signal(SIGCHLD, SIG_IGN);
  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);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jg     1210 <main+0xb0>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fd <main+0x9d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
imul   $0x7,-0x14(%rbp),%edi
call   1070 <sleep@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    1202 <main+0xa2>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118d <main+0x2d>
movl   $0x1,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jg     1271 <main+0x111>
mov    -0x14(%rbp),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x14,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <sleep@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     1252 <main+0xf2>
jmp    1239 <main+0xd9>
mov    -0x14(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1217 <main+0xb7>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     11ab <main+0x4b>
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0x2,%ebx
test   %rax,%rax
je     11ab <main+0x4b>
xor    %eax,%eax
call   1080 <fork@plt>
mov    $0x3,%ebx
test   %rax,%rax
jne    11f2 <main+0x92>
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   1070 <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   1060 <exit@plt>
lea    0xe4f(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x14,%eax
nopw   0x0(%rax,%rax,1)
mov    %eax,%edi
call   1070 <sleep@plt>
test   %eax,%eax
jne    1210 <main+0xb0>
lea    0xe4e(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe13(%rip),%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x14,%eax
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%edi
call   1070 <sleep@plt>
test   %eax,%eax
jne    1250 <main+0xf0>
lea    0xe0e(%rip),%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdd3(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x14,%eax
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%edi
call   1070 <sleep@plt>
test   %eax,%eax
jne    1290 <main+0x130>
lea    0xdce(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xd93(%rip),%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x14,%eax
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%edi
call   1070 <sleep@plt>
test   %eax,%eax
jne    12d0 <main+0x170>
lea    0xd8e(%rip),%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.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"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @SIGCHLD
%7 = load i32, i32* @SIG_IGN
%8 = call i32 @signal(i32 %6, i32 %7)
store i32 1, i32* %3
br label %9
9: ; preds = %28, %0
%10 = load i32, i32* %3
%11 = icmp sle i32 %10, 3
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = call i64 (...) @fork()
store i64 %13, i64* %2
%14 = load i64, i64* %2
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load i32, i32* %3
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i32, i32* %3
%21 = mul nsw i32 %20, 7
%22 = call i32 @sleep(i32 %21)
%23 = load i32, i32* %3
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %23, i32 %24)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %12
br label %28
28: ; preds = %27
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %9
31: ; preds = %9
store i32 1, i32* %3
br label %32
32: ; preds = %46, %31
%33 = load i32, i32* %3
%34 = icmp sle i32 %33, 4
br i1 %34, label %35, label %49
35: ; preds = %32
%36 = load i32, i32* %3
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %36)
store i32 20, i32* %4
br label %38
38: ; preds = %42, %35
%39 = load i32, i32* %4
%40 = call i32 @sleep(i32 %39)
store i32 %40, i32* %4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38
br label %38
43: ; preds = %38
%44 = load i32, i32* %3
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %32
49: ; preds = %32
%50 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.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"
define i32 @main() {
%1 = load i32, i32* @SIGCHLD
%2 = load i32, i32* @SIG_IGN
%3 = tail call i32 @signal(i32 %1, i32 %2)
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %15
6: ; preds = %18, %15, %0
%7 = phi i32 [ 1, %0 ], [ 2, %15 ], [ 3, %18 ]
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = mul nuw nsw i32 %7, 7
%11 = tail call i32 @sleep(i32 %10)
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %12)
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %0
%16 = tail call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %6, label %18
18: ; preds = %15
%19 = tail call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %6, label %21
21: ; preds = %18
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 1)
br label %23
23: ; preds = %23, %21
%24 = phi i32 [ 20, %21 ], [ %25, %23 ]
%25 = tail call i32 @sleep(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %23
27: ; preds = %23
%28 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 1)
%29 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 2)
br label %30
30: ; preds = %30, %27
%31 = phi i32 [ 20, %27 ], [ %32, %30 ]
%32 = tail call i32 @sleep(i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %30
34: ; preds = %30
%35 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 2)
%36 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 3)
br label %37
37: ; preds = %37, %34
%38 = phi i32 [ 20, %34 ], [ %39, %37 ]
%39 = tail call i32 @sleep(i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %37
41: ; preds = %37
%42 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 3)
%43 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 4)
br label %44
44: ; preds = %44, %41
%45 = phi i32 [ 20, %41 ], [ %46, %44 ]
%46 = tail call i32 @sleep(i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %44
48: ; preds = %44
%49 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 4)
%50 = tail call i32 @exit(i32 0)
unreachable
}