#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;
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                           struct timespec *, struct timespec *);
void throttle_sleep(uint64_t nsecs) {
  struct timespec sleep_time;
  sleep_time.tv_sec = nsecs / 1000000000;
  sleep_time.tv_nsec = nsecs % 1000000000;
  while (clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep_time, &sleep_time))
    ;
}


x86 assembly (clang, -O0):
<throttle_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x10(%rbp),%rcx
mov    %rcx,%rdx
call   1030 <clock_nanosleep@plt>
cmp    $0x0,%rax
je     115d <throttle_sleep+0x4d>
jmp    1137 <throttle_sleep+0x27>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<throttle_sleep>:
push   %r14
push   %rbx
push   %rax
movslq %edi,%rax
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3b9aca00,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0x4(%rsp)
mov    0x2ea7(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1030 <clock_nanosleep@plt>
test   %rax,%rax
jne    1150 <throttle_sleep+0x40>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define void @throttle_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = sdiv i32 %4, 1000000000
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %5, i32* %6
%7 = load i32, i32* %2
%8 = srem i32 %7, 1000000000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %8, i32* %9
br label %10
10: ; preds = %14, %1
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i64 @clock_nanosleep(i32 %11, i32 0, %struct.timespec* %3, %struct.timespec* %3)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %10
15: ; preds = %10
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define void @throttle_sleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = sdiv i32 %0, 1000000000
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %4, i32* %5
%6 = srem i32 %0, 1000000000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %6, i32* %7
br label %8
8: ; preds = %8, %1
%9 = load i32, i32* @CLOCK_MONOTONIC
%10 = call i64 @clock_nanosleep(i32 %9, i32 0, %struct.timespec* %2, %struct.timespec* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %8
12: ; preds = %8
ret void
}