#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 {
  long tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
void rate_limit(int milliseconds) {
  long step_ns = milliseconds * 1000000;
  struct timespec t;
  clock_gettime(CLOCK_REALTIME, &t);
  long sleep_ns = step_ns - (t.tv_nsec % step_ns);
  t.tv_sec = 0;
  t.tv_nsec = sleep_ns;
  nanosleep(&t, NULL);
}


x86 assembly (clang, -O0):
<rate_limit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
imul   $0xf4240,-0x4(%rbp),%eax
cltq
mov    %rax,-0x10(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
cqto
idivq  -0x10(%rbp)
mov    -0x30(%rbp),%rax
sub    %rdx,%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rate_limit>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
imul   $0xf4240,%edi,%ebp
movslq %ebp,%rbx
mov    0x2eaa(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%rax
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
je     115a <rate_limit+0x3a>
cqto
idiv   %rbx
jmp    115e <rate_limit+0x3e>
xor    %edx,%edx
div    %ebp
sub    %rdx,%rbx
movq   $0x0,0x10(%rsp)
mov    %rbx,0x8(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
define void @rate_limit(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.timespec
%5 = alloca i64
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = mul nsw i32 %6, 1000000
%8 = sext i32 %7 to i64
store i64 %8, i64* %3
%9 = load i32, i32* @CLOCK_REALTIME
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %4)
%11 = load i64, i64* %3
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* %3
%15 = srem i64 %13, %14
%16 = sub nsw i64 %11, %15
store i64 %16, i64* %5
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %17
%18 = load i64, i64* %5
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %18, i64* %19
%20 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_REALTIME = global i32 0
define void @rate_limit(i32 %0) {
%2 = alloca %struct.timespec
%3 = mul nsw i32 %0, 1000000
%4 = sext i32 %3 to i64
%5 = bitcast %struct.timespec* %2 to i8*
%6 = load i32, i32* @CLOCK_REALTIME
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %2)
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%9 = load i64, i64* %8
%10 = srem i64 %9, %4
%11 = sub nsw i64 %4, %10
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %12
store i64 %11, i64* %8
%13 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
ret void
}