#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 */

/* Variables and functions */
unsigned long long rdtsc();
int /*<<< orphan*/ usleep(unsigned long long);
void consume(unsigned long long sleep_us, unsigned long long sample_tsc,
             unsigned long long timeout_tsc) {
  unsigned long long end, now, end_sample;
  now = rdtsc();
  end = now + timeout_tsc;
  end_sample = now + sample_tsc;
  while (now < end) {
    now = rdtsc();
    if (now > end_sample) {
      usleep(sleep_us);
      end_sample += sample_tsc;
    }
  }
}


x86 assembly (clang, -O0):
<consume>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1030 <rdtsc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1198 <consume+0x78>
mov    $0x0,%al
call   1030 <rdtsc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    1193 <consume+0x73>
mov    -0x8(%rbp),%rdi
call   1040 <usleep@plt>
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1157 <consume+0x37>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consume>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1030 <rdtsc@plt>
add    %rax,%r12
cmp    %r12,%rax
jae    1171 <consume+0x51>
mov    %rax,%r13
add    %r14,%r13
jmp    1155 <consume+0x35>
nopl   0x0(%rax)
cmp    %r12,%rbx
jae    1171 <consume+0x51>
xor    %eax,%eax
call   1030 <rdtsc@plt>
mov    %rax,%rbx
cmp    %r13,%rax
jbe    1150 <consume+0x30>
mov    %r15,%rdi
call   1040 <usleep@plt>
add    %r14,%r13
jmp    1150 <consume+0x30>
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @consume(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%10 = call i64 (...) @rdtsc()
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = load i64, i64* %6
%13 = add i64 %11, %12
store i64 %13, i64* %7
%14 = load i64, i64* %8
%15 = load i64, i64* %5
%16 = add i64 %14, %15
store i64 %16, i64* %9
br label %17
17: ; preds = %32, %3
%18 = load i64, i64* %8
%19 = load i64, i64* %7
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = call i64 (...) @rdtsc()
store i64 %22, i64* %8
%23 = load i64, i64* %8
%24 = load i64, i64* %9
%25 = icmp ugt i64 %23, %24
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i64, i64* %4
%28 = call i32 @usleep(i64 %27)
%29 = load i64, i64* %5
%30 = load i64, i64* %9
%31 = add i64 %30, %29
store i64 %31, i64* %9
br label %32
32: ; preds = %26, %21
br label %17
33: ; preds = %17
ret void
}

LLVM IR (-O2):
define void @consume(i64 %0, i64 %1, i64 %2) {
%4 = tail call i64 (...) @rdtsc()
%5 = add i64 %4, %2
%6 = icmp ult i64 %4, %5
br i1 %6, label %7, label %19
7: ; preds = %3
%8 = add i64 %4, %1
br label %9
9: ; preds = %7, %16
%10 = phi i64 [ %17, %16 ], [ %8, %7 ]
%11 = tail call i64 (...) @rdtsc()
%12 = icmp ugt i64 %11, %10
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i32 @usleep(i64 %0)
%15 = add i64 %10, %1
br label %16
16: ; preds = %13, %9
%17 = phi i64 [ %15, %13 ], [ %10, %9 ]
%18 = icmp ult i64 %11, %5
br i1 %18, label %9, label %19
19: ; preds = %16, %3
ret void
}