#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 *);
int /*<<< orphan*/ free(char *);
double get_seconds();
char *malloc(int);
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");
  char *x = malloc(sizeof(char));
  static char y = 'a';
  printf("Parent put x in the heap at %p and y in static at %p.\n", x, &y);
  free(x);
  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    1205 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x30(%rbp)
jmp    120c <main+0x3c>
movl   $0x1,-0x30(%rbp)
mov    $0x0,%al
call   1040 <get_seconds@plt>
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    12b7 <main+0xe7>
mov    -0x2c(%rbp),%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1292 <main+0xc2>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xd9d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jne    12a4 <main+0xd4>
mov    -0x2c(%rbp),%edi
call   10d0 <child_code@plt>
jmp    12a9 <main+0xd9>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    121f <main+0x4f>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xd5f(%rip),%rdi
lea    0x2da3(%rip),%rdx
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    138d <main+0x1bd>
lea    -0x14(%rbp),%rdi
call   10e0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1360 <main+0x190>
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd2f(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12fc <main+0x12c>
mov    $0x0,%al
call   1040 <get_seconds@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
lea    0xd00(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <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    1215 <main+0x35>
mov    0x8(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebp
xor    %r15d,%r15d
xor    %eax,%eax
call   1040 <get_seconds@plt>
movsd  %xmm0,0x8(%rsp)
test   %ebp,%ebp
jg     1227 <main+0x47>
jmp    1290 <main+0xb0>
xor    %eax,%eax
call   1040 <get_seconds@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%ebp
lea    0xdd2(%rip),%r15
xor    %ebx,%ebx
jmp    124e <main+0x6e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
call   10e0 <child_code@plt>
add    $0x1,%ebx
cmp    %ebx,%ebp
je     128d <main+0xad>
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     1240 <main+0x60>
cmp    $0xffffffff,%eax
jne    1247 <main+0x67>
mov    0x2d6f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd8c(%rip),%rsi
jmp    134f <main+0x16f>
mov    $0x1,%r15b
lea    0xe17(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
lea    0xd75(%rip),%rdi
lea    0x2dd9(%rip),%rdx
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
test   %r15b,%r15b
je     130f <main+0x12f>
lea    0x4(%rsp),%r15
lea    0xd93(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10f0 <wait@plt>
cmp    $0xffffffff,%eax
je     1331 <main+0x151>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,0x4(%rsp)
mov    %r12,%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
add    $0xffffffff,%ebp
jne    12e0 <main+0x100>
xor    %eax,%eax
call   1040 <get_seconds@plt>
subsd  0x8(%rsp),%xmm0
lea    0xd6f(%rip),%rdi
mov    $0x1,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%ebx
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd0d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    (%r14),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <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"
@main.y = internal global i8 97
@.str.3 = [55 x i8] c"Parent put x in the heap at %p and y in static at %p.\0A\00"
@.str.4 = [17 x i8] c"wait failed: %s\0A\00"
@.str.5 = [37 x i8] c"Child %d exited with error code %d.\0A\00"
@.str.6 = [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
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %11
br label %21
20: ; preds = %2
store i32 1, i32* %11
br label %21
21: ; preds = %20, %15
%22 = call double (...) @get_seconds()
store double %22, double* %8
store i32 0, i32* %10
br label %23
23: ; preds = %50, %21
%24 = load i32, i32* %10
%25 = load i32, i32* %11
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %53
27: ; preds = %23
%28 = load i32, i32* %10
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = call i32 (...) @fork()
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %43
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 0
%40 = load i8*, i8** %39
%41 = call i32 @perror(i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %27
%44 = load i32, i32* %7
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* %10
%48 = call i32 @child_code(i32 %47)
br label %49
49: ; preds = %46, %43
br label %50
50: ; preds = %49
%51 = load i32, i32* %10
%52 = add nsw i32 %51, 1
store i32 %52, i32* %10
br label %23
53: ; preds = %23
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%55 = call i8* @malloc(i32 1)
store i8* %55, i8** %12
%56 = load i8*, i8** %12
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i8* %56, i8* @main.y)
%58 = load i8*, i8** %12
%59 = call i32 @free(i8* %58)
store i32 0, i32* %10
br label %60
60: ; preds = %84, %53
%61 = load i32, i32* %10
%62 = load i32, i32* %11
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %87
64: ; preds = %60
%65 = call i32 @wait(i32* %6)
store i32 %65, i32* %7
%66 = load i32, i32* %7
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %78
68: ; preds = %64
%69 = load i32, i32* @stderr
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %71)
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 0
%75 = load i8*, i8** %74
%76 = call i32 @perror(i8* %75)
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %64
%79 = load i32, i32* %6
%80 = call i32 @WEXITSTATUS(i32 %79)
store i32 %80, i32* %6
%81 = load i32, i32* %7
%82 = load i32, i32* %6
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %81, i32 %82)
br label %84
84: ; preds = %78
%85 = load i32, i32* %10
%86 = add nsw i32 %85, 1
store i32 %86, i32* %10
br label %60
87: ; preds = %60
%88 = call double (...) @get_seconds()
store double %88, double* %9
%89 = load double, double* %9
%90 = load double, double* %8
%91 = fsub double %89, %90
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), double %91)
%93 = 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
@main.y = internal global i8 97
@.str.3 = [55 x i8] c"Parent put x in the heap at %p and y in static at %p.\0A\00"
@.str.4 = [17 x i8] c"wait failed: %s\0A\00"
@.str.5 = [37 x i8] c"Child %d exited with error code %d.\0A\00"
@.str.6 = [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 %14
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 %14, label %34
14: ; preds = %6, %8
%15 = phi double [ %7, %6 ], [ %12, %8 ]
%16 = phi i32 [ 1, %6 ], [ %11, %8 ]
br label %17
17: ; preds = %14, %31
%18 = phi i32 [ %32, %31 ], [ 0, %14 ]
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = tail call i32 (...) @fork()
switch i32 %20, label %31 [
i32 -1, label %21
i32 0, label %29
]
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %1
%27 = tail call i32 @perror(i8* %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = tail call i32 @child_code(i32 %18)
br label %31
31: ; preds = %17, %29
%32 = add nuw nsw i32 %18, 1
%33 = icmp eq i32 %32, %16
br i1 %33, label %34, label %17
34: ; preds = %31, %8
%35 = phi i1 [ false, %8 ], [ true, %31 ]
%36 = phi double [ %12, %8 ], [ %15, %31 ]
%37 = phi i32 [ %11, %8 ], [ %16, %31 ]
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%39 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0), i8* %39, i8* @main.y)
%41 = tail call i32 @free(i8* %39)
br i1 %35, label %42, label %60
42: ; preds = %34, %54
%43 = phi i32 [ %58, %54 ], [ 0, %34 ]
%44 = call i32 @wait(i32* %3)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %54
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %49)
%51 = load i8*, i8** %1
%52 = call i32 @perror(i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %42
%55 = load i32, i32* %3
%56 = call i32 @WEXITSTATUS(i32 %55)
store i32 %56, i32* %3
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %44, i32 %56)
%58 = add nuw nsw i32 %43, 1
%59 = icmp eq i32 %58, %37
br i1 %59, label %60, label %42
60: ; preds = %54, %34
%61 = call double (...) @get_seconds()
%62 = fsub double %61, %36
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), double %62)
%64 = call i32 @exit(i32 0)
unreachable
}