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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void mm_sleep(unsigned int ms) {
  int err;
  struct timespec req, rem;
  rem.tv_sec = ms / 1000;
  rem.tv_nsec = (ms % 1000) * 1000000;
  do {
    err = 0;
    req.tv_sec = rem.tv_sec;
    req.tv_nsec = rem.tv_nsec;
    err = nanosleep(&req, &rem);
  } while (err && (errno == EINTR));
}


x86 assembly (clang, -O0):
<mm_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x19(%rbp)
je     118b <mm_sleep+0x7b>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    113f <mm_sleep+0x2f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mm_sleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x8(%rsp)
imul   $0x3e8,%eax,%ecx
sub    %ecx,%edi
imul   $0xf4240,%edi,%ecx
mov    %ecx,0xc(%rsp)
mov    %eax,0x10(%rsp)
mov    %ecx,0x14(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11a2 <mm_sleep+0x92>
mov    0x2e80(%rip),%r15
mov    0x2e71(%rip),%r12
mov    (%r12),%rax
cmp    %rax,(%r15)
jne    11a2 <mm_sleep+0x92>
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11a2 <mm_sleep+0x92>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1180 <mm_sleep+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @mm_sleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = udiv i32 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* %2
%10 = urem i32 %9, 1000
%11 = mul i32 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 %11, i32* %12
br label %13
13: ; preds = %28, %1
store i32 0, i32* %3
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
store i32 %20, i32* %3
br label %21
21: ; preds = %13
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br label %28
28: ; preds = %24, %21
%29 = phi i1 [ false, %21 ], [ %27, %24 ]
br i1 %29, label %13, label %30
30: ; preds = %28
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @mm_sleep(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 = udiv i32 %0, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 %6, i32* %7
%8 = urem i32 %0, 1000
%9 = mul nuw nsw i32 %8, 1000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %6, i32* %11
store i32 %9, i32* %12
%13 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%14 = icmp ne i32 %13, 0
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %19, label %28
19: ; preds = %1, %19
%20 = load i32, i32* %7
%21 = load i32, i32* %10
store i32 %20, i32* %11
store i32 %21, i32* %12
%22 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%23 = icmp ne i32 %22, 0
%24 = load i64, i64* @errno
%25 = load i64, i64* @EINTR
%26 = icmp eq i64 %24, %25
%27 = select i1 %23, i1 %26, i1 false
br i1 %27, label %19, label %28
28: ; preds = %19, %1
ret void
}