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

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void sleep_millis(size_t millis) {
  struct timespec req, rem;
  int ret;
  req.tv_sec = millis / 1000;
  req.tv_nsec = (millis % 1000) * 1000000ul;
  ret = nanosleep(&req, &rem);
  while (ret != 0)
    ret = nanosleep(&rem, &rem);
}


x86 assembly (clang, -O0):
<sleep_millis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
imul   $0xf4240,%rdx,%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1175 <sleep_millis+0x65>
lea    -0x28(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x2c(%rbp)
jmp    1157 <sleep_millis+0x47>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_millis>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
mov    %rdx,(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rdi
imul   $0xf4240,%rdi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     116f <sleep_millis+0x5f>
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
jne    1160 <sleep_millis+0x50>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
define void @sleep_millis(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = udiv i64 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %7, i64* %8
%9 = load i64, i64* %2
%10 = urem i64 %9, 1000
%11 = mul i64 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %11, i64* %12
%13 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %13, i32* %5
br label %14
14: ; preds = %17, %1
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
store i32 %18, i32* %5
br label %14
19: ; preds = %14
ret void
}

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