#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 atoi(char *);
int /*<<< orphan*/ child_code(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
double get_seconds();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int wait(int *);
int main(int argc, char *argv[]) {
  int status;
  pid_t pid;
  double start, stop;
  int i, num_children;
  if (argc == 2) {
    num_children = atoi(argv[1]);
  } else {
    num_children = 1;
  }
  start = get_seconds();
  for (i = 0; i < num_children; i++) {
    printf("Creating child %d.\n", i);
    pid = fork();
    if (pid == -1) {
      fprintf(stderr, "fork failed: %s\n", strerror(errno));
      perror(argv[0]);
      exit(1);
    }
    if (pid == 0) {
      child_code(i);
      exit(i);
    }
  }
  printf("Hello from the parent.\n");
  for (i = 0; i < num_children; i++) {
    pid = wait(&status);
    if (pid == -1) {
      fprintf(stderr, "wait failed: %s\n", strerror(errno));
      perror(argv[0]);
      exit(1);
    }
    status = WEXITSTATUS(status);
    printf("Child %d exited with error code %d.\n", pid, status);
  }
  stop = get_seconds();
  printf("Elapsed time = %f seconds.\n", stop - start);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11e5 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x30(%rbp)
jmp    11ec <main+0x3c>
movl   $0x1,-0x30(%rbp)
mov    $0x0,%al
call   1030 <get_seconds@plt>
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    129f <main+0xef>
mov    -0x2c(%rbp),%esi
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1272 <main+0xc2>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdbd(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    128c <main+0xdc>
mov    -0x2c(%rbp),%edi
call   10b0 <child_code@plt>
mov    -0x2c(%rbp),%edi
call   1090 <exit@plt>
jmp    1291 <main+0xe1>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ff <main+0x4f>
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1345 <main+0x195>
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1318 <main+0x168>
mov    0x2d03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd40(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xd1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12b4 <main+0x104>
mov    $0x0,%al
call   1030 <get_seconds@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
lea    0xd11(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1201 <main+0x41>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <get_seconds@plt>
movsd  %xmm0,0x8(%rsp)
test   %ebp,%ebp
jg     1213 <main+0x53>
lea    0xe80(%rip),%rdi
call   1040 <puts@plt>
jmp    128f <main+0xcf>
xor    %eax,%eax
call   1030 <get_seconds@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%ebp
lea    0xde6(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12b1 <main+0xf1>
cmp    $0xffffffff,%eax
je     12bf <main+0xff>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1220 <main+0x60>
lea    0xe29(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%r15
lea    0xdd7(%rip),%r12
nop
mov    %r15,%rdi
call   10d0 <wait@plt>
cmp    $0xffffffff,%eax
je     12df <main+0x11f>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,0x4(%rsp)
mov    %r12,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
jne    1260 <main+0xa0>
xor    %eax,%eax
call   1030 <get_seconds@plt>
subsd  0x8(%rsp),%xmm0
lea    0xdb8(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    %ebx,%edi
call   10c0 <child_code@plt>
mov    %ebx,%edi
call   10a0 <exit@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%ebx
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd37(%rip),%rsi
jmp    12fd <main+0x13d>
mov    0x2cfa(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd28(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    (%r14),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Creating child %d.\0A\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"fork failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"Hello from the parent.\0A\00"
@.str.3 = [17 x i8] c"wait failed: %s\0A\00"
@.str.4 = [37 x i8] c"Child %d exited with error code %d.\0A\00"
@.str.5 = [28 x i8] c"Elapsed time = %f seconds.\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 double
%9 = alloca double
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %11
br label %20
19: ; preds = %2
store i32 1, i32* %11
br label %20
20: ; preds = %19, %14
%21 = call double (...) @get_seconds()
store double %21, double* %8
store i32 0, i32* %10
br label %22
22: ; preds = %51, %20
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %54
26: ; preds = %22
%27 = load i32, i32* %10
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %27)
%29 = call i32 (...) @fork()
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
%40 = call i32 @perror(i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %26
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i32, i32* %10
%47 = call i32 @child_code(i32 %46)
%48 = load i32, i32* %10
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %42
br label %51
51: ; preds = %50
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1
store i32 %53, i32* %10
br label %22
54: ; preds = %22
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %10
br label %56
56: ; preds = %80, %54
%57 = load i32, i32* %10
%58 = load i32, i32* %11
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %83
60: ; preds = %56
%61 = call i32 @wait(i32* %6)
store i32 %61, i32* %7
%62 = load i32, i32* %7
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %74
64: ; preds = %60
%65 = load i32, i32* @stderr
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %67)
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 0
%71 = load i8*, i8** %70
%72 = call i32 @perror(i8* %71)
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %60
%75 = load i32, i32* %6
%76 = call i32 @WEXITSTATUS(i32 %75)
store i32 %76, i32* %6
%77 = load i32, i32* %7
%78 = load i32, i32* %6
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %77, i32 %78)
br label %80
80: ; preds = %74
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 1
store i32 %82, i32* %10
br label %56
83: ; preds = %56
%84 = call double (...) @get_seconds()
store double %84, double* %9
%85 = load double, double* %9
%86 = load double, double* %8
%87 = fsub double %85, %86
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), double %87)
%89 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [20 x i8] c"Creating child %d.\0A\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"fork failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [17 x i8] c"wait failed: %s\0A\00"
@.str.4 = [37 x i8] c"Child %d exited with error code %d.\0A\00"
@.str.5 = [28 x i8] c"Elapsed time = %f seconds.\0A\00"
@str = [23 x i8] c"Hello from the parent.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call double (...) @get_seconds()
br label %16
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = tail call double (...) @get_seconds()
%13 = icmp sgt i32 %11, 0
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %57
16: ; preds = %6, %8
%17 = phi double [ %7, %6 ], [ %12, %8 ]
%18 = phi i32 [ 1, %6 ], [ %11, %8 ]
br label %19
19: ; preds = %16, %34
%20 = phi i32 [ %35, %34 ], [ 0, %16 ]
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = tail call i32 (...) @fork()
switch i32 %22, label %34 [
i32 -1, label %23
i32 0, label %31
]
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %1
%29 = tail call i32 @perror(i8* %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %19
%32 = tail call i32 @child_code(i32 %20)
%33 = tail call i32 @exit(i32 %20)
unreachable
34: ; preds = %19
%35 = add nuw nsw i32 %20, 1
%36 = icmp eq i32 %35, %18
br i1 %36, label %37, label %19
37: ; preds = %34
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %39
39: ; preds = %37, %51
%40 = phi i32 [ %55, %51 ], [ 0, %37 ]
%41 = call i32 @wait(i32* %3)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i8*, i8** %1
%49 = call i32 @perror(i8* %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %39
%52 = load i32, i32* %3
%53 = call i32 @WEXITSTATUS(i32 %52)
store i32 %53, i32* %3
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %41, i32 %53)
%55 = add nuw nsw i32 %40, 1
%56 = icmp eq i32 %55, %18
br i1 %56, label %57, label %39
57: ; preds = %51, %14
%58 = phi double [ %12, %14 ], [ %17, %51 ]
%59 = call double (...) @get_seconds()
%60 = fsub double %59, %58
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), double %60)
%62 = call i32 @exit(i32 0)
unreachable
}