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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ EINTR;
scalar_t__ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                           struct timespec *, struct timespec *);
scalar_t__ errno;
void sleepMillis(uint32_t millis) {
  struct timespec sleep;
  sleep.tv_sec = millis / 1000;
  sleep.tv_nsec = (millis % 1000) * 1000000L;
  while (clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep, &sleep) && errno == EINTR)
    ;
}


x86 assembly (clang, -O0):
<sleepMillis>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
imul   $0xf4240,%rax,%rax
mov    %eax,-0xc(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x10(%rbp),%rcx
mov    %rcx,%rdx
call   1030 <clock_nanosleep@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x11(%rbp)
je     1184 <sleepMillis+0x74>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1194 <sleepMillis+0x84>
jmp    1199 <sleepMillis+0x89>
jmp    1141 <sleepMillis+0x31>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleepMillis>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x4(%rsp)
mov    0x2e9d(%rip),%r14
mov    %rsp,%rbx
mov    0x2e83(%rip),%r15
mov    0x2e74(%rip),%r12
nopl   0x0(%rax)
mov    (%r14),%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1030 <clock_nanosleep@plt>
test   %rax,%rax
je     117e <sleepMillis+0x6e>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1160 <sleepMillis+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @sleepMillis(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = sdiv i32 %4, 1000
%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, 1000
%9 = sext i32 %8 to i64
%10 = mul nsw i64 %9, 1000000
%11 = trunc i64 %10 to i32
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %11, i32* %12
br label %13
13: ; preds = %23, %1
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = call i64 @clock_nanosleep(i32 %14, i32 0, %struct.timespec* %3, %struct.timespec* %3)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %13
%22 = phi i1 [ false, %13 ], [ %20, %17 ]
br i1 %22, label %23, label %24
23: ; preds = %21
br label %13
24: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @sleepMillis(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = sdiv i32 %0, 1000
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %4, i32* %5
%6 = srem i32 %0, 1000
%7 = mul nsw i32 %6, 1000000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %7, i32* %8
br label %9
9: ; preds = %9, %1
%10 = load i32, i32* @CLOCK_MONOTONIC
%11 = call i64 @clock_nanosleep(i32 %10, i32 0, %struct.timespec* %2, %struct.timespec* %2)
%12 = icmp ne i64 %11, 0
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = select i1 %12, i1 %15, i1 false
br i1 %16, label %9, label %17
17: ; preds = %9
ret void
}