#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 scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
int /*<<< orphan*/ EXIT_SUCCESS;
scalar_t__ clock();
int /*<<< orphan*/ do_little();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ handle_fatal_error(char *);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(void) {
  int pid;
  int status = 0;
  int i;
  clock_t begin_time;
  clock_t end_time;
  double duration;
  begin_time = clock();
  for (i = 0; i < 50000; i++) {
    if ((pid = fork()) < 0) {
      handle_fatal_error("Error when trying to fork\n");
    } else if (pid == 0) {
      do_little();
    } else {
      waitpid(pid, &status, 0);
    }
  }
  end_time = clock();
  duration = (double)(end_time - begin_time) / CLOCKS_PER_SEC;
  printf("%2.1f seconds\n", duration);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0xc350,-0x10(%rbp)
jge    1205 <main+0x95>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c9 <main+0x59>
lea    0xe41(%rip),%rdi
call   1050 <handle_fatal_error@plt>
jmp    11f2 <main+0x82>
cmpl   $0x0,-0x8(%rbp)
jne    11df <main+0x6f>
mov    $0x0,%al
call   1060 <do_little@plt>
jmp    11ed <main+0x7d>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    11f2 <main+0x82>
jmp    11f7 <main+0x87>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1198 <main+0x28>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2dc4(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
lea    0xde2(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r14
mov    $0xc350,%ebp
lea    0xe6b(%rip),%r15
lea    0x4(%rsp),%rbx
jmp    11ad <main+0x3d>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1050 <handle_fatal_error@plt>
add    $0xffffffff,%ebp
je     11d9 <main+0x69>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11a0 <main+0x30>
je     11d0 <main+0x60>
mov    %eax,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    11a8 <main+0x38>
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <do_little@plt>
jmp    11a8 <main+0x38>
xor    %eax,%eax
call   1030 <clock@plt>
sub    %r14,%rax
cvtsi2sd %rax,%xmm0
mov    0x2df9(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"Error when trying to fork\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.1 = [15 x i8] c"%2.1f seconds\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca double
store i32 0, i32* %1
store i32 0, i32* %3
%8 = call i64 (...) @clock()
store i64 %8, i64* %5
store i32 0, i32* %4
br label %9
9: ; preds = %27, %0
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 50000
br i1 %11, label %12, label %30
12: ; preds = %9
%13 = call i32 (...) @fork()
store i32 %13, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @handle_fatal_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %26
17: ; preds = %12
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (...) @do_little()
br label %25
22: ; preds = %17
%23 = load i32, i32* %2
%24 = call i32 @waitpid(i32 %23, i32* %3, i32 0)
br label %25
25: ; preds = %22, %20
br label %26
26: ; preds = %25, %15
br label %27
27: ; preds = %26
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %9
30: ; preds = %9
%31 = call i64 (...) @clock()
store i64 %31, i64* %6
%32 = load i64, i64* %6
%33 = load i64, i64* %5
%34 = sub nsw i64 %32, %33
%35 = sitofp i64 %34 to double
%36 = load double, double* @CLOCKS_PER_SEC
%37 = fdiv double %35, %36
store double %37, double* %7
%38 = load double, double* %7
%39 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), double %38)
%40 = load i32, i32* @EXIT_SUCCESS
%41 = call i32 @exit(i32 %40)
unreachable
}

LLVM IR (-O2):
@.str = [27 x i8] c"Error when trying to fork\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
@.str.1 = [15 x i8] c"%2.1f seconds\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = tail call i64 (...) @clock()
br label %4
4: ; preds = %0, %16
%5 = phi i32 [ 0, %0 ], [ %17, %16 ]
%6 = call i32 (...) @fork()
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = call i32 @handle_fatal_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %16
10: ; preds = %4
%11 = icmp eq i32 %6, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = call i32 (...) @do_little()
br label %16
14: ; preds = %10
%15 = call i32 @waitpid(i32 %6, i32* %1, i32 0)
br label %16
16: ; preds = %8, %14, %12
%17 = add nuw nsw i32 %5, 1
%18 = icmp eq i32 %17, 50000
br i1 %18, label %19, label %4
19: ; preds = %16
%20 = call i64 (...) @clock()
%21 = sub nsw i64 %20, %3
%22 = sitofp i64 %21 to double
%23 = load double, double* @CLOCKS_PER_SEC
%24 = fdiv double %22, %23
%25 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), double %24)
%26 = load i32, i32* @EXIT_SUCCESS
%27 = call i32 @exit(i32 %26)
unreachable
}