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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/ *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void msleep(int ms) {
  struct timeval tv;
  int v;
  do {
    tv.tv_sec = ms / 1000;
    tv.tv_usec = (ms % 1000) * 1000;
    v = select(0, NULL, NULL, NULL, &tv);
  } while ((v < 0) && (errno == EINTR));
  if (v < 0)
    error(NULL);
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%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   $0x3e8,%edx,%eax
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <select@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jge    118e <msleep+0x6e>
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    112b <msleep+0xb>
cmpl   $0x0,-0x14(%rbp)
jge    11ac <msleep+0x8c>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <error@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rbx
add    %ecx,%ebx
imul   $0x3e8,%ebx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%ebp
lea    0x8(%rsp),%r14
mov    0x2e83(%rip),%r15
mov    0x2e74(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,0x8(%rsp)
mov    %ebp,0xc(%rsp)
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1030 <select@plt>
test   %eax,%eax
jns    1195 <msleep+0x75>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1170 <msleep+0x50>
test   %eax,%eax
jns    11a0 <msleep+0x80>
xor    %edi,%edi
call   1040 <error@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@errno = global i64 0
@EINTR = global i64 0
define void @msleep(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %21, %1
%6 = load i32, i32* %2
%7 = sdiv i32 %6, 1000
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* %2
%10 = srem i32 %9, 1000
%11 = mul nsw i32 %10, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %3)
store i32 %13, i32* %4
br label %14
14: ; preds = %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
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 %5, label %23
23: ; preds = %21
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @error(i32* null)
br label %28
28: ; preds = %26, %23
ret void
}

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