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

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ errno;
int nanosleep(struct timespec *, struct timespec *);
void ssgl_msleep(int milliseconds) {
  int rc;
  struct timespec req, rem;
  req.tv_sec = milliseconds / 1000;
  req.tv_nsec = 1000000L * (milliseconds - (req.tv_sec * 1000));
  rem.tv_sec = 0;
  rem.tv_nsec = 0;
  do {
    rc = nanosleep(&req, &rem);
    if (rc == 0)
      break;
    if (rc < 0 && errno == -EINTR) {
      req = rem;
      rem.tv_sec = 0;
      rem.tv_nsec = 0;
      continue;
    }
  } while (1);
}


x86 assembly (clang, -O0):
<ssgl_msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x3e8,-0x18(%rbp),%ecx
sub    %ecx,%eax
cltq
imul   $0xf4240,%rax,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1170 <ssgl_msleep+0x60>
jmp    11ce <ssgl_msleep+0xbe>
cmpl   $0x0,-0x8(%rbp)
jge    11ba <ssgl_msleep+0xaa>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2e4c(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jne    11ba <ssgl_msleep+0xaa>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
jmp    11bf <ssgl_msleep+0xaf>
jmp    11bf <ssgl_msleep+0xaf>
mov    $0x1,%al
test   $0x1,%al
jne    1151 <ssgl_msleep+0x41>
jmp    11ce <ssgl_msleep+0xbe>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssgl_msleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x10(%rsp)
imul   $0xfffffc18,%ecx,%ecx
add    %ecx,%eax
cltq
imul   $0xf4240,%rax,%rax
mov    %rax,0x18(%rsp)
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11c7 <ssgl_msleep+0xb7>
mov    0x2e6c(%rip),%r15
mov    0x2e5d(%rip),%r12
lea    0x10(%rsp),%r14
mov    %rsp,%rbx
jmp    119f <ssgl_msleep+0x8f>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     11c7 <ssgl_msleep+0xb7>
test   %eax,%eax
jns    1190 <ssgl_msleep+0x80>
mov    (%r15),%eax
add    (%r12),%eax
jne    1190 <ssgl_msleep+0x80>
movups (%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
jmp    1190 <ssgl_msleep+0x80>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i32 0
@EINTR = global i32 0
define void @ssgl_msleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = sdiv i32 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* %2
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = mul nsw i32 %11, 1000
%13 = sub nsw i32 %9, %12
%14 = sext i32 %13 to i64
%15 = mul nsw i64 1000000, %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %15, i64* %16
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %18
br label %19
19: ; preds = %38, %1
%20 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
br label %39
24: ; preds = %19
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = load i32, i32* @EINTR
%30 = sub nsw i32 0, %29
%31 = icmp eq i32 %28, %30
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = bitcast %struct.timespec* %4 to i8*
%34 = bitcast %struct.timespec* %5 to i8*
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 0, i32* %35
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %36
br label %38
37: ; preds = %27, %24
br label %38
38: ; preds = %37, %32
br i1 true, label %19, label %39
39: ; preds = %38, %23
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i32 0
@EINTR = global i32 0
define void @ssgl_msleep(i32 %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 i32 %0, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %6, i32* %7
%8 = mul nsw i32 %6, -1000
%9 = add i32 %8, %0
%10 = sext i32 %9 to i64
%11 = mul nsw i64 %10, 1000000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %11, i64* %12
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 0, i64* %14
%15 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %1, %26
%18 = phi i32 [ %27, %26 ], [ %15, %1 ]
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @errno
%22 = load i32, i32* @EINTR
%23 = sub nsw i32 0, %22
%24 = icmp eq i32 %21, %23
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 0, i32* %13
store i64 0, i64* %14
br label %26
26: ; preds = %17, %20, %25
%27 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %17
29: ; preds = %26, %1
ret void
}