#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 /*<<< orphan*/ uint64_t;
struct timespec {
  int /*<<< orphan*/ tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ rem;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ms_to_timespec(int /*<<< orphan*/, struct timespec *);
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void sleep_ms(uint64_t ms) {
  struct timespec req, rem;
  ms_to_timespec(ms, &req);
  memset(&rem, 0, sizeof(rem));
  do {
    if (nanosleep(&req, &rem) < 0) {
      if (errno == EINTR) {
        rem.tv_sec = req.tv_sec;
        rem.tv_nsec = req.tv_nsec;
        continue;
      }
    }
  } while (0);
}


x86 assembly (clang, -O0):
<sleep_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <ms_to_timespec@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <nanosleep@plt>
cmp    $0x0,%rax
jge    119e <sleep_ms+0x6e>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
jne    1199 <sleep_ms+0x69>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11a3 <sleep_ms+0x73>
jmp    119e <sleep_ms+0x6e>
jmp    11a3 <sleep_ms+0x73>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_ms>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1030 <ms_to_timespec@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <nanosleep@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @sleep_ms(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @ms_to_timespec(i32 %5, %struct.timespec* %3)
%7 = call i32 @memset(%struct.timespec* %4, i32 0, i32 8)
br label %8
8: ; preds = %1
%9 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %20, i32* %21
br label %24
22: ; preds = %11
br label %23
23: ; preds = %22, %8
br label %24
24: ; preds = %15, %23
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @sleep_ms(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = call i32 @ms_to_timespec(i32 %0, %struct.timespec* %2)
%7 = call i32 @memset(%struct.timespec* %3, i32 0, i32 8)
%8 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
ret void
}