#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);
    }
  }
  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    1297 <main+0xe7>
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    1284 <main+0xd4>
mov    -0x2c(%rbp),%edi
call   10b0 <child_code@plt>
jmp    1289 <main+0xd9>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ff <main+0x4f>
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    133d <main+0x18d>
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1310 <main+0x160>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd48(%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    0xd26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12ac <main+0xfc>
mov    $0x0,%al
call   1030 <get_seconds@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
lea    0xd19(%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    12bf <main+0xff>
xor    %eax,%eax
call   1030 <get_seconds@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%ebp
lea    0xde6(%rip),%r15
xor    %ebx,%ebx
jmp    122e <main+0x6e>
xchg   %ax,%ax
mov    %ebx,%edi
call   10c0 <child_code@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
je     126d <main+0xad>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1220 <main+0x60>
cmp    $0xffffffff,%eax
jne    1227 <main+0x67>
mov    0x2d8f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdac(%rip),%rsi
jmp    12ff <main+0x13f>
lea    0xe03(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%r15
lea    0xdb1(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   10d0 <wait@plt>
cmp    $0xffffffff,%eax
je     12e1 <main+0x121>
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    1290 <main+0xd0>
xor    %eax,%eax
call   1030 <get_seconds@plt>
subsd  0x8(%rsp),%xmm0
lea    0xd88(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd26(%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 = %49, %20
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %52
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 %48
45: ; preds = %42
%46 = load i32, i32* %10
%47 = call i32 @child_code(i32 %46)
br label %48
48: ; preds = %45, %42
br label %49
49: ; preds = %48
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %22
52: ; preds = %22
%53 = 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 %54
54: ; preds = %78, %52
%55 = load i32, i32* %10
%56 = load i32, i32* %11
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %81
58: ; preds = %54
%59 = call i32 @wait(i32* %6)
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %72
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %65)
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 0
%69 = load i8*, i8** %68
%70 = call i32 @perror(i8* %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %58
%73 = load i32, i32* %6
%74 = call i32 @WEXITSTATUS(i32 %73)
store i32 %74, i32* %6
%75 = load i32, i32* %7
%76 = load i32, i32* %6
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %75, i32 %76)
br label %78
78: ; preds = %72
%79 = load i32, i32* %10
%80 = add nsw i32 %79, 1
store i32 %80, i32* %10
br label %54
81: ; preds = %54
%82 = call double (...) @get_seconds()
store double %82, double* %9
%83 = load double, double* %9
%84 = load double, double* %8
%85 = fsub double %83, %84
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), double %85)
%87 = 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 %56
16: ; preds = %6, %8
%17 = phi double [ %7, %6 ], [ %12, %8 ]
%18 = phi i32 [ 1, %6 ], [ %11, %8 ]
br label %19
19: ; preds = %16, %33
%20 = phi i32 [ %34, %33 ], [ 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 %33 [
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)
br label %33
33: ; preds = %19, %31
%34 = add nuw nsw i32 %20, 1
%35 = icmp eq i32 %34, %18
br i1 %35, label %36, label %19
36: ; preds = %33
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %36, %50
%39 = phi i32 [ %54, %50 ], [ 0, %36 ]
%40 = call i32 @wait(i32* %3)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %50
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %1
%48 = call i32 @perror(i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %38
%51 = load i32, i32* %3
%52 = call i32 @WEXITSTATUS(i32 %51)
store i32 %52, i32* %3
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %40, i32 %52)
%54 = add nuw nsw i32 %39, 1
%55 = icmp eq i32 %54, %18
br i1 %55, label %56, label %38
56: ; preds = %50, %14
%57 = phi double [ %12, %14 ], [ %17, %50 ]
%58 = call double (...) @get_seconds()
%59 = fsub double %58, %57
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), double %59)
%61 = call i32 @exit(i32 0)
unreachable
}