#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int time_t;
struct timespec {
  int tv_sec;
  double tv_nsec;
};
struct TYPE_3__ {
  int rem;
  scalar_t__ quot;
};
typedef TYPE_1__ ldiv_t;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int EINTR;
int /*<<< orphan*/ TIMER_ABSTIME;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    int /*<<< orphan*/ *);
TYPE_1__ ldiv(int, int);
void msleep(double wait_ms) {
  struct timespec req;
  int ret;
  assert(!clock_gettime(CLOCK_MONOTONIC, &req));
  if (wait_ms > 1000) {
    time_t sec = wait_ms / 1000;
    wait_ms -= sec * 1000;
    assert(wait_ms > 0);
    req.tv_sec += sec;
  }
  req.tv_nsec += wait_ms * 1000000;
  if (req.tv_nsec >= 1000000000) {
    ldiv_t result = ldiv(req.tv_nsec, 1000000000);
    req.tv_sec += result.quot;
    req.tv_nsec = result.rem;
  }
  do {
    ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &req, NULL);
  } while (ret == EINTR);
  assert(ret == 0);
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movsd  -0x8(%rbp),%xmm0
movsd  0xe82(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11db <msleep+0x9b>
movsd  -0x8(%rbp),%xmm0
movsd  0xe6b(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x20(%rbp)
imul   $0x3e8,-0x20(%rbp),%eax
cvtsi2sd %eax,%xmm1
movsd  -0x8(%rbp),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
movsd  0xe23(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  0xe01(%rip),%xmm1
ucomisd %xmm1,%xmm0
jb     123f <msleep+0xff>
cvttsd2si -0x10(%rbp),%edi
mov    $0x3b9aca00,%esi
call   1040 <ldiv@plt>
mov    %eax,-0x30(%rbp)
mov    %rdx,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
movslq -0x18(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
cvtsi2sdl -0x30(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1244 <msleep+0x104>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <clock_nanosleep@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2d60(%rip),%rcx
cmp    (%rcx),%eax
je     1244 <msleep+0x104>
cmpl   $0x0,-0x1c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,(%rsp)
mov    0x2e89(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
movsd  (%rsp),%xmm1
ucomisd 0xe82(%rip),%xmm1
jbe    11c2 <msleep+0x82>
movapd %xmm1,%xmm0
divsd  0xe74(%rip),%xmm0
cvttsd2si %xmm0,%ebx
imul   $0x3e8,%ebx,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
subsd  %xmm0,%xmm1
movsd  %xmm1,(%rsp)
xorpd  %xmm0,%xmm0
xor    %edi,%edi
ucomisd %xmm0,%xmm1
seta   %dil
call   1050 <assert@plt>
movsd  (%rsp),%xmm1
add    %ebx,0x8(%rsp)
mulsd  0xe3e(%rip),%xmm1
addsd  0x10(%rsp),%xmm1
movsd  %xmm1,0x10(%rsp)
ucomisd 0xe32(%rip),%xmm1
jb     11ff <msleep+0xbf>
cvttsd2si %xmm1,%edi
mov    $0x3b9aca00,%esi
call   1040 <ldiv@plt>
add    %edx,0x8(%rsp)
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    0x2de2(%rip),%r15
lea    0x8(%rsp),%r12
mov    0x2dbe(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%edi
mov    (%r15),%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1060 <clock_nanosleep@plt>
cmp    (%rbx),%eax
je     1220 <msleep+0xe0>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, double }
%struct.TYPE_3__ = type { i32, i64 }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
@EINTR = global i32 0
define void @msleep(double %0) {
%2 = alloca double
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.TYPE_3__
store double %0, double* %2
%7 = load i32, i32* @CLOCK_MONOTONIC
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = icmp ne i32 %8, 0
%10 = xor i1 %9, true
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load double, double* %2
%14 = fcmp ogt double %13, 1.000000e+03
br i1 %14, label %15, label %32
15: ; preds = %1
%16 = load double, double* %2
%17 = fdiv double %16, 1.000000e+03
%18 = fptosi double %17 to i32
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = mul nsw i32 %19, 1000
%21 = sitofp i32 %20 to double
%22 = load double, double* %2
%23 = fsub double %22, %21
store double %23, double* %2
%24 = load double, double* %2
%25 = fcmp ogt double %24, 0.000000e+00
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32, i32* %5
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%30 = load i32, i32* %29
%31 = add nsw i32 %30, %28
store i32 %31, i32* %29
br label %32
32: ; preds = %15, %1
%33 = load double, double* %2
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%35 = load double, double* %34
%36 = call double @llvm.fmuladd.f64(double %33, double 1.000000e+06, double %35)
store double %36, double* %34
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%38 = load double, double* %37
%39 = fcmp oge double %38, 1.000000e+09
br i1 %39, label %40, label %61
40: ; preds = %32
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%42 = load double, double* %41
%43 = fptosi double %42 to i32
%44 = call { i32, i64 } @ldiv(i32 %43, i32 1000000000)
%45 = bitcast %struct.TYPE_3__* %6 to { i32, i64 }*
%46 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %45, i32 0, i32 0
%47 = extractvalue { i32, i64 } %44, 0
store i32 %47, i32* %46
%48 = getelementptr inbounds { i32, i64 }, { i32, i64 }* %45, i32 0, i32 1
%49 = extractvalue { i32, i64 } %44, 1
store i64 %49, i64* %48
%50 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 1
%51 = load i64, i64* %50
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%53 = load i32, i32* %52
%54 = sext i32 %53 to i64
%55 = add nsw i64 %54, %51
%56 = trunc i64 %55 to i32
store i32 %56, i32* %52
%57 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %6, i32 0, i32 0
%58 = load i32, i32* %57
%59 = sitofp i32 %58 to double
%60 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store double %59, double* %60
br label %61
61: ; preds = %40, %32
br label %62
62: ; preds = %66, %61
%63 = load i32, i32* @CLOCK_MONOTONIC
%64 = load i32, i32* @TIMER_ABSTIME
%65 = call i32 @clock_nanosleep(i32 %63, i32 %64, %struct.timespec* %3, i32* null)
store i32 %65, i32* %4
br label %66
66: ; preds = %62
%67 = load i32, i32* %4
%68 = load i32, i32* @EINTR
%69 = icmp eq i32 %67, %68
br i1 %69, label %62, label %70
70: ; preds = %66
%71 = load i32, i32* %4
%72 = icmp eq i32 %71, 0
%73 = zext i1 %72 to i32
%74 = call i32 @assert(i32 %73)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, double }
@CLOCK_MONOTONIC = global i32 0
@TIMER_ABSTIME = global i32 0
@EINTR = global i32 0
define void @msleep(double %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = icmp eq i32 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = fcmp ogt double %0, 1.000000e+03
br i1 %9, label %10, label %22
10: ; preds = %1
%11 = fdiv double %0, 1.000000e+03
%12 = fptosi double %11 to i32
%13 = mul nsw i32 %12, 1000
%14 = sitofp i32 %13 to double
%15 = fsub double %0, %14
%16 = fcmp ogt double %15, 0.000000e+00
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%20 = load i32, i32* %19
%21 = add nsw i32 %20, %12
store i32 %21, i32* %19
br label %22
22: ; preds = %10, %1
%23 = phi double [ %15, %10 ], [ %0, %1 ]
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%25 = load double, double* %24
%26 = call double @llvm.fmuladd.f64(double %23, double 1.000000e+06, double %25)
store double %26, double* %24
%27 = fcmp ult double %26, 1.000000e+09
br i1 %27, label %38, label %28
28: ; preds = %22
%29 = fptosi double %26 to i32
%30 = call { i32, i64 } @ldiv(i32 %29, i32 1000000000)
%31 = extractvalue { i32, i64 } %30, 0
%32 = extractvalue { i32, i64 } %30, 1
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%34 = load i32, i32* %33
%35 = trunc i64 %32 to i32
%36 = add i32 %34, %35
store i32 %36, i32* %33
%37 = sitofp i32 %31 to double
store double %37, double* %24
br label %38
38: ; preds = %28, %22
br label %39
39: ; preds = %38, %39
%40 = load i32, i32* @CLOCK_MONOTONIC
%41 = load i32, i32* @TIMER_ABSTIME
%42 = call i32 @clock_nanosleep(i32 %40, i32 %41, %struct.timespec* %2, i32* null)
%43 = load i32, i32* @EINTR
%44 = icmp eq i32 %42, %43
br i1 %44, label %39, label %45
45: ; preds = %39
%46 = icmp eq i32 %42, 0
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
ret void
}