#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 tv_sec;
  float tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, float, char *);
void print_time_elapsed(struct timespec *time_start, struct timespec *time_end,
                        char *str) {
  float time_in_ms =
      (time_end->tv_sec * 1000 - time_start->tv_sec * 1000 +
       (float)(time_end->tv_nsec - time_start->tv_nsec) / 1000000);
  if (time_in_ms < 0.0001) {
    printf("[%ld ns]: %s\n", time_end->tv_nsec - time_start->tv_nsec, str);
  } else if (time_in_ms < 1) {
    printf("[%f us]: %s\n", time_in_ms * 1000, str);
  } else if (time_in_ms < 1000) {
    printf("[%f ms]: %s\n", time_in_ms, str);
  } else {
    printf("[%f s]: %s\n", time_in_ms / 1000, str);
  }
}


x86 assembly (clang, -O0):
<print_time_elapsed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
imul   $0x3e8,(%rax),%eax
mov    -0x8(%rbp),%rcx
imul   $0x3e8,(%rcx),%ecx
sub    %ecx,%eax
cvtsi2ss %eax,%xmm0
mov    -0x10(%rbp),%rax
movss  0x4(%rax),%xmm1
mov    -0x8(%rbp),%rax
subss  0x4(%rax),%xmm1
movss  0xeb0(%rip),%xmm2
divss  %xmm2,%xmm1
addss  %xmm1,%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm1
movsd  0xe8a(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    11a7 <print_time_elapsed+0x97>
mov    -0x10(%rbp),%rax
movss  0x4(%rax),%xmm0
mov    -0x8(%rbp),%rax
subss  0x4(%rax),%xmm0
mov    -0x18(%rbp),%rsi
lea    0xe77(%rip),%rdi
call   1030 <printf@plt>
jmp    1232 <print_time_elapsed+0x122>
movss  0xe5d(%rip),%xmm0
ucomiss -0x1c(%rbp),%xmm0
jbe    11db <print_time_elapsed+0xcb>
movss  0xe4f(%rip),%xmm0
mulss  -0x1c(%rbp),%xmm0
mov    -0x18(%rbp),%rsi
lea    0xe51(%rip),%rdi
call   1030 <printf@plt>
jmp    122d <print_time_elapsed+0x11d>
movss  0xe2d(%rip),%xmm0
ucomiss -0x1c(%rbp),%xmm0
jbe    1207 <print_time_elapsed+0xf7>
movss  -0x1c(%rbp),%xmm0
mov    -0x18(%rbp),%rsi
lea    0xe32(%rip),%rdi
call   1030 <printf@plt>
jmp    1228 <print_time_elapsed+0x118>
movss  -0x1c(%rbp),%xmm0
movss  0xdfc(%rip),%xmm1
divss  %xmm1,%xmm0
mov    -0x18(%rbp),%rsi
lea    0xe19(%rip),%rdi
call   1030 <printf@plt>
jmp    122d <print_time_elapsed+0x11d>
jmp    1232 <print_time_elapsed+0x122>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_time_elapsed>:
mov    (%rsi),%eax
sub    (%rdi),%eax
imul   $0x3e8,%eax,%eax
cvtsi2ss %eax,%xmm2
movss  0x4(%rsi),%xmm1
subss  0x4(%rdi),%xmm1
movaps %xmm1,%xmm0
divss  0xecd(%rip),%xmm0
addss  %xmm2,%xmm0
xorps  %xmm2,%xmm2
cvtss2sd %xmm0,%xmm2
movsd  0xeca(%rip),%xmm3
ucomisd %xmm2,%xmm3
jbe    115e <print_time_elapsed+0x4e>
lea    0xec5(%rip),%rdi
movaps %xmm1,%xmm0
mov    %rdx,%rsi
jmp    1030 <printf@plt>
movss  0xe9e(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    1182 <print_time_elapsed+0x72>
mulss  0xe95(%rip),%xmm0
lea    0xeac(%rip),%rdi
mov    %rdx,%rsi
jmp    1030 <printf@plt>
movss  0xe7e(%rip),%xmm1
ucomiss %xmm0,%xmm1
jbe    119e <print_time_elapsed+0x8e>
lea    0xe9d(%rip),%rdi
mov    %rdx,%rsi
jmp    1030 <printf@plt>
divss  0xe62(%rip),%xmm0
lea    0xe93(%rip),%rdi
mov    %rdx,%rsi
jmp    1030 <printf@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, float }
@.str = [14 x i8] c"[%ld ns]: %s\0A\00"
@.str.1 = [13 x i8] c"[%f us]: %s\0A\00"
@.str.2 = [13 x i8] c"[%f ms]: %s\0A\00"
@.str.3 = [12 x i8] c"[%f s]: %s\0A\00"
define void @print_time_elapsed(%struct.timespec* %0, %struct.timespec* %1, i8* %2) {
%4 = alloca %struct.timespec*
%5 = alloca %struct.timespec*
%6 = alloca i8*
%7 = alloca float
store %struct.timespec* %0, %struct.timespec** %4
store %struct.timespec* %1, %struct.timespec** %5
store i8* %2, i8** %6
%8 = load %struct.timespec*, %struct.timespec** %5
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
%10 = load i32, i32* %9
%11 = mul nsw i32 %10, 1000
%12 = load %struct.timespec*, %struct.timespec** %4
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%14 = load i32, i32* %13
%15 = mul nsw i32 %14, 1000
%16 = sub nsw i32 %11, %15
%17 = sitofp i32 %16 to float
%18 = load %struct.timespec*, %struct.timespec** %5
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i32 0, i32 1
%20 = load float, float* %19
%21 = load %struct.timespec*, %struct.timespec** %4
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %21, i32 0, i32 1
%23 = load float, float* %22
%24 = fsub float %20, %23
%25 = fdiv float %24, 1.000000e+06
%26 = fadd float %17, %25
store float %26, float* %7
%27 = load float, float* %7
%28 = fpext float %27 to double
%29 = fcmp olt double %28, 1.000000e-04
br i1 %29, label %30, label %40
30: ; preds = %3
%31 = load %struct.timespec*, %struct.timespec** %5
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %31, i32 0, i32 1
%33 = load float, float* %32
%34 = load %struct.timespec*, %struct.timespec** %4
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %34, i32 0, i32 1
%36 = load float, float* %35
%37 = fsub float %33, %36
%38 = load i8*, i8** %6
%39 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), float %37, i8* %38)
br label %62
40: ; preds = %3
%41 = load float, float* %7
%42 = fcmp olt float %41, 1.000000e+00
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load float, float* %7
%45 = fmul float %44, 1.000000e+03
%46 = load i8*, i8** %6
%47 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), float %45, i8* %46)
br label %61
48: ; preds = %40
%49 = load float, float* %7
%50 = fcmp olt float %49, 1.000000e+03
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load float, float* %7
%53 = load i8*, i8** %6
%54 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), float %52, i8* %53)
br label %60
55: ; preds = %48
%56 = load float, float* %7
%57 = fdiv float %56, 1.000000e+03
%58 = load i8*, i8** %6
%59 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), float %57, i8* %58)
br label %60
60: ; preds = %55, %51
br label %61
61: ; preds = %60, %43
br label %62
62: ; preds = %61, %30
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, float }
@.str = [14 x i8] c"[%ld ns]: %s\0A\00"
@.str.1 = [13 x i8] c"[%f us]: %s\0A\00"
@.str.2 = [13 x i8] c"[%f ms]: %s\0A\00"
@.str.3 = [12 x i8] c"[%f s]: %s\0A\00"
define void @print_time_elapsed(%struct.timespec* nocapture %0, %struct.timespec* nocapture %1, i8* %2) {
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%7 = load i32, i32* %6
%8 = sub i32 %5, %7
%9 = mul i32 %8, 1000
%10 = sitofp i32 %9 to float
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%12 = load float, float* %11
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%14 = load float, float* %13
%15 = fsub float %12, %14
%16 = fdiv float %15, 1.000000e+06
%17 = fadd float %16, %10
%18 = fpext float %17 to double
%19 = fcmp olt double %18, 1.000000e-04
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), float %15, i8* %2)
br label %34
22: ; preds = %3
%23 = fcmp olt float %17, 1.000000e+00
br i1 %23, label %24, label %27
24: ; preds = %22
%25 = fmul float %17, 1.000000e+03
%26 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), float %25, i8* %2)
br label %34
27: ; preds = %22
%28 = fcmp olt float %17, 1.000000e+03
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), float %17, i8* %2)
br label %34
31: ; preds = %27
%32 = fdiv float %17, 1.000000e+03
%33 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), float %32, i8* %2)
br label %34
34: ; preds = %24, %31, %29, %20
ret void
}