#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*/ 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);
    }
  }
  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    11d5 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x30(%rbp)
jmp    11dc <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    1275 <main+0xd5>
mov    -0x2c(%rbp),%esi
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1262 <main+0xc2>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdcd(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1267 <main+0xc7>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ef <main+0x4f>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    131b <main+0x17b>
lea    -0x14(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12ee <main+0x14e>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xd6a(%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    0xd48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    128a <main+0xea>
mov    $0x0,%al
call   1030 <get_seconds@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
lea    0xd3b(%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    11f1 <main+0x41>
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1030 <get_seconds@plt>
test   %ebp,%ebp
movsd  %xmm0,0x8(%rsp)
jg     1203 <main+0x53>
lea    0xe90(%rip),%rdi
call   1040 <puts@plt>
jmp    127f <main+0xcf>
xor    %eax,%eax
call   1030 <get_seconds@plt>
movsd  %xmm0,0x8(%rsp)
mov    $0x1,%ebp
lea    0xdf6(%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   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     12a1 <main+0xf1>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    1210 <main+0x60>
lea    0xe41(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%r15
lea    0xdef(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   10c0 <wait@plt>
cmp    $0xffffffff,%eax
je     12c1 <main+0x111>
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    1250 <main+0xa0>
xor    %eax,%eax
call   1030 <get_seconds@plt>
subsd  0x8(%rsp),%xmm0
lea    0xdc8(%rip),%rdi
mov    $0x1,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%ebx
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd55(%rip),%rsi
jmp    12df <main+0x12f>
mov    0x2d18(%rip),%rax
mov    (%rax),%ebx
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd46(%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 = %43, %20
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %46
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
br label %43
43: ; preds = %42
%44 = load i32, i32* %10
%45 = add nsw i32 %44, 1
store i32 %45, i32* %10
br label %22
46: ; preds = %22
%47 = 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 %48
48: ; preds = %72, %46
%49 = load i32, i32* %10
%50 = load i32, i32* %11
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %75
52: ; preds = %48
%53 = call i32 @wait(i32* %6)
store i32 %53, i32* %7
%54 = load i32, i32* %7
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %66
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %59)
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 0
%63 = load i8*, i8** %62
%64 = call i32 @perror(i8* %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %52
%67 = load i32, i32* %6
%68 = call i32 @WEXITSTATUS(i32 %67)
store i32 %68, i32* %6
%69 = load i32, i32* %7
%70 = load i32, i32* %6
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %69, i32 %70)
br label %72
72: ; preds = %66
%73 = load i32, i32* %10
%74 = add nsw i32 %73, 1
store i32 %74, i32* %10
br label %48
75: ; preds = %48
%76 = call double (...) @get_seconds()
store double %76, double* %9
%77 = load double, double* %9
%78 = load double, double* %8
%79 = fsub double %77, %78
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), double %79)
%81 = 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 %55
16: ; preds = %6, %8
%17 = phi double [ %7, %6 ], [ %12, %8 ]
%18 = phi i32 [ 1, %6 ], [ %11, %8 ]
br label %21
19: ; preds = %21
%20 = icmp eq i32 %26, %18
br i1 %20, label %35, label %21
21: ; preds = %16, %19
%22 = phi i32 [ %26, %19 ], [ 0, %16 ]
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = tail call i32 (...) @fork()
%25 = icmp eq i32 %24, -1
%26 = add nuw nsw i32 %22, 1
br i1 %25, label %27, label %19
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %1
%33 = tail call i32 @perror(i8* %32)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %19
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %37
37: ; preds = %35, %49
%38 = phi i32 [ %53, %49 ], [ 0, %35 ]
%39 = call i32 @wait(i32* %3)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %49
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %1
%47 = call i32 @perror(i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %37
%50 = load i32, i32* %3
%51 = call i32 @WEXITSTATUS(i32 %50)
store i32 %51, i32* %3
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 %51)
%53 = add nuw nsw i32 %38, 1
%54 = icmp eq i32 %53, %18
br i1 %54, label %55, label %37
55: ; preds = %49, %14
%56 = phi double [ %12, %14 ], [ %17, %49 ]
%57 = call double (...) @get_seconds()
%58 = fsub double %57, %56
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), double %58)
%60 = call i32 @exit(i32 0)
unreachable
}