#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 tv_sec;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
unsigned long long cycles_per_second() {
  double seconds;
  unsigned long long starttick, endtick;
  unsigned long long starttime, endtime;
  struct timeval tv;
  gettimeofday(&tv, 0);
  starttime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
  while (1) {
    gettimeofday(&tv, 0);
    endtime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    if (endtime != starttime) {
      break;
    }
  }
  while (1) {
    gettimeofday(&tv, 0);
    endtime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    if ((endtime - starttime) > 1) {
      ({
        __asm__ __volatile__("rdtsc\n\t"
                             "shlq\t$32, %%rdx\n\t"
                             "orq\t%%rdx, %%rax\n\t"
                             : "=A"(starttick)
                             :
                             : "%rdx");
      });
      break;
    }
  }
  starttime = endtime;
  while (1) {
    gettimeofday(&tv, 0);
    endtime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
    if ((endtime - starttime) > 1000) {
      ({
        __asm__ __volatile__("rdtsc\n\t"
                             "shlq\t$32, %%rdx\n\t"
                             "orq\t%%rdx, %%rax\n\t"
                             : "=A"(endtick)
                             :
                             : "%rdx");
      });
      break;
    }
  }
  return (endtick - starttick);
}


x86 assembly (clang, -O0):
<cycles_per_second>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,-0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,-0x30(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1185 <cycles_per_second+0x75>
jmp    118a <cycles_per_second+0x7a>
jmp    1145 <cycles_per_second+0x35>
jmp    118f <cycles_per_second+0x7f>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,-0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
cmp    $0x1,%rax
jbe    11e0 <cycles_per_second+0xd0>
rdtsc
shl    $0x20,%rdx
or     %rdx,%rax
mov    %rax,-0x10(%rbp)
jmp    11e5 <cycles_per_second+0xd5>
jmp    118f <cycles_per_second+0x7f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,-0x30(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x2c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
sub    -0x20(%rbp),%rax
cmp    $0x3e8,%rax
jbe    1240 <cycles_per_second+0x130>
rdtsc
shl    $0x20,%rdx
or     %rdx,%rax
mov    %rax,-0x18(%rbp)
jmp    1245 <cycles_per_second+0x135>
jmp    11ed <cycles_per_second+0xdd>
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cycles_per_second>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
imul   $0x3e8,%rax,%rax
movslq 0xc(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%r15
add    %rax,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
imul   $0x3e8,0x8(%rsp),%eax
movslq 0xc(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
add    %eax,%ecx
cmp    %r15d,%ecx
je     1160 <cycles_per_second+0x50>
lea    0x8(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
imul   $0x3e8,%rax,%rax
movslq 0xc(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%rbx
add    %rax,%rbx
mov    %rbx,%rax
sub    %r15,%rax
cmp    $0x2,%rax
jb     11a0 <cycles_per_second+0x90>
rdtsc
shl    $0x20,%rdx
or     %rdx,%rax
mov    %rax,%r14
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq 0x8(%rsp),%rax
imul   $0x3e8,%rax,%rax
movslq 0xc(%rsp),%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%rcx
add    %rax,%rcx
sub    %rbx,%rcx
cmp    $0x3e9,%rcx
jb     1200 <cycles_per_second+0xf0>
rdtsc
shl    $0x20,%rdx
or     %rdx,%rax
sub    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i64 @cycles_per_second() {
%1 = alloca double
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca %struct.timeval
%7 = call i32 @gettimeofday(%struct.timeval* %6, i32 0)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%9 = load i32, i32* %8
%10 = mul nsw i32 %9, 1000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%12 = load i32, i32* %11
%13 = sdiv i32 %12, 1000
%14 = add nsw i32 %10, %13
%15 = sext i32 %14 to i64
store i64 %15, i64* %4
br label %16
16: ; preds = %0, %30
%17 = call i32 @gettimeofday(%struct.timeval* %6, i32 0)
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = mul nsw i32 %19, 1000
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%22 = load i32, i32* %21
%23 = sdiv i32 %22, 1000
%24 = add nsw i32 %20, %23
%25 = sext i32 %24 to i64
store i64 %25, i64* %5
%26 = load i64, i64* %5
%27 = load i64, i64* %4
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %16
br label %31
30: ; preds = %16
br label %16
31: ; preds = %29
br label %32
32: ; preds = %31, %48
%33 = call i32 @gettimeofday(%struct.timeval* %6, i32 0)
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = mul nsw i32 %35, 1000
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%38 = load i32, i32* %37
%39 = sdiv i32 %38, 1000
%40 = add nsw i32 %36, %39
%41 = sext i32 %40 to i64
store i64 %41, i64* %5
%42 = load i64, i64* %5
%43 = load i64, i64* %4
%44 = sub i64 %42, %43
%45 = icmp ugt i64 %44, 1
br i1 %45, label %46, label %48
46: ; preds = %32
%47 = call i64 asm sideeffect "rdtsc\0A\09shlq\09$$32, %rdx\0A\09orq\09%rdx, %rax\0A\09", "=A,~{rdx},~{dirflag},~{fpsr},~{flags}"()
store i64 %47, i64* %2
br label %49
48: ; preds = %32
br label %32
49: ; preds = %46
%50 = load i64, i64* %5
store i64 %50, i64* %4
br label %51
51: ; preds = %49, %67
%52 = call i32 @gettimeofday(%struct.timeval* %6, i32 0)
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%54 = load i32, i32* %53
%55 = mul nsw i32 %54, 1000
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%57 = load i32, i32* %56
%58 = sdiv i32 %57, 1000
%59 = add nsw i32 %55, %58
%60 = sext i32 %59 to i64
store i64 %60, i64* %5
%61 = load i64, i64* %5
%62 = load i64, i64* %4
%63 = sub i64 %61, %62
%64 = icmp ugt i64 %63, 1000
br i1 %64, label %65, label %67
65: ; preds = %51
%66 = call i64 asm sideeffect "rdtsc\0A\09shlq\09$$32, %rdx\0A\09orq\09%rdx, %rax\0A\09", "=A,~{rdx},~{dirflag},~{fpsr},~{flags}"()
store i64 %66, i64* %3
br label %68
67: ; preds = %51
br label %51
68: ; preds = %65
%69 = load i64, i64* %3
%70 = load i64, i64* %2
%71 = sub i64 %69, %70
ret i64 %71
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i64 @cycles_per_second() {
%1 = alloca %struct.timeval
%2 = bitcast %struct.timeval* %1 to i8*
%3 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = mul nsw i32 %5, 1000
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%8 = load i32, i32* %7
%9 = sdiv i32 %8, 1000
%10 = add nsw i32 %9, %6
br label %11
11: ; preds = %11, %0
%12 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%13 = load i32, i32* %4
%14 = mul nsw i32 %13, 1000
%15 = load i32, i32* %7
%16 = sdiv i32 %15, 1000
%17 = add nsw i32 %16, %14
%18 = icmp eq i32 %17, %10
br i1 %18, label %11, label %19
19: ; preds = %11
%20 = sext i32 %10 to i64
br label %21
21: ; preds = %19, %21
%22 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%23 = load i32, i32* %4
%24 = mul nsw i32 %23, 1000
%25 = load i32, i32* %7
%26 = sdiv i32 %25, 1000
%27 = add nsw i32 %26, %24
%28 = sext i32 %27 to i64
%29 = sub nsw i64 %28, %20
%30 = icmp ugt i64 %29, 1
br i1 %30, label %31, label %21
31: ; preds = %21
%32 = call i64 asm sideeffect "rdtsc\0A\09shlq\09$$32, %rdx\0A\09orq\09%rdx, %rax\0A\09", "=A,~{rdx},~{dirflag},~{fpsr},~{flags}"()
br label %33
33: ; preds = %33, %31
%34 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%35 = load i32, i32* %4
%36 = mul nsw i32 %35, 1000
%37 = load i32, i32* %7
%38 = sdiv i32 %37, 1000
%39 = add nsw i32 %38, %36
%40 = sext i32 %39 to i64
%41 = sub nsw i64 %40, %28
%42 = icmp ugt i64 %41, 1000
br i1 %42, label %43, label %33
43: ; preds = %33
%44 = call i64 asm sideeffect "rdtsc\0A\09shlq\09$$32, %rdx\0A\09orq\09%rdx, %rax\0A\09", "=A,~{rdx},~{dirflag},~{fpsr},~{flags}"()
%45 = sub i64 %44, %32
ret i64 %45
}