#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 */

/* Variables and functions */
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int wait(int *);
int main(int argc, char **argv) {
  int i, ret, code;
  if (argc > 1)
    signal(SIGCHLD, SIG_IGN);
  for (i = 1; i < 6; i++)
    if (fork() == 0) {
      sleep(i * 2);
      printf("Child #=%d PID=%d slept for %d seconds\n", i, getpid(), i * 2);
      exit(i);
    }
  sleep(5);
  ret = wait(&code);
  printf("Wait: ret = %d code = %x\n", ret, code);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11a7 <main+0x37>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
movl   $0x1,-0x14(%rbp)
cmpl   $0x6,-0x14(%rbp)
jge    1215 <main+0xa5>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    1202 <main+0x92>
mov    -0x14(%rbp),%edi
shl    $0x1,%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
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <exit@plt>
jmp    1207 <main+0x97>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ae <main+0x3e>
mov    $0x5,%edi
call   1070 <sleep@plt>
lea    -0x1c(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     118f <main+0x1f>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1050 <signal@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11fc <main+0x8c>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1203 <main+0x93>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     120a <main+0x9a>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1211 <main+0xa1>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1218 <main+0xa8>
mov    $0x5,%edi
call   1070 <sleep@plt>
lea    0x4(%rsp),%rdi
call   1080 <wait@plt>
mov    0x4(%rsp),%edx
lea    0xe3e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x1,%ebx
jmp    121d <main+0xad>
mov    $0x2,%ebx
jmp    121d <main+0xad>
mov    $0x3,%ebx
jmp    121d <main+0xad>
mov    $0x4,%ebx
jmp    121d <main+0xad>
mov    $0x5,%ebx
lea    (%rbx,%rbx,1),%ebp
mov    %ebp,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xdcb(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [40 x i8] c"Child #=%d PID=%d slept for %d seconds\0A\00"
@.str.1 = [26 x i8] c"Wait: ret = %d code = %x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @SIGCHLD
%13 = load i32, i32* @SIG_IGN
%14 = call i32 @signal(i32 %12, i32 %13)
br label %15
15: ; preds = %11, %2
store i32 1, i32* %6
br label %16
16: ; preds = %34, %15
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 6
br i1 %18, label %19, label %37
19: ; preds = %16
%20 = call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %19
%23 = load i32, i32* %6
%24 = mul nsw i32 %23, 2
%25 = call i32 @sleep(i32 %24)
%26 = load i32, i32* %6
%27 = call i32 (...) @getpid()
%28 = load i32, i32* %6
%29 = mul nsw i32 %28, 2
%30 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %27, i32 %29)
%31 = load i32, i32* %6
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %19
br label %34
34: ; preds = %33
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %16
37: ; preds = %16
%38 = call i32 @sleep(i32 5)
%39 = call i32 @wait(i32* %8)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = load i32, i32* %8
%42 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %41)
ret i32 0
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str = [40 x i8] c"Child #=%d PID=%d slept for %d seconds\0A\00"
@.str.1 = [26 x i8] c"Wait: ret = %d code = %x\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @SIGCHLD
%8 = load i32, i32* @SIG_IGN
%9 = tail call i32 @signal(i32 %7, i32 %8)
br label %10
10: ; preds = %6, %2
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %29, %26, %23, %20, %10
%14 = phi i32 [ 1, %10 ], [ 2, %20 ], [ 3, %23 ], [ 4, %26 ], [ 5, %29 ]
%15 = shl nuw nsw i32 %14, 1
%16 = tail call i32 @sleep(i32 %15)
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %14, i32 %17, i32 %15)
%19 = tail call i32 @exit(i32 %14)
unreachable
20: ; preds = %10
%21 = tail call i64 (...) @fork()
%22 = icmp eq i64 %21, 0
br i1 %22, label %13, label %23
23: ; preds = %20
%24 = tail call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %13, label %26
26: ; preds = %23
%27 = tail call i64 (...) @fork()
%28 = icmp eq i64 %27, 0
br i1 %28, label %13, label %29
29: ; preds = %26
%30 = tail call i64 (...) @fork()
%31 = icmp eq i64 %30, 0
br i1 %31, label %13, label %32
32: ; preds = %29
%33 = tail call i32 @sleep(i32 5)
%34 = call i32 @wait(i32* %3)
%35 = load i32, i32* %3
%36 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %35)
ret i32 0
}