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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
int msleep(long millisecs) {
  struct timespec req, rem;
  req.tv_sec = millisecs / 1000;
  req.tv_nsec = (millisecs % 1000) * 1000 * 1000;
  int ret;
  while ((ret = nanosleep(&req, &rem)) == -1 && errno == EINTR)
    req = rem;
  return ret;
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x2d(%rbp)
jne    1188 <msleep+0x78>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    0x2e59(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1198 <msleep+0x88>
jmp    11ad <msleep+0x9d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    114e <msleep+0x3e>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %rdi,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rdi
imul   $0xf4240,%rdi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11b2 <msleep+0xa2>
mov    0x2e74(%rip),%r15
mov    0x2e65(%rip),%r12
mov    (%r12),%rcx
cmp    %rcx,(%r15)
jne    11b2 <msleep+0xa2>
mov    %rsp,%r14
lea    0x18(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11b2 <msleep+0xa2>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1190 <msleep+0x80>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @msleep(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = sdiv i64 %0, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %6, i64* %7
%8 = srem i64 %0, 1000
%9 = mul nsw i64 %8, 1000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %9, i64* %10
%11 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%12 = icmp eq i32 %11, -1
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = select i1 %12, i1 %15, i1 false
br i1 %16, label %17, label %24
17: ; preds = %1, %17
%18 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%19 = icmp eq i32 %18, -1
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %17, label %24
24: ; preds = %17, %1
%25 = phi i32 [ %11, %1 ], [ %18, %17 ]
ret i32 %25
}