#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 */
int EINTR;
int errno;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int msleep(unsigned int millis) {
  struct timespec ts;
  ts.tv_sec = (millis / 1000);
  ts.tv_nsec = (millis % 1000) * 1000000;
  while (nanosleep(&ts, &ts) != 0) {
    int errcode = errno;
    if (errcode != EINTR) {
      return errcode;
    }
  }
  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
imul   $0xf4240,%edx,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
je     1183 <msleep+0x73>
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%eax
je     117e <msleep+0x6e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    118a <msleep+0x7a>
jmp    113f <msleep+0x2f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x8(%rsp)
imul   $0x3e8,%eax,%eax
sub    %eax,%edi
imul   $0xf4240,%edi,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rbx
mov    0x2e98(%rip),%r14
mov    0x2e89(%rip),%r15
nop
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     116a <msleep+0x5a>
mov    (%r14),%eax
cmp    (%r15),%eax
je     1150 <msleep+0x40>
jmp    116c <msleep+0x5c>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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