#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;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ empty_function();
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ printf(char *, double);
double timediff(struct timespec *, struct timespec *);
int main(int argc, char **argv) {
  struct timespec start;
  struct timespec end;
  clock_gettime(CLOCK_REALTIME, &start);
  for (unsigned long i = 0; i < 1.0e8; i++)
    ;
  clock_gettime(CLOCK_REALTIME, &end);
  double loop_time = timediff(&start, &end) / (double)1.0e8;
  printf("Loop takes %g nanoseconds\n", loop_time);
  clock_gettime(CLOCK_REALTIME, &start);
  for (unsigned long i = 0; i < 1.0e8; i++) {
    empty_function();
  }
  clock_gettime(CLOCK_REALTIME, &end);
  double function_time = timediff(&start, &end) / 1.0e8 - loop_time;
  printf("Function takes %g nanoseconds\n", function_time);
  clock_gettime(CLOCK_REALTIME, &start);
  for (unsigned long i = 0; i < 1.0e8; i++) {
    getuid();
  }
  clock_gettime(CLOCK_REALTIME, &end);
  double syscall_time = timediff(&start, &end) / 1.0e8 - loop_time;
  printf("getuid() takes %g nanoseconds\n", syscall_time);
}


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)
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
movq   $0x0,-0x28(%rbp)
movaps 0xe89(%rip),%xmm0
movq   -0x28(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xe88(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xe51(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11cf <main+0x7f>
jmp    11be <main+0x6e>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1180 <main+0x30>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <timediff@plt>
movsd  0xe0a(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
lea    0xe25(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
movq   $0x0,-0x38(%rbp)
movaps 0xddf(%rip),%xmm0
movq   -0x38(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xdde(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xda7(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    127b <main+0x12b>
mov    $0x0,%al
call   1060 <empty_function@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    122a <main+0xda>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <timediff@plt>
movsd  0xd5e(%rip),%xmm1
divsd  %xmm1,%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
lea    0xd8f(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
movq   $0x0,-0x48(%rbp)
movaps 0xd2e(%rip),%xmm0
movq   -0x48(%rbp),%xmm1
punpckldq %xmm0,%xmm1
movapd 0xd2d(%rip),%xmm0
subpd  %xmm0,%xmm1
movaps %xmm1,%xmm0
unpckhpd %xmm1,%xmm1
addsd  %xmm0,%xmm1
movsd  0xcf6(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    132c <main+0x1dc>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    12db <main+0x18b>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1070 <timediff@plt>
movsd  0xcad(%rip),%xmm1
divsd  %xmm1,%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
lea    0xcfd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e80(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <timediff@plt>
divsd  0xe6d(%rip),%xmm0
lea    0xe6e(%rip),%rdi
movsd  %xmm0,0x18(%rsp)
call   1050 <printf@plt>
mov    (%r15),%edi
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
mov    $0x5f5e100,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1060 <empty_function@plt>
add    $0xffffffffffffffff,%rbx
jne    11c0 <main+0x70>
mov    (%r15),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <timediff@plt>
divsd  0xe0b(%rip),%xmm0
subsd  0x18(%rsp),%xmm0
lea    0xe21(%rip),%rdi
call   1050 <printf@plt>
mov    (%r15),%edi
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
mov    $0x5f5e100,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getuid@plt>
add    $0xffffffffffffffff,%rbx
jne    1220 <main+0xd0>
mov    (%r15),%edi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <clock_gettime@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1070 <timediff@plt>
divsd  0xdae(%rip),%xmm0
subsd  0x18(%rsp),%xmm0
lea    0xde3(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
@.str = [27 x i8] c"Loop takes %g nanoseconds\0A\00"
@.str.1 = [31 x i8] c"Function takes %g nanoseconds\0A\00"
@.str.2 = [31 x i8] c"getuid() takes %g nanoseconds\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca i64
%9 = alloca double
%10 = alloca i64
%11 = alloca double
%12 = alloca i64
%13 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %6)
store i64 0, i64* %8
br label %16
16: ; preds = %21, %2
%17 = load i64, i64* %8
%18 = uitofp i64 %17 to double
%19 = fcmp olt double %18, 1.000000e+08
br i1 %19, label %20, label %24
20: ; preds = %16
br label %21
21: ; preds = %20
%22 = load i64, i64* %8
%23 = add i64 %22, 1
store i64 %23, i64* %8
br label %16
24: ; preds = %16
%25 = load i32, i32* @CLOCK_REALTIME
%26 = call i32 @clock_gettime(i32 %25, %struct.timespec* %7)
%27 = call double @timediff(%struct.timespec* %6, %struct.timespec* %7)
%28 = fdiv double %27, 1.000000e+08
store double %28, double* %9
%29 = load double, double* %9
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), double %29)
%31 = load i32, i32* @CLOCK_REALTIME
%32 = call i32 @clock_gettime(i32 %31, %struct.timespec* %6)
store i64 0, i64* %10
br label %33
33: ; preds = %39, %24
%34 = load i64, i64* %10
%35 = uitofp i64 %34 to double
%36 = fcmp olt double %35, 1.000000e+08
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = call i32 (...) @empty_function()
br label %39
39: ; preds = %37
%40 = load i64, i64* %10
%41 = add i64 %40, 1
store i64 %41, i64* %10
br label %33
42: ; preds = %33
%43 = load i32, i32* @CLOCK_REALTIME
%44 = call i32 @clock_gettime(i32 %43, %struct.timespec* %7)
%45 = call double @timediff(%struct.timespec* %6, %struct.timespec* %7)
%46 = fdiv double %45, 1.000000e+08
%47 = load double, double* %9
%48 = fsub double %46, %47
store double %48, double* %11
%49 = load double, double* %11
%50 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %49)
%51 = load i32, i32* @CLOCK_REALTIME
%52 = call i32 @clock_gettime(i32 %51, %struct.timespec* %6)
store i64 0, i64* %12
br label %53
53: ; preds = %59, %42
%54 = load i64, i64* %12
%55 = uitofp i64 %54 to double
%56 = fcmp olt double %55, 1.000000e+08
br i1 %56, label %57, label %62
57: ; preds = %53
%58 = call i32 (...) @getuid()
br label %59
59: ; preds = %57
%60 = load i64, i64* %12
%61 = add i64 %60, 1
store i64 %61, i64* %12
br label %53
62: ; preds = %53
%63 = load i32, i32* @CLOCK_REALTIME
%64 = call i32 @clock_gettime(i32 %63, %struct.timespec* %7)
%65 = call double @timediff(%struct.timespec* %6, %struct.timespec* %7)
%66 = fdiv double %65, 1.000000e+08
%67 = load double, double* %9
%68 = fsub double %66, %67
store double %68, double* %13
%69 = load double, double* %13
%70 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), double %69)
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_REALTIME = global i32 0
@.str = [27 x i8] c"Loop takes %g nanoseconds\0A\00"
@.str.1 = [31 x i8] c"Function takes %g nanoseconds\0A\00"
@.str.2 = [31 x i8] c"getuid() takes %g nanoseconds\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast %struct.timespec* %4 to i8*
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = load i32, i32* @CLOCK_REALTIME
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %4)
%11 = call double @timediff(%struct.timespec* %3, %struct.timespec* %4)
%12 = fdiv double %11, 1.000000e+08
%13 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), double %12)
%14 = load i32, i32* @CLOCK_REALTIME
%15 = call i32 @clock_gettime(i32 %14, %struct.timespec* %3)
br label %25
16: ; preds = %25
%17 = load i32, i32* @CLOCK_REALTIME
%18 = call i32 @clock_gettime(i32 %17, %struct.timespec* %4)
%19 = call double @timediff(%struct.timespec* %3, %struct.timespec* %4)
%20 = fdiv double %19, 1.000000e+08
%21 = fsub double %20, %12
%22 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), double %21)
%23 = load i32, i32* @CLOCK_REALTIME
%24 = call i32 @clock_gettime(i32 %23, %struct.timespec* %3)
br label %37
25: ; preds = %2, %25
%26 = phi i64 [ 0, %2 ], [ %28, %25 ]
%27 = call i32 (...) @empty_function()
%28 = add nuw nsw i64 %26, 1
%29 = icmp eq i64 %28, 100000000
br i1 %29, label %16, label %25
30: ; preds = %37
%31 = load i32, i32* @CLOCK_REALTIME
%32 = call i32 @clock_gettime(i32 %31, %struct.timespec* %4)
%33 = call double @timediff(%struct.timespec* %3, %struct.timespec* %4)
%34 = fdiv double %33, 1.000000e+08
%35 = fsub double %34, %12
%36 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), double %35)
ret i32 0
37: ; preds = %16, %37
%38 = phi i64 [ 0, %16 ], [ %40, %37 ]
%39 = call i32 (...) @getuid()
%40 = add nuw nsw i64 %38, 1
%41 = icmp eq i64 %40, 100000000
br i1 %41, label %30, label %37
}