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

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void sleep_for(long t) {
  struct timespec req;
  const time_t sec = (int)(t / 1000);
  const long ms = t - (sec * 1000);
  req.tv_sec = sec;
  req.tv_nsec = ms * 1000000L;
  while (-1 == nanosleep(&req, &req))
    ;
}


x86 assembly (clang, -O0):
<sleep_for>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
imul   $0x3e8,-0x1c(%rbp),%ecx
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
imul   $0xf4240,-0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1174 <sleep_for+0x64>
jmp    1154 <sleep_for+0x44>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_for>:
push   %rbx
sub    $0x10,%rsp
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %rdi,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
movabs $0x3e800000000,%rax
imul   %rdx,%rax
sar    $0x20,%rax
sub    %rax,%rdi
mov    %edx,(%rsp)
imul   $0xf4240,%rdi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1160 <sleep_for+0x50>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
define void @sleep_for(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i64
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = sdiv i64 %6, 1000
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = load i64, i64* %2
%10 = load i32, i32* %4
%11 = mul nsw i32 %10, 1000
%12 = sext i32 %11 to i64
%13 = sub nsw i64 %9, %12
store i64 %13, i64* %5
%14 = load i32, i32* %4
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i64, i64* %5
%17 = mul nsw i64 %16, 1000000
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %17, i64* %18
br label %19
19: ; preds = %22, %1
%20 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%21 = icmp eq i32 -1, %20
br i1 %21, label %22, label %23
22: ; preds = %19
br label %19
23: ; preds = %19
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
define void @sleep_for(i64 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = sdiv i64 %0, 1000
%5 = trunc i64 %4 to i32
%6 = mul i64 %4, 4294967296000
%7 = ashr exact i64 %6, 32
%8 = sub nsw i64 %0, %7
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %5, i32* %9
%10 = mul nsw i64 %8, 1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %10, i64* %11
br label %12
12: ; preds = %12, %1
%13 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %12, label %15
15: ; preds = %12
ret void
}