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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int atoi(char *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ getppid();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, double, double, char const *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
double time_diff(struct timeval *, struct timeval *);
int main(int argc, char *argv[]) {
  int i = 0;
  int n = atoi(argv[1]);
  int which = atoi(argv[2]);
  const char *name = "unknown";
  struct timeval tv;
  gettimeofday(&tv, NULL);
  struct timeval start;
  gettimeofday(&start, NULL);
  if (which == 0) {
    name = "gettimeofday";
    for (i = 0; i < n; ++i) {
      gettimeofday(&tv, NULL);
    }
  } else if (which == 1) {
    name = "time";
    for (i = 0; i < n; ++i) {
      time(NULL);
    }
  } else if (which == 2) {
    name = "clock_gettime";
    for (i = 0; i < n; ++i) {
      struct timespec ts;
      clock_gettime(CLOCK_REALTIME, &ts);
    }
  } else {
    name = "getppid";
    for (i = 0; i < n; ++i) {
      getppid();
    }
  }
  struct timeval end;
  gettimeofday(&end, NULL);
  double d = time_diff(&end, &start);
  printf("%f seconds, %f micro seconds per %s\n", d, d * 1000000 / n, name);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe54(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    121a <main+0xaa>
lea    0xe2d(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1215 <main+0xa5>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ee <main+0x7e>
jmp    12ed <main+0x17d>
cmpl   $0x1,-0x1c(%rbp)
jne    125e <main+0xee>
lea    0xdfb(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1259 <main+0xe9>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1236 <main+0xc6>
jmp    12e8 <main+0x178>
cmpl   $0x2,-0x1c(%rbp)
jne    12ab <main+0x13b>
lea    0xdae(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12a6 <main+0x136>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    127a <main+0x10a>
jmp    12e3 <main+0x173>
lea    0xd79(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12de <main+0x16e>
mov    $0x0,%al
call   1090 <getppid@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12bd <main+0x14d>
jmp    12e3 <main+0x173>
jmp    12e8 <main+0x178>
jmp    12ed <main+0x17d>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x48(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <time_diff@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  0xce7(%rip),%xmm1
mulsd  -0x50(%rbp),%xmm1
cvtsi2sdl -0x18(%rbp),%xmm2
divsd  %xmm2,%xmm1
mov    -0x28(%rbp),%rsi
lea    0xd01(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
mov    0x10(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %ebp,%ebp
je     11f3 <main+0x83>
cmp    $0x1,%ebp
je     1221 <main+0xb1>
cmp    $0x2,%ebp
jne    123e <main+0xce>
lea    0xe4e(%rip),%r14
test   %r15d,%r15d
jle    125c <main+0xec>
mov    0x2e09(%rip),%r12
lea    0x8(%rsp),%rbp
mov    %r15d,%ebx
nop
mov    (%r12),%edi
mov    %rbp,%rsi
call   1030 <clock_gettime@plt>
add    $0xffffffff,%ebx
jne    11e0 <main+0x70>
jmp    125c <main+0xec>
lea    0xe0e(%rip),%r14
test   %r15d,%r15d
jle    125c <main+0xec>
lea    0x18(%rsp),%rbp
mov    %r15d,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
add    $0xffffffff,%ebx
jne    1210 <main+0xa0>
jmp    125c <main+0xec>
lea    0xdf6(%rip),%r14
test   %r15d,%r15d
jle    125c <main+0xec>
mov    %r15d,%ebx
xor    %edi,%edi
call   1060 <time@plt>
add    $0xffffffff,%ebx
jne    1230 <main+0xc0>
jmp    125c <main+0xec>
lea    0xdde(%rip),%r14
test   %r15d,%r15d
jle    125c <main+0xec>
mov    %r15d,%ebx
nopl   (%rax)
xor    %eax,%eax
call   1090 <getppid@plt>
add    $0xffffffff,%ebx
jne    1250 <main+0xe0>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <time_diff@plt>
movsd  0xd80(%rip),%xmm1
mulsd  %xmm0,%xmm1
cvtsi2sd %r15d,%xmm2
divsd  %xmm2,%xmm1
lea    0xd97(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.timespec = type { i32 }
@.str = [8 x i8] c"unknown\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
@.str.2 = [5 x i8] c"time\00"
@.str.3 = [14 x i8] c"clock_gettime\00"
@CLOCK_REALTIME = global i32 0
@.str.4 = [8 x i8] c"getppid\00"
@.str.5 = [37 x i8] c"%f seconds, %f micro seconds per %s\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 i32
%9 = alloca i8*
%10 = alloca %struct.timeval
%11 = alloca %struct.timeval
%12 = alloca %struct.timespec
%13 = alloca %struct.timeval
%14 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%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* %7
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %8
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %9
%23 = call i32 @gettimeofday(%struct.timeval* %10, i32* null)
%24 = call i32 @gettimeofday(%struct.timeval* %11, i32* null)
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %2
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %9
store i32 0, i32* %6
br label %28
28: ; preds = %34, %27
%29 = load i32, i32* %6
%30 = load i32, i32* %7
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = call i32 @gettimeofday(%struct.timeval* %10, i32* null)
br label %34
34: ; preds = %32
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %28
37: ; preds = %28
br label %80
38: ; preds = %2
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %52
41: ; preds = %38
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %9
store i32 0, i32* %6
br label %42
42: ; preds = %48, %41
%43 = load i32, i32* %6
%44 = load i32, i32* %7
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = call i32 @time(i32* null)
br label %48
48: ; preds = %46
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %42
51: ; preds = %42
br label %79
52: ; preds = %38
%53 = load i32, i32* %8
%54 = icmp eq i32 %53, 2
br i1 %54, label %55, label %67
55: ; preds = %52
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8** %9
store i32 0, i32* %6
br label %56
56: ; preds = %63, %55
%57 = load i32, i32* %6
%58 = load i32, i32* %7
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %66
60: ; preds = %56
%61 = load i32, i32* @CLOCK_REALTIME
%62 = call i32 @clock_gettime(i32 %61, %struct.timespec* %12)
br label %63
63: ; preds = %60
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 1
store i32 %65, i32* %6
br label %56
66: ; preds = %56
br label %78
67: ; preds = %52
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8** %9
store i32 0, i32* %6
br label %68
68: ; preds = %74, %67
%69 = load i32, i32* %6
%70 = load i32, i32* %7
%71 = icmp slt i32 %69, %70
br i1 %71, label %72, label %77
72: ; preds = %68
%73 = call i32 (...) @getppid()
br label %74
74: ; preds = %72
%75 = load i32, i32* %6
%76 = add nsw i32 %75, 1
store i32 %76, i32* %6
br label %68
77: ; preds = %68
br label %78
78: ; preds = %77, %66
br label %79
79: ; preds = %78, %51
br label %80
80: ; preds = %79, %37
%81 = call i32 @gettimeofday(%struct.timeval* %13, i32* null)
%82 = call double @time_diff(%struct.timeval* %13, %struct.timeval* %11)
store double %82, double* %14
%83 = load double, double* %14
%84 = load double, double* %14
%85 = fmul double %84, 1.000000e+06
%86 = load i32, i32* %7
%87 = sitofp i32 %86 to double
%88 = fdiv double %85, %87
%89 = load i8*, i8** %9
%90 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), double %83, double %88, i8* %89)
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.timespec = type { i32 }
@.str.1 = [13 x i8] c"gettimeofday\00"
@.str.2 = [5 x i8] c"time\00"
@.str.3 = [14 x i8] c"clock_gettime\00"
@CLOCK_REALTIME = global i32 0
@.str.4 = [8 x i8] c"getppid\00"
@.str.5 = [37 x i8] c"%f seconds, %f micro seconds per %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.timespec
%6 = alloca %struct.timeval
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = bitcast %struct.timeval* %3 to i8*
%14 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%15 = bitcast %struct.timeval* %4 to i8*
%16 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
switch i32 %12, label %25 [
i32 0, label %23
i32 1, label %21
i32 2, label %17
]
17: ; preds = %2
%18 = icmp sgt i32 %9, 0
br i1 %18, label %19, label %48
19: ; preds = %17
%20 = bitcast %struct.timespec* %5 to i8*
br label %37
21: ; preds = %2
%22 = icmp sgt i32 %9, 0
br i1 %22, label %32, label %48
23: ; preds = %2
%24 = icmp sgt i32 %9, 0
br i1 %24, label %27, label %48
25: ; preds = %2
%26 = icmp sgt i32 %9, 0
br i1 %26, label %43, label %48
27: ; preds = %23, %27
%28 = phi i32 [ %30, %27 ], [ 0, %23 ]
%29 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%30 = add nuw nsw i32 %28, 1
%31 = icmp eq i32 %30, %9
br i1 %31, label %48, label %27
32: ; preds = %21, %32
%33 = phi i32 [ %35, %32 ], [ 0, %21 ]
%34 = tail call i32 @time(i32* null)
%35 = add nuw nsw i32 %33, 1
%36 = icmp eq i32 %35, %9
br i1 %36, label %48, label %32
37: ; preds = %19, %37
%38 = phi i32 [ 0, %19 ], [ %41, %37 ]
%39 = load i32, i32* @CLOCK_REALTIME
%40 = call i32 @clock_gettime(i32 %39, %struct.timespec* %5)
%41 = add nuw nsw i32 %38, 1
%42 = icmp eq i32 %41, %9
br i1 %42, label %48, label %37
43: ; preds = %25, %43
%44 = phi i32 [ %46, %43 ], [ 0, %25 ]
%45 = tail call i32 (...) @getppid()
%46 = add nuw nsw i32 %44, 1
%47 = icmp eq i32 %46, %9
br i1 %47, label %48, label %43
48: ; preds = %37, %32, %27, %43, %17, %21, %23, %25
%49 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %25 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %23 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %21 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %17 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), %43 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %27 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %32 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %37 ]
%50 = bitcast %struct.timeval* %6 to i8*
%51 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%52 = call double @time_diff(%struct.timeval* %6, %struct.timeval* %4)
%53 = fmul double %52, 1.000000e+06
%54 = sitofp i32 %9 to double
%55 = fdiv double %53, %54
%56 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), double %52, double %55, i8* %49)
ret i32 0
}