#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_destroy(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_destroy 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_destroy results allocation failure.");
    exit(1);
  }
  for (i = 0; i < iter; i++) {
    child_pid = vfork();
    if (child_pid == 0) {
      clock_gettime(CLOCK_MONOTONIC, &results[2 * i]);
      _exit((int)1);
    } else if (child_pid == -1) {
      exit(1);
    } else {
      clock_gettime(CLOCK_MONOTONIC, &results[2 * i + 1]);
    }
  }
  discrete_elapsed_hr(overheads, results, &iter, "Process::vfork_destroy");
  free(results);
  free(overheads);
  return 0;
}


x86 assembly (clang, -O0):
<process_vfork_destroy>:
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_destroy+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_destroy+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_destroy+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_destroy+0x134>
lea    0xd80(%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    1364 <process_vfork_destroy+0x1d4>
call   10a0 <vfork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1314 <process_vfork_destroy+0x184>
mov    0x2cf8(%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>
mov    $0x1,%edi
call   1040 <_exit@plt>
cmpl   $0xffffffff,-0x8(%rbp)
jne    1328 <process_vfork_destroy+0x198>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cb9(%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>
jmp    1351 <process_vfork_destroy+0x1c1>
jmp    1356 <process_vfork_destroy+0x1c6>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12cb <process_vfork_destroy+0x13b>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x4(%rbp),%rdx
lea    0xcf1(%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_destroy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
mov    %edi,0xc(%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     12eb <process_vfork_destroy+0x15b>
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_destroy+0x50>
mov    %r15d,%eax
lea    0x0(,%rax,8),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
xor    %esi,%esi
mov    %rax,%rdi
mov    %ebp,%edx
call   1060 <memset@plt>
test   %r13,%r13
je     12f4 <process_vfork_destroy+0x164>
test   %r15d,%r15d
je     1281 <process_vfork_destroy+0xf1>
mov    %r15d,%eax
mov    %eax,%r15d
lea    0x4(%r13),%rbp
xor    %eax,%eax
mov    %eax,%ebx
mov    %rbx,%r14
nopl   0x0(%rax)
call   10a0 <vfork@plt>
cmp    $0xffffffff,%eax
je     12be <process_vfork_destroy+0x12e>
test   %eax,%eax
je     12c8 <process_vfork_destroy+0x138>
mov    (%r12),%edi
mov    %rbp,%rsi
call   1050 <clock_gettime@plt>
mov    %r14,%rax
add    $0x1,%rax
add    $0x2,%rbx
add    $0x8,%rbp
mov    %rax,%r14
cmp    %r15,%rax
jb     1250 <process_vfork_destroy+0xc0>
lea    0xde0(%rip),%rcx
lea    0xc(%rsp),%rdx
mov    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1090 <discrete_elapsed_hr@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%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
and    $0xfffffffe,%eax
lea    0x0(,%rax,4),%rsi
add    %r13,%rsi
call   1050 <clock_gettime@plt>
mov    $0x1,%edi
call   1040 <_exit@plt>
lea    0xd0e(%rip),%rdi
jmp    12fb <process_vfork_destroy+0x16b>
lea    0xd3a(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [53 x i8] c"Process::vfork_destroy overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [51 x i8] c"Process::vfork_destroy results allocation failure.\00"
@.str.2 = [23 x i8] c"Process::vfork_destroy\00"
define i32 @process_vfork_destroy(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 ([53 x i8], [53 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 ([51 x i8], [51 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 = call i32 @vfork()
store i32 %75, i32* %3
%76 = load i32, i32* %3
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %87
78: ; preds = %74
%79 = load i32, i32* @CLOCK_MONOTONIC
%80 = load %struct.timespec*, %struct.timespec** %8
%81 = load i32, i32* %5
%82 = mul nsw i32 2, %81
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds %struct.timespec, %struct.timespec* %80, i64 %83
%85 = call i32 @clock_gettime(i32 %79, %struct.timespec* %84)
%86 = call i32 @_exit(i32 1)
unreachable
87: ; preds = %74
%88 = load i32, i32* %3
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %92
90: ; preds = %87
%91 = call i32 @exit(i32 1)
unreachable
92: ; preds = %87
%93 = load i32, i32* @CLOCK_MONOTONIC
%94 = load %struct.timespec*, %struct.timespec** %8
%95 = load i32, i32* %5
%96 = mul nsw i32 2, %95
%97 = add nsw i32 %96, 1
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds %struct.timespec, %struct.timespec* %94, i64 %98
%100 = call i32 @clock_gettime(i32 %93, %struct.timespec* %99)
br label %101
101: ; preds = %92
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 ([23 x i8], [23 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 = [53 x i8] c"Process::vfork_destroy overheads allocation failure.\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [51 x i8] c"Process::vfork_destroy results allocation failure.\00"
@.str.2 = [23 x i8] c"Process::vfork_destroy\00"
define i32 @process_vfork_destroy(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 ([53 x i8], [53 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 %55, label %30
30: ; preds = %28
%31 = zext i32 %0 to i64
br label %35
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %30, %47
%36 = phi i64 [ 0, %30 ], [ %53, %47 ]
%37 = call i32 @vfork()
switch i32 %37, label %47 [
i32 0, label %38
i32 -1, label %45
]
38: ; preds = %35
%39 = load i32, i32* @CLOCK_MONOTONIC
%40 = shl nuw i64 %36, 1
%41 = and i64 %40, 4294967294
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i64 %41
%43 = call i32 @clock_gettime(i32 %39, %struct.timespec* %42)
%44 = call i32 @_exit(i32 1)
unreachable
45: ; preds = %35
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %35
%48 = load i32, i32* @CLOCK_MONOTONIC
%49 = shl nuw nsw i64 %36, 1
%50 = or i64 %49, 1
%51 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i64 %50
%52 = call i32 @clock_gettime(i32 %48, %struct.timespec* %51)
%53 = add nuw nsw i64 %36, 1
%54 = icmp ult i64 %53, %31
br i1 %54, label %35, label %55
55: ; preds = %47, %28
%56 = call i32 @discrete_elapsed_hr(%struct.timespec* %4, %struct.timespec* %25, i32* %2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%57 = call i32 @free(%struct.timespec* %25)
%58 = call i32 @free(%struct.timespec* %4)
ret i32 0
}