#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_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
void msleep(unsigned int msecs) {
  int err;
  struct timespec time;
  time.tv_sec = 0;
  while (msecs >= 1000) {
    time.tv_sec++;
    msecs -= 1000;
  }
  time.tv_nsec = msecs;
  time.tv_nsec *= 1000000;
  do {
    err = nanosleep(&time, &time);
  } while (err && errno == EINTR);
  if (err) {
    fprintf(stderr, "nanosleep() failed with: %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jb     116c <msleep+0x3c>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x3e8,%eax
mov    %eax,-0x4(%rbp)
jmp    1143 <msleep+0x13>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
imul   $0xf4240,-0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x8(%rbp)
mov    %al,-0x19(%rbp)
je     11b4 <msleep+0x84>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    117c <msleep+0x4c>
cmpl   $0x0,-0x8(%rbp)
je     11f6 <msleep+0xc6>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe0f(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x3e8,%edi
jb     115f <msleep+0x2f>
add    $0xfffffc18,%edi
imul   $0x10624dd3,%rdi,%rax
shr    $0x26,%rax
imul   $0x3e8,%eax,%ecx
sub    %ecx,%edi
add    $0x1,%eax
jmp    1161 <msleep+0x31>
xor    %eax,%eax
mov    %rax,0x8(%rsp)
imul   $0xf4240,%edi,%eax
mov    %eax,(%rsp)
mov    %rsp,%rbx
mov    0x2e5f(%rip),%r14
mov    0x2e50(%rip),%r15
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
mov    (%r14),%rdi
test   %eax,%eax
je     1197 <msleep+0x67>
cmp    (%r15),%rdi
je     1180 <msleep+0x50>
test   %eax,%eax
je     11ba <msleep+0x8a>
mov    0x2e3e(%rip),%rax
mov    (%rax),%ebx
call   1050 <strerror@plt>
lea    0xe50(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [29 x i8] c"nanosleep() failed with: %s\0A\00"
define void @msleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %5
br label %6
6: ; preds = %9, %1
%7 = load i32, i32* %2
%8 = icmp uge i32 %7, 1000
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%11 = load i64, i64* %10
%12 = add nsw i64 %11, 1
store i64 %12, i64* %10
%13 = load i32, i32* %2
%14 = sub i32 %13, 1000
store i32 %14, i32* %2
br label %6
15: ; preds = %6
%16 = load i32, i32* %2
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = mul i32 %19, 1000000
store i32 %20, i32* %18
br label %21
21: ; preds = %30, %15
%22 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
store i32 %22, i32* %3
br label %23
23: ; preds = %21
%24 = load i32, i32* %3
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br label %30
30: ; preds = %26, %23
%31 = phi i1 [ false, %23 ], [ %29, %26 ]
br i1 %31, label %21, label %32
32: ; preds = %30
%33 = load i32, i32* %3
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %35, %32
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [29 x i8] c"nanosleep() failed with: %s\0A\00"
define void @msleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%5 = icmp ugt i32 %0, 999
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = add i32 %0, -1000
%8 = udiv i32 %7, 1000
%9 = urem i32 %7, 1000
%10 = add nuw nsw i32 %8, 1
%11 = zext i32 %10 to i64
br label %12
12: ; preds = %6, %1
%13 = phi i64 [ 0, %1 ], [ %11, %6 ]
%14 = phi i32 [ %0, %1 ], [ %9, %6 ]
store i64 %13, i64* %4
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%16 = mul nuw nsw i32 %14, 1000000
store i32 %16, i32* %15
br label %17
17: ; preds = %17, %12
%18 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%19 = icmp ne i32 %18, 0
%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
br i1 %19, label %25, label %29
25: ; preds = %24
%26 = load i32, i32* @stderr
%27 = call i8* @strerror(i64 %20)
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %24
ret void
}