#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 timespec {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ discrete_elapsed_hr(struct timespec *, struct timespec *,
                                       unsigned int *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(struct timespec *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int vfork();
int process_vfork_create(unsigned int iter) {
  pid_t child_pid;
  int status = 0;
  int i = 0;
  size_t sz = sizeof(struct timespec);
  struct timespec *overheads = NULL;
  overheads = (struct timespec *)malloc(2 * 1000 * sz);
  memset(overheads, 0, 2 * 1000 * sz);
  if (overheads == NULL) {
    perror("Process::vfork_create overheads allocation failure.");
    exit(1);
  }
  for (i = 0; i < 1000; i++) {
    clock_gettime(CLOCK_MONOTONIC, &overheads[2 * i]);
    clock_gettime(CLOCK_MONOTONIC, &overheads[2 * i + 1]);
  }
  struct timespec *results = NULL;
  results = (struct timespec *)malloc(2 * iter * sz);
  memset(results, 0, 2 * iter * sz);
  if (results == NULL) {
    perror("Process::vfork_create results allocation failure.");
    exit(1);
  }
  for (i = 0; i < iter; i++) {
    clock_gettime(CLOCK_MONOTONIC, &results[2 * i]);
    child_pid = vfork();
    if (child_pid == 0) {
      clock_gettime(CLOCK_MONOTONIC, &results[2 * i + 1]);
      _exit((int)1);
    } else if (child_pid == -1) {
      exit(1);
    } else {
    }
  }
  discrete_elapsed_hr(overheads, results, &iter, "Process::vfork_create");
  free(results);
  free(overheads);
  return 0;
}


x86 assembly (clang, -O0):
<process_vfork_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x4,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
imul   $0x7d0,-0x18(%rbp),%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
imul   $0x7d0,-0x18(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1202 <process_vfork_create+0x72>
lea    0xe0d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x3e8,-0x10(%rbp)
jge    1269 <process_vfork_create+0xd9>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <clock_gettime@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <clock_gettime@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1209 <process_vfork_create+0x79>
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
imul   -0x18(%rbp),%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
mov    %eax,%eax
imul   -0x18(%rbp),%rax
mov    %eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12c4 <process_vfork_create+0x134>
lea    0xd7f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1369 <process_vfork_create+0x1d9>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <clock_gettime@plt>
call   10a0 <vfork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1338 <process_vfork_create+0x1a8>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
call   1050 <clock_gettime@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
cmpl   $0xffffffff,-0x8(%rbp)
jne    134c <process_vfork_create+0x1bc>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1351 <process_vfork_create+0x1c1>
jmp    1356 <process_vfork_create+0x1c6>
jmp    135b <process_vfork_create+0x1cb>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12cb <process_vfork_create+0x13b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x4(%rbp),%rdx
lea    0xcea(%rip),%rcx
call   1090 <discrete_elapsed_hr@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_vfork_create>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
mov    %edi,0x4(%rsp)
mov    $0x1f40,%r14d
mov    %r14d,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
xor    %esi,%esi
mov    %rax,%rdi
mov    %r14d,%edx
call   1060 <memset@plt>
test   %r13,%r13
je     12ed <process_vfork_create+0x15d>
mov    %r13,0x10(%rsp)
lea    0x4(%r13),%rbp
xor    %eax,%eax
mov    0x2e0a(%rip),%r12
xchg   %ax,%ax
mov    %rax,%rbx
mov    (%r12),%edi
lea    (%rax,%r13,1),%rsi
call   1050 <clock_gettime@plt>
mov    (%r12),%edi
lea    (%rbx,%rbp,1),%rsi
call   1050 <clock_gettime@plt>
add    $0x8,%rbx
mov    %rbx,%rax
cmp    $0x1f40,%rbx
jne    11e0 <process_vfork_create+0x50>
mov    %r15d,%eax
lea    0x0(,%rax,8),%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
xor    %esi,%esi
mov    %rax,%rdi
mov    %ebx,%edx
call   1060 <memset@plt>
test   %r13,%r13
je     12f6 <process_vfork_create+0x166>
mov    %r13,0x8(%rsp)
test   %r15d,%r15d
je     1281 <process_vfork_create+0xf1>
mov    %r13,%rbp
mov    %r15d,%eax
mov    %eax,%r15d
xor    %eax,%eax
mov    %eax,%ebx
mov    %rbx,%r14
mov    (%r12),%edi
mov    %rbp,%rsi
call   1050 <clock_gettime@plt>
call   10a0 <vfork@plt>
cmp    $0xffffffff,%eax
je     12be <process_vfork_create+0x12e>
test   %eax,%eax
je     12c8 <process_vfork_create+0x138>
mov    %r14,%rax
add    $0x1,%rax
add    $0x2,%rbx
add    $0x8,%rbp
mov    %rax,%r14
cmp    %r15,%rax
jb     1250 <process_vfork_create+0xc0>
lea    0xdde(%rip),%rcx
lea    0x4(%rsp),%rdx
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1090 <discrete_elapsed_hr@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    (%r12),%edi
mov    %ebx,%eax
shl    $0x2,%rax
or     $0x4,%rax
mov    0x8(%rsp),%rsi
add    %rax,%rsi
call   1050 <clock_gettime@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
lea    0xd0c(%rip),%rdi
jmp    12fd <process_vfork_create+0x16d>
lea    0xd37(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [52 x i8] c"Process::vfork_create overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [50 x i8] c"Process::vfork_create results allocation failure.\00"
@.str.2 = [22 x i8] c"Process::vfork_create\00"
define i32 @process_vfork_create(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.timespec*
%8 = alloca %struct.timespec*
store i32 %0, i32* %2
store i32 0, i32* %4
store i32 0, i32* %5
store i64 4, i64* %6
store %struct.timespec* null, %struct.timespec** %7
%9 = load i64, i64* %6
%10 = mul i64 2000, %9
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to %struct.timespec*
store %struct.timespec* %13, %struct.timespec** %7
%14 = load %struct.timespec*, %struct.timespec** %7
%15 = load i64, i64* %6
%16 = mul i64 2000, %15
%17 = trunc i64 %16 to i32
%18 = call i32 @memset(%struct.timespec* %14, i32 0, i32 %17)
%19 = load %struct.timespec*, %struct.timespec** %7
%20 = icmp eq %struct.timespec* %19, null
br i1 %20, label %21, label %24
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %1
store i32 0, i32* %5
br label %25
25: ; preds = %44, %24
%26 = load i32, i32* %5
%27 = icmp slt i32 %26, 1000
br i1 %27, label %28, label %47
28: ; preds = %25
%29 = load i32, i32* @CLOCK_MONOTONIC
%30 = load %struct.timespec*, %struct.timespec** %7
%31 = load i32, i32* %5
%32 = mul nsw i32 2, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %30, i64 %33
%35 = call i32 @clock_gettime(i32 %29, %struct.timespec* %34)
%36 = load i32, i32* @CLOCK_MONOTONIC
%37 = load %struct.timespec*, %struct.timespec** %7
%38 = load i32, i32* %5
%39 = mul nsw i32 2, %38
%40 = add nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %37, i64 %41
%43 = call i32 @clock_gettime(i32 %36, %struct.timespec* %42)
br label %44
44: ; preds = %28
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
store i32 %46, i32* %5
br label %25
47: ; preds = %25
store %struct.timespec* null, %struct.timespec** %8
%48 = load i32, i32* %2
%49 = mul i32 2, %48
%50 = zext i32 %49 to i64
%51 = load i64, i64* %6
%52 = mul i64 %50, %51
%53 = trunc i64 %52 to i32
%54 = call i64 @malloc(i32 %53)
%55 = inttoptr i64 %54 to %struct.timespec*
store %struct.timespec* %55, %struct.timespec** %8
%56 = load %struct.timespec*, %struct.timespec** %8
%57 = load i32, i32* %2
%58 = mul i32 2, %57
%59 = zext i32 %58 to i64
%60 = load i64, i64* %6
%61 = mul i64 %59, %60
%62 = trunc i64 %61 to i32
%63 = call i32 @memset(%struct.timespec* %56, i32 0, i32 %62)
%64 = load %struct.timespec*, %struct.timespec** %8
%65 = icmp eq %struct.timespec* %64, null
br i1 %65, label %66, label %69
66: ; preds = %47
%67 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %47
store i32 0, i32* %5
br label %70
70: ; preds = %103, %69
%71 = load i32, i32* %5
%72 = load i32, i32* %2
%73 = icmp ult i32 %71, %72
br i1 %73, label %74, label %106
74: ; preds = %70
%75 = load i32, i32* @CLOCK_MONOTONIC
%76 = load %struct.timespec*, %struct.timespec** %8
%77 = load i32, i32* %5
%78 = mul nsw i32 2, %77
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds %struct.timespec, %struct.timespec* %76, i64 %79
%81 = call i32 @clock_gettime(i32 %75, %struct.timespec* %80)
%82 = call i32 @vfork()
store i32 %82, i32* %3
%83 = load i32, i32* %3
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %95
85: ; preds = %74
%86 = load i32, i32* @CLOCK_MONOTONIC
%87 = load %struct.timespec*, %struct.timespec** %8
%88 = load i32, i32* %5
%89 = mul nsw i32 2, %88
%90 = add nsw i32 %89, 1
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds %struct.timespec, %struct.timespec* %87, i64 %91
%93 = call i32 @clock_gettime(i32 %86, %struct.timespec* %92)
%94 = call i32 @_exit(i32 1)
unreachable
95: ; preds = %74
%96 = load i32, i32* %3
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %100
98: ; preds = %95
%99 = call i32 @exit(i32 1)
unreachable
100: ; preds = %95
br label %101
101: ; preds = %100
br label %102
102: ; preds = %101
br label %103
103: ; preds = %102
%104 = load i32, i32* %5
%105 = add nsw i32 %104, 1
store i32 %105, i32* %5
br label %70
106: ; preds = %70
%107 = load %struct.timespec*, %struct.timespec** %7
%108 = load %struct.timespec*, %struct.timespec** %8
%109 = call i32 @discrete_elapsed_hr(%struct.timespec* %107, %struct.timespec* %108, i32* %2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%110 = load %struct.timespec*, %struct.timespec** %8
%111 = call i32 @free(%struct.timespec* %110)
%112 = load %struct.timespec*, %struct.timespec** %7
%113 = call i32 @free(%struct.timespec* %112)
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [52 x i8] c"Process::vfork_create overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [50 x i8] c"Process::vfork_create results allocation failure.\00"
@.str.2 = [22 x i8] c"Process::vfork_create\00"
define i32 @process_vfork_create(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = call i64 @malloc(i32 8000)
%4 = inttoptr i64 %3 to %struct.timespec*
%5 = call i32 @memset(%struct.timespec* %4, i32 0, i32 8000)
%6 = icmp eq i64 %3, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1, %10
%11 = phi i64 [ %20, %10 ], [ 0, %1 ]
%12 = load i32, i32* @CLOCK_MONOTONIC
%13 = shl nuw nsw i64 %11, 1
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 %13
%15 = call i32 @clock_gettime(i32 %12, %struct.timespec* %14)
%16 = load i32, i32* @CLOCK_MONOTONIC
%17 = or i64 %13, 1
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 %17
%19 = call i32 @clock_gettime(i32 %16, %struct.timespec* %18)
%20 = add nuw nsw i64 %11, 1
%21 = icmp eq i64 %20, 1000
br i1 %21, label %22, label %10
22: ; preds = %10
%23 = shl i32 %0, 3
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to %struct.timespec*
%26 = call i32 @memset(%struct.timespec* %25, i32 0, i32 %23)
%27 = icmp eq i64 %24, 0
br i1 %27, label %32, label %28
28: ; preds = %22
%29 = icmp eq i32 %0, 0
br i1 %29, label %54, label %30
30: ; preds = %28
%31 = zext i32 %0 to i64
br label %35
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %30, %51
%36 = phi i64 [ 0, %30 ], [ %52, %51 ]
%37 = load i32, i32* @CLOCK_MONOTONIC
%38 = shl nuw nsw i64 %36, 1
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i64 %38
%40 = call i32 @clock_gettime(i32 %37, %struct.timespec* %39)
%41 = call i32 @vfork()
switch i32 %41, label %51 [
i32 0, label %42
i32 -1, label %49
]
42: ; preds = %35
%43 = load i32, i32* @CLOCK_MONOTONIC
%44 = and i64 %38, 4294967294
%45 = or i64 %44, 1
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i64 %45
%47 = call i32 @clock_gettime(i32 %43, %struct.timespec* %46)
%48 = call i32 @_exit(i32 1)
unreachable
49: ; preds = %35
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %35
%52 = add nuw nsw i64 %36, 1
%53 = icmp ult i64 %52, %31
br i1 %53, label %35, label %54
54: ; preds = %51, %28
%55 = call i32 @discrete_elapsed_hr(%struct.timespec* %4, %struct.timespec* %25, i32* %2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @free(%struct.timespec* %25)
%57 = call i32 @free(%struct.timespec* %4)
ret i32 0
}