#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 */
typedef int uint64_t;

/* Variables and functions */
int cpu_speed();
int /*<<< orphan*/ printf(char *, int, ...);
int rdtsc();
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  uint64_t t1, t2;
  printf("CPU Speed: %u\n", cpu_speed());
  t1 = rdtsc();
  usleep(500 * 1000);
  t2 = rdtsc();
  printf("%Lu - %Lu = %Lu\n", t2, t1, t2 - t1);
  printf("%Lu - %Lu = %Lu\n", t2 / cpu_speed(), t1 / cpu_speed(),
         (t2 - t1) / cpu_speed());
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <cpu_speed@plt>
mov    %eax,%esi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %eax,-0x14(%rbp)
mov    $0x7a120,%edi
call   1060 <usleep@plt>
mov    $0x0,%al
call   1040 <rdtsc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1050 <cpu_speed@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <cpu_speed@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <cpu_speed@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
cltd
idiv   %ecx
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %eax,%eax
call   1050 <cpu_speed@plt>
lea    0xeaa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    %eax,%r15d
mov    $0x7a120,%edi
call   1060 <usleep@plt>
xor    %eax,%eax
call   1040 <rdtsc@plt>
mov    %eax,%ebp
mov    %eax,%ebx
sub    %r15d,%ebx
lea    0xe87(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
mov    %r15d,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1050 <cpu_speed@plt>
mov    %eax,%ecx
mov    %ebp,%eax
cltd
idiv   %ecx
mov    %eax,%r12d
xor    %eax,%eax
call   1050 <cpu_speed@plt>
mov    %eax,%ecx
mov    %r15d,%eax
cltd
idiv   %ecx
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <cpu_speed@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"CPU Speed: %u\0A\00"
@.str.1 = [17 x i8] c"%Lu - %Lu = %Lu\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @cpu_speed()
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = call i32 (...) @rdtsc()
store i32 %10, i32* %6
%11 = call i32 @usleep(i32 500000)
%12 = call i32 (...) @rdtsc()
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = load i32, i32* %6
%17 = sub nsw i32 %15, %16
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14, i32 %17)
%19 = load i32, i32* %7
%20 = call i32 (...) @cpu_speed()
%21 = sdiv i32 %19, %20
%22 = load i32, i32* %6
%23 = call i32 (...) @cpu_speed()
%24 = sdiv i32 %22, %23
%25 = load i32, i32* %7
%26 = load i32, i32* %6
%27 = sub nsw i32 %25, %26
%28 = call i32 (...) @cpu_speed()
%29 = sdiv i32 %27, %28
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %24, i32 %29)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"CPU Speed: %u\0A\00"
@.str.1 = [17 x i8] c"%Lu - %Lu = %Lu\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @cpu_speed()
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = tail call i32 (...) @rdtsc()
%6 = tail call i32 @usleep(i32 500000)
%7 = tail call i32 (...) @rdtsc()
%8 = sub nsw i32 %7, %5
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %5, i32 %8)
%10 = tail call i32 (...) @cpu_speed()
%11 = sdiv i32 %7, %10
%12 = tail call i32 (...) @cpu_speed()
%13 = sdiv i32 %5, %12
%14 = tail call i32 (...) @cpu_speed()
%15 = sdiv i32 %8, %14
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %13, i32 %15)
ret i32 0
}