#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;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
unsigned int msleep(unsigned int ms) {
  struct timespec req;
  struct timespec rem;
  req.tv_sec = ms / 1000;
  req.tv_nsec = (ms % 1000) * 1000000L;
  if (nanosleep(&req, &rem) < 0) {
    if (errno == EINTR) {
      return (rem.tv_sec * 1000) + (rem.tv_nsec / 1000000L);
    }
    return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %edx,%eax
imul   $0xf4240,%rax,%rax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
jge    11ab <msleep+0x9b>
mov    0x2e80(%rip),%rax
mov    (%rax),%rax
mov    0x2e6e(%rip),%rcx
cmp    (%rcx),%rax
jne    119f <msleep+0x8f>
imul   $0x3e8,-0x18(%rbp),%eax
mov    %eax,%eax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    11b2 <msleep+0xa2>
movl   $0x0,-0x4(%rbp)
jmp    11b2 <msleep+0xa2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x10(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%edi
imul   $0xf4240,%edi,%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1030 <nanosleep@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1155 <msleep+0x45>
add    $0x18,%rsp
ret
mov    0x2e84(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e72(%rip),%rdx
cmp    (%rdx),%rcx
jne    1150 <msleep+0x40>
imul   $0x3e8,0x8(%rsp),%ecx
mov    0xc(%rsp),%eax
imul   $0x431bde83,%rax,%rax
shr    $0x32,%rax
add    %ecx,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @msleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = udiv i32 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* %3
%10 = urem i32 %9, 1000
%11 = zext i32 %10 to i64
%12 = mul nsw i64 %11, 1000000
%13 = trunc i64 %12 to i32
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %13, i32* %14
%15 = call i64 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %33
17: ; preds = %1
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%23 = load i32, i32* %22
%24 = mul i32 %23, 1000
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%27 = load i32, i32* %26
%28 = zext i32 %27 to i64
%29 = sdiv i64 %28, 1000000
%30 = add nsw i64 %25, %29
%31 = trunc i64 %30 to i32
store i32 %31, i32* %2
br label %34
32: ; preds = %17
store i32 0, i32* %2
br label %34
33: ; preds = %1
store i32 0, i32* %2
br label %34
34: ; preds = %33, %32, %21
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @msleep(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* %2, 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* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %25
17: ; preds = %13
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = mul i32 %19, 1000
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = udiv i32 %22, 1000000
%24 = add i32 %23, %20
br label %25
25: ; preds = %1, %13, %17
%26 = phi i32 [ %24, %17 ], [ 0, %13 ], [ 0, %1 ]
ret i32 %26
}