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

/* Variables and functions */
scalar_t__ EFAULT;
scalar_t__ EINVAL;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
int msleep(int msecs) {
  int rc;
  struct timespec req, rem;
  rem.tv_sec = 0;
  rem.tv_nsec = 0;
  req.tv_sec = msecs / 1000;
  req.tv_nsec = (msecs % 1000) * 1000000;
  while (1) {
    rc = nanosleep(&req, &rem);
    if (rc == 0)
      break;
    if ((errno == EINVAL) || (errno == EFAULT))
      break;
    req.tv_sec = rem.tv_sec;
    req.tv_nsec = rem.tv_nsec;
    rem.tv_sec = 0;
    rem.tv_nsec = 0;
  }
  return rc;
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%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
imul   $0xf4240,%edx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    116a <msleep+0x5a>
jmp    11c2 <msleep+0xb2>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
je     119e <msleep+0x8e>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
jne    11a3 <msleep+0x93>
jmp    11c2 <msleep+0xb2>
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
jmp    114b <msleep+0x3b>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11c1 <msleep+0xb1>
mov    0x2e6e(%rip),%r15
mov    (%r15),%rcx
mov    0x2e5c(%rip),%r12
cmp    (%r12),%rcx
je     11c3 <msleep+0xb3>
mov    0x2e77(%rip),%r13
cmp    0x0(%r13),%rcx
je     11c3 <msleep+0xb3>
lea    0x8(%rsp),%r14
mov    %rsp,%rbx
nop
mov    (%rsp),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11c1 <msleep+0xb1>
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     11c3 <msleep+0xb3>
cmp    0x0(%r13),%rcx
jne    1190 <msleep+0x80>
jmp    11c3 <msleep+0xb3>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINVAL = global i64 0
@EFAULT = 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* %2
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 0, i32* %7
%8 = load i32, i32* %2
%9 = sdiv i32 %8, 1000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* %2
%12 = srem i32 %11, 1000
%13 = mul nsw i32 %12, 1000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %13, i32* %14
br label %15
15: ; preds = %1, %29
%16 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
br label %38
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINVAL
%23 = icmp eq i64 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EFAULT
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24, %20
br label %38
29: ; preds = %24
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 0, i32* %36
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 0, i32* %37
br label %15
38: ; preds = %28, %19
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@errno = global i64 0
@EINVAL = global i64 0
@EFAULT = 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 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 0, i32* %7
%8 = sdiv i32 %0, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %8, i32* %9
%10 = srem i32 %0, 1000
%11 = mul nsw i32 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %34, label %15
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINVAL
%18 = icmp eq i64 %16, %17
%19 = load i64, i64* @EFAULT
%20 = icmp eq i64 %16, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %34, label %29
22: ; preds = %29
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINVAL
%25 = icmp eq i64 %23, %24
%26 = load i64, i64* @EFAULT
%27 = icmp eq i64 %23, %26
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %34, label %29
29: ; preds = %15, %22
%30 = load i32, i32* %6
store i32 %30, i32* %9
%31 = load i32, i32* %7
store i32 %31, i32* %12
store i32 0, i32* %6
store i32 0, i32* %7
%32 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %22
34: ; preds = %22, %29, %15, %1
%35 = phi i32 [ 0, %1 ], [ %13, %15 ], [ 0, %29 ], [ %32, %22 ]
ret i32 %35
}