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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
int millisleep(unsigned long ms_pause) {
  struct timespec time_to_wait;
  int ret_err;
  time_to_wait.tv_sec = ms_pause / 1000;
  time_to_wait.tv_nsec = (ms_pause % 1000) * 1000000;
  do {
    ret_err = nanosleep(&time_to_wait, &time_to_wait);
  } while (ret_err == -1 && errno == EINTR &&
           (time_to_wait.tv_sec > 0 || time_to_wait.tv_nsec >= 1000000));
  if (ret_err == -1 && errno == EINTR)
    ret_err = 0;
  return (ret_err);
}


x86 assembly (clang, -O0):
<millisleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
imul   $0xf4240,%rdx,%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    11a8 <millisleep+0x98>
mov    0x2e74(%rip),%rax
mov    (%rax),%rcx
xor    %eax,%eax
mov    0x2e60(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1d(%rbp)
jne    11a8 <millisleep+0x98>
mov    $0x1,%al
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x1e(%rbp)
ja     11a2 <millisleep+0x92>
cmpq   $0xf4240,-0x10(%rbp)
setae  %al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1147 <millisleep+0x37>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11de <millisleep+0xce>
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
jne    11de <millisleep+0xce>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<millisleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
mov    %rdx,(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rdi
imul   $0xf4240,%rdi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rbx
mov    0x2e8b(%rip),%r14
mov    0x2e7c(%rip),%r15
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1197 <millisleep+0x87>
mov    (%r14),%rcx
mov    (%r15),%rdx
cmp    %rdx,%rcx
jne    118d <millisleep+0x7d>
cmpq   $0x0,(%rsp)
jne    1160 <millisleep+0x50>
cmpq   $0xf423f,0x8(%rsp)
ja     1160 <millisleep+0x50>
xor    %eax,%eax
cmp    %rdx,%rcx
setne  %al
neg    %eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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