#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 */
typedef int time_t;
struct timespec {
  int tv_sec;
  unsigned long tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
int msleep(unsigned long millisec) {
  struct timespec req = {0};
  time_t sec = (int)(millisec / 1000);
  millisec = millisec - (sec * 1000);
  req.tv_sec = sec;
  req.tv_nsec = millisec * 1000000L;
  while (nanosleep(&req, &req) == -1)
    continue;
  return 1;
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
imul   $0x3e8,-0x24(%rbp),%ecx
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
imul   $0xf4240,-0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    118e <msleep+0x6e>
jmp    1174 <msleep+0x54>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msleep>:
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
movabs $0x3e800000000,%rax
imul   %rdx,%rax
sar    $0x20,%rax
sub    %rax,%rdi
mov    %edx,(%rsp)
imul   $0xf4240,%rdi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1170 <msleep+0x60>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64, i32 }
define i32 @msleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca i32
store i64 %0, i64* %2
%5 = bitcast %struct.timespec* %3 to i8*
%6 = load i64, i64* %2
%7 = udiv i64 %6, 1000
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = load i64, i64* %2
%10 = load i32, i32* %4
%11 = mul nsw i32 %10, 1000
%12 = sext i32 %11 to i64
%13 = sub i64 %9, %12
store i64 %13, i64* %2
%14 = load i32, i32* %4
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i64, i64* %2
%17 = mul i64 %16, 1000000
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %17, i64* %18
br label %19
19: ; preds = %22, %1
%20 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %19
br label %19
23: ; preds = %19
ret i32 1
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64, i32 }
define i32 @msleep(i64 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = udiv i64 %0, 1000
%5 = trunc i64 %4 to i32
%6 = mul i64 %4, 4294967296000
%7 = ashr exact i64 %6, 32
%8 = sub i64 %0, %7
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %5, i32* %9
%10 = mul i64 %8, 1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %10, i64* %11
br label %12
12: ; preds = %12, %1
%13 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %12, label %15
15: ; preds = %12
ret i32 1
}