#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) {
      printf("Child, memory location of iter:%p\n", &i);
      num_children += 1;
      child_code(i);
    } else {
      printf("Parent, memory location of iter:%p\n", &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    12c9 <main+0x119>
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    12a4 <main+0xf4>
lea    0xda2(%rip),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   10b0 <child_code@plt>
jmp    12b6 <main+0x106>
lea    0xd9d(%rip),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bb <main+0x10b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ff <main+0x4f>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    136f <main+0x1bf>
lea    -0x14(%rbp),%rdi
call   10c0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1342 <main+0x192>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd5d(%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    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12de <main+0x12e>
mov    $0x0,%al
call   1030 <get_seconds@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
lea    0xd2e(%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   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1213 <main+0x53>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <get_seconds@plt>
movsd  %xmm0,0x10(%rsp)
movl   $0x0,0x8(%rsp)
test   %ebp,%ebp
jg     122d <main+0x6d>
lea    0xebd(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,0x8(%rsp)
jmp    1309 <main+0x149>
xor    %eax,%eax
call   1030 <get_seconds@plt>
movsd  %xmm0,0x10(%rsp)
movl   $0x0,0x8(%rsp)
mov    $0x1,%ebp
lea    0xdcc(%rip),%r12
lea    0xdea(%rip),%r15
lea    0x8(%rsp),%rbx
lea    0xe01(%rip),%r13
xor    %esi,%esi
jmp    1278 <main+0xb8>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    0x8(%rsp),%edi
call   10c0 <child_code@plt>
mov    0x8(%rsp),%esi
add    $0x1,%esi
mov    %esi,0x8(%rsp)
cmp    %ebp,%esi
jge    12a5 <main+0xe5>
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     1250 <main+0x90>
cmp    $0xffffffff,%eax
je     132b <main+0x16b>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1269 <main+0xa9>
lea    0xe12(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,0x8(%rsp)
test   %ebp,%ebp
jle    1309 <main+0x149>
lea    0xc(%rsp),%r15
lea    0xdb4(%rip),%r12
nopl   0x0(%rax)
mov    %r15,%rdi
call   10d0 <wait@plt>
cmp    $0xffffffff,%eax
je     134b <main+0x18b>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,0xc(%rsp)
mov    %r12,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%eax
add    $0x1,%eax
mov    %eax,0x8(%rsp)
cmp    %ebp,%eax
jl     12d0 <main+0x110>
xor    %eax,%eax
call   1030 <get_seconds@plt>
subsd  0x10(%rsp),%xmm0
lea    0xd85(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%ebx
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xccb(%rip),%rsi
jmp    1369 <main+0x1a9>
mov    0x2c8e(%rip),%rax
mov    (%rax),%ebx
mov    0x2c7d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd03(%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 = [35 x i8] c"Child, memory location of iter:%p\0A\00"
@.str.3 = [36 x i8] c"Parent, memory location of iter:%p\0A\00"
@.str.4 = [24 x i8] c"Hello from the parent.\0A\00"
@.str.5 = [17 x i8] c"wait failed: %s\0A\00"
@.str.6 = [37 x i8] c"Child %d exited with error code %d.\0A\00"
@.str.7 = [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 = %54, %20
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %57
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 %51
45: ; preds = %42
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32* %10)
%47 = load i32, i32* %11
%48 = add nsw i32 %47, 1
store i32 %48, i32* %11
%49 = load i32, i32* %10
%50 = call i32 @child_code(i32 %49)
br label %53
51: ; preds = %42
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32* %10)
br label %53
53: ; preds = %51, %45
br label %54
54: ; preds = %53
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %22
57: ; preds = %22
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %10
br label %59
59: ; preds = %83, %57
%60 = load i32, i32* %10
%61 = load i32, i32* %11
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %86
63: ; preds = %59
%64 = call i32 @wait(i32* %6)
store i32 %64, i32* %7
%65 = load i32, i32* %7
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %77
67: ; preds = %63
%68 = load i32, i32* @stderr
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %70)
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 0
%74 = load i8*, i8** %73
%75 = call i32 @perror(i8* %74)
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %63
%78 = load i32, i32* %6
%79 = call i32 @WEXITSTATUS(i32 %78)
store i32 %79, i32* %6
%80 = load i32, i32* %7
%81 = load i32, i32* %6
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %80, i32 %81)
br label %83
83: ; preds = %77
%84 = load i32, i32* %10
%85 = add nsw i32 %84, 1
store i32 %85, i32* %10
br label %59
86: ; preds = %59
%87 = call double (...) @get_seconds()
store double %87, double* %9
%88 = load double, double* %9
%89 = load double, double* %8
%90 = fsub double %88, %89
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), double %90)
%92 = 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.2 = [35 x i8] c"Child, memory location of iter:%p\0A\00"
@.str.3 = [36 x i8] c"Parent, memory location of iter:%p\0A\00"
@.str.5 = [17 x i8] c"wait failed: %s\0A\00"
@.str.6 = [37 x i8] c"Child %d exited with error code %d.\0A\00"
@.str.7 = [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 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call double (...) @get_seconds()
store i32 0, i32* %4
br label %18
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = tail call double (...) @get_seconds()
store i32 0, i32* %4
%15 = icmp sgt i32 %13, 0
br i1 %15, label %18, label %16
16: ; preds = %10
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
store i32 0, i32* %4
br label %67
18: ; preds = %8, %10
%19 = phi double [ %9, %8 ], [ %14, %10 ]
%20 = phi i32 [ 1, %8 ], [ %13, %10 ]
br label %21
21: ; preds = %18, %41
%22 = phi i32 [ %42, %41 ], [ %20, %18 ]
%23 = phi i32 [ %44, %41 ], [ 0, %18 ]
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = call i32 (...) @fork()
switch i32 %25, label %39 [
i32 -1, label %26
i32 0, label %34
]
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %1
%32 = call i32 @perror(i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%36 = add nsw i32 %22, 1
%37 = load i32, i32* %4
%38 = call i32 @child_code(i32 %37)
br label %41
39: ; preds = %21
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32* %4)
br label %41
41: ; preds = %34, %39
%42 = phi i32 [ %36, %34 ], [ %22, %39 ]
%43 = load i32, i32* %4
%44 = add nsw i32 %43, 1
store i32 %44, i32* %4
%45 = icmp slt i32 %44, %42
br i1 %45, label %21, label %46
46: ; preds = %41
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
store i32 0, i32* %4
%48 = icmp sgt i32 %42, 0
br i1 %48, label %49, label %67
49: ; preds = %46, %60
%50 = call i32 @wait(i32* %3)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %60
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %1
%58 = call i32 @perror(i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %49
%61 = load i32, i32* %3
%62 = call i32 @WEXITSTATUS(i32 %61)
store i32 %62, i32* %3
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %50, i32 %62)
%64 = load i32, i32* %4
%65 = add nsw i32 %64, 1
store i32 %65, i32* %4
%66 = icmp slt i32 %65, %42
br i1 %66, label %49, label %67
67: ; preds = %60, %16, %46
%68 = phi double [ %14, %16 ], [ %19, %46 ], [ %19, %60 ]
%69 = call double (...) @get_seconds()
%70 = fsub double %69, %68
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0), double %70)
%72 = call i32 @exit(i32 0)
unreachable
}