#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 */
struct timeval {
  double tv_sec;
  double tv_usec;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
int /*<<< orphan*/ wait();
int main() {
  int process_count;
  long int i;
  long long int x = 0;
  pid_t pid;
  struct timeval start_time, stop_time, elapsed_time;
  gettimeofday(&start_time, NULL);
  printf("How many processes?   ");
  scanf("%d", &process_count);
  pid = fork();
  for (i = 1; i < process_count; i++) {
    if (pid == 0)
      pid = fork();
  }
  if (pid > 0) {
    for (i = 0; i < 500000000; i++) {
      x = x + 1;
    }
    gettimeofday(&stop_time, NULL);
    timersub(&stop_time, &start_time, &elapsed_time);
    printf("Process %d total time was %f seconds. x = %lld.\n", getpid(),
           elapsed_time.tv_sec + elapsed_time.tv_usec / 1000000.0, x);
  } else {
    wait();
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe76(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1060 <scanf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
movq   $0x1,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x8(%rbp),%rcx
cmp    %rcx,%rax
jge    1202 <main+0x92>
cmpq   $0x0,-0x20(%rbp)
jne    11ec <main+0x7c>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
jmp    11f1 <main+0x81>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11c5 <main+0x55>
cmpq   $0x0,-0x20(%rbp)
jle    1298 <main+0x128>
movq   $0x0,-0x10(%rbp)
cmpq   $0x1dcd6500,-0x10(%rbp)
1d
jge    1240 <main+0xd0>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1215 <main+0xa5>
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1070 <timersub@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
movsd  -0x50(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm1
movsd  0xd87(%rip),%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
mov    -0x18(%rbp),%rdx
lea    0xd96(%rip),%rdi
mov    $0x1,%al
call   1040 <printf@plt>
jmp    129f <main+0x12f>
mov    $0x0,%al
call   1080 <wait@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x40,%rsp
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0xe80(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe89(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1060 <scanf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    0xc(%rsp),%ecx
cmp    $0x2,%ecx
jl     11de <main+0x6e>
mov    $0x1,%ebx
jmp    11cc <main+0x5c>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
movslq %ecx,%rdx
cmp    %rdx,%rbx
jge    11de <main+0x6e>
test   %rax,%rax
jne    11c0 <main+0x50>
xor    %eax,%eax
call   1090 <fork@plt>
mov    0xc(%rsp),%ecx
jmp    11c0 <main+0x50>
test   %rax,%rax
jle    1236 <main+0xc6>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
call   1070 <timersub@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
movsd  0x18(%rsp),%xmm0
divsd  0xde7(%rip),%xmm0
addsd  0x10(%rsp),%xmm0
lea    0xdfc(%rip),%rdi
mov    $0x1dcd6500,%edx
mov    %eax,%esi
mov    $0x1,%al
call   1040 <printf@plt>
jmp    123d <main+0xcd>
xor    %eax,%eax
call   1080 <wait@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { double, double }
@.str = [23 x i8] c"How many processes? \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [49 x i8] c"Process %d total time was %f seconds. x = %lld.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
store i32 0, i32* %1
store i64 0, i64* %4
%9 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%12 = call i64 (...) @fork()
store i64 %12, i64* %5
store i64 1, i64* %3
br label %13
13: ; preds = %24, %0
%14 = load i64, i64* %3
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = icmp slt i64 %14, %16
br i1 %17, label %18, label %27
18: ; preds = %13
%19 = load i64, i64* %5
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i64 (...) @fork()
store i64 %22, i64* %5
br label %23
23: ; preds = %21, %18
br label %24
24: ; preds = %23
%25 = load i64, i64* %3
%26 = add nsw i64 %25, 1
store i64 %26, i64* %3
br label %13
27: ; preds = %13
%28 = load i64, i64* %5
%29 = icmp sgt i64 %28, 0
br i1 %29, label %30, label %52
30: ; preds = %27
store i64 0, i64* %3
br label %31
31: ; preds = %37, %30
%32 = load i64, i64* %3
%33 = icmp slt i64 %32, 500000000
br i1 %33, label %34, label %40
34: ; preds = %31
%35 = load i64, i64* %4
%36 = add nsw i64 %35, 1
store i64 %36, i64* %4
br label %37
37: ; preds = %34
%38 = load i64, i64* %3
%39 = add nsw i64 %38, 1
store i64 %39, i64* %3
br label %31
40: ; preds = %31
%41 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%42 = call i32 @timersub(%struct.timeval* %7, %struct.timeval* %6, %struct.timeval* %8)
%43 = call i32 (...) @getpid()
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%45 = load double, double* %44
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%47 = load double, double* %46
%48 = fdiv double %47, 1.000000e+06
%49 = fadd double %45, %48
%50 = load i64, i64* %4
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %43, double %49, i64 %50)
br label %54
52: ; preds = %27
%53 = call i32 (...) @wait()
br label %54
54: ; preds = %52, %40
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
%struct.timeval = type { double, double }
@.str = [23 x i8] c"How many processes? \00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [49 x i8] c"Process %d total time was %f seconds. x = %lld.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast i32* %1 to i8*
%6 = bitcast %struct.timeval* %2 to i8*
%7 = bitcast %struct.timeval* %3 to i8*
%8 = bitcast %struct.timeval* %4 to i8*
%9 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%12 = call i64 (...) @fork()
%13 = load i32, i32* %1
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %29
15: ; preds = %0, %23
%16 = phi i32 [ %24, %23 ], [ %13, %0 ]
%17 = phi i64 [ %25, %23 ], [ %12, %0 ]
%18 = phi i64 [ %26, %23 ], [ 1, %0 ]
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = call i64 (...) @fork()
%22 = load i32, i32* %1
br label %23
23: ; preds = %15, %20
%24 = phi i32 [ %22, %20 ], [ %16, %15 ]
%25 = phi i64 [ %21, %20 ], [ %17, %15 ]
%26 = add nuw nsw i64 %18, 1
%27 = sext i32 %24 to i64
%28 = icmp slt i64 %26, %27
br i1 %28, label %15, label %29
29: ; preds = %23, %0
%30 = phi i64 [ %12, %0 ], [ %25, %23 ]
%31 = icmp sgt i64 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%34 = call i32 @timersub(%struct.timeval* %3, %struct.timeval* %2, %struct.timeval* %4)
%35 = call i32 (...) @getpid()
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%37 = load double, double* %36
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%39 = load double, double* %38
%40 = fdiv double %39, 1.000000e+06
%41 = fadd double %37, %40
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %35, double %41, i64 500000000)
br label %45
43: ; preds = %29
%44 = call i32 (...) @wait()
br label %45
45: ; preds = %43, %32
ret i32 0
}