#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_MONOTONIC;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ elapsed_time_hr(struct timespec, struct timespec, char *);
void loop_timer(unsigned long limit) {
  struct timespec t1, t2;
  int index;
  clock_gettime(CLOCK_MONOTONIC, &t1);
  for (index = 0; index < limit; index++) {
  }
  clock_gettime(CLOCK_MONOTONIC, &t2);
  elapsed_time_hr(t1, t2, "Loop on its own");
}


x86 assembly (clang, -O0):
<loop_timer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
movl   $0x0,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1166 <loop_timer+0x46>
jmp    1158 <loop_timer+0x38>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1145 <loop_timer+0x25>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    -0x10(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    0xe7b(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop_timer>:
push   %rbx
sub    $0x10,%rsp
mov    0x2ebc(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
mov    0x8(%rsp),%edi
mov    (%rsp),%esi
lea    0xeb0(%rip),%rdx
call   1030 <elapsed_time_hr@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [16 x i8] c"Loop on its own\00"
define void @loop_timer(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
store i64 %0, i64* %2
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %3)
store i32 0, i32* %5
br label %8
8: ; preds = %14, %1
%9 = load i32, i32* %5
%10 = sext i32 %9 to i64
%11 = load i64, i64* %2
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %17
13: ; preds = %8
br label %14
14: ; preds = %13
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
store i32 %16, i32* %5
br label %8
17: ; preds = %8
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = call i32 @clock_gettime(i32 %18, %struct.timespec* %4)
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @elapsed_time_hr(i32 %21, i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@CLOCK_MONOTONIC = global i32 0
@.str = [16 x i8] c"Loop on its own\00"
define void @loop_timer(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = load i32, i32* @CLOCK_MONOTONIC
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %2)
%8 = load i32, i32* @CLOCK_MONOTONIC
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %3)
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @elapsed_time_hr(i32 %11, i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
ret void
}