#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 scalar_t__ uint64_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, scalar_t__, scalar_t__, scalar_t__);
int /*<<< orphan*/ puts(char *);
scalar_t__ rdtsc_end();
scalar_t__ rdtsc_start();
unsigned int sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int tries = 0;
  uint64_t start = rdtsc_start();
  while (1) {
    unsigned int remaining = sleep(1);
    if (!remaining)
      break;
    if (tries++ == 100) {
      fprintf(stderr, "failed to sleep for 1 second!\n");
      return 1;
    }
  }
  uint64_t end = rdtsc_end();
  if (end < start || end - start < 1000000) {
    fprintf(stderr,
            "RDTSC difference doesn't correspond to sleep of 1 second!\n");
    return 1;
  }
  printf("RDTSC: start = %lu, end = %lu, diff = %lu\n", start, end,
         end - start);
  puts("TEST OK!");
  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)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1050 <rdtsc_start@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11a4 <main+0x44>
jmp    11de <main+0x7e>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x64,%eax
jne    11d9 <main+0x79>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125b <main+0xfb>
jmp    1188 <main+0x28>
mov    $0x0,%al
call   1060 <rdtsc_end@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jl     120b <main+0xab>
mov    -0x30(%rbp),%rax
sub    -0x20(%rbp),%rax
cmp    $0xf4240,%rax
jge    122c <main+0xcc>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    125b <main+0xfb>
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
mov    -0x30(%rbp),%rcx
sub    -0x20(%rbp),%rcx
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1050 <rdtsc_start@plt>
mov    %rax,%rbx
mov    $0x65,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x1,%edi
call   1080 <sleep@plt>
test   %eax,%eax
je     11a5 <main+0x45>
add    $0xffffffff,%ebp
jne    1180 <main+0x20>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
jmp    11cd <main+0x6d>
xor    %eax,%eax
call   1060 <rdtsc_end@plt>
mov    %rax,%rcx
sub    %rbx,%rcx
jl     11bd <main+0x5d>
cmp    $0xf423f,%rcx
jg     11de <main+0x7e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe75(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <printf@plt>
lea    0xe8e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11d7 <main+0x77>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"failed to sleep for 1 second!\0A\00"
@.str.1 = [59 x i8] c"RDTSC difference doesn't correspond to sleep of 1 second!\0A\00"
@.str.2 = [43 x i8] c"RDTSC: start = %lu, end = %lu, diff = %lu\0A\00"
@.str.3 = [9 x i8] c"TEST OK!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = call i64 (...) @rdtsc_start()
store i64 %10, i64* %7
br label %11
11: ; preds = %2, %23
%12 = call i32 @sleep(i32 1)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %11
br label %24
16: ; preds = %11
%17 = load i32, i32* %6
%18 = add nsw i32 %17, 1
store i32 %18, i32* %6
%19 = icmp eq i32 %17, 100
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %45
23: ; preds = %16
br label %11
24: ; preds = %15
%25 = call i64 (...) @rdtsc_end()
store i64 %25, i64* %9
%26 = load i64, i64* %9
%27 = load i64, i64* %7
%28 = icmp slt i64 %26, %27
br i1 %28, label %34, label %29
29: ; preds = %24
%30 = load i64, i64* %9
%31 = load i64, i64* %7
%32 = sub nsw i64 %30, %31
%33 = icmp slt i64 %32, 1000000
br i1 %33, label %34, label %37
34: ; preds = %29, %24
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %45
37: ; preds = %29
%38 = load i64, i64* %7
%39 = load i64, i64* %9
%40 = load i64, i64* %9
%41 = load i64, i64* %7
%42 = sub nsw i64 %40, %41
%43 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i64 %38, i64 %39, i64 %42)
%44 = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %45
45: ; preds = %37, %34, %20
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"failed to sleep for 1 second!\0A\00"
@.str.1 = [59 x i8] c"RDTSC difference doesn't correspond to sleep of 1 second!\0A\00"
@.str.2 = [43 x i8] c"RDTSC: start = %lu, end = %lu, diff = %lu\0A\00"
@.str.3 = [9 x i8] c"TEST OK!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @rdtsc_start()
br label %4
4: ; preds = %8, %2
%5 = phi i32 [ 0, %2 ], [ %9, %8 ]
%6 = tail call i32 @sleep(i32 1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %4
%9 = add nuw nsw i32 %5, 1
%10 = icmp eq i32 %5, 100
br i1 %10, label %11, label %4
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %26
14: ; preds = %4
%15 = tail call i64 (...) @rdtsc_end()
%16 = icmp slt i64 %15, %3
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = sub nsw i64 %15, %3
%19 = icmp slt i64 %18, 1000000
br i1 %19, label %20, label %23
20: ; preds = %17, %14
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
br label %26
23: ; preds = %17
%24 = tail call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i64 %3, i64 %15, i64 %18)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %11, %20, %23
%27 = phi i32 [ 1, %20 ], [ 0, %23 ], [ 1, %11 ]
ret i32 %27
}