#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 ulong;
struct timespec {
  int tv_sec;
  int tv_nsec;
};

/* Variables and functions */
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void sleep_msec(ulong msec) {
  struct timespec req;
  struct timespec rem;
  req.tv_sec = msec / 1000;
  req.tv_nsec = (msec * 1000000) % 1000000000;
  while (nanosleep(&req, &rem)) {
    req = rem;
  }
}


x86 assembly (clang, -O0):
<sleep_msec>:
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)
imul   $0xf4240,-0x4(%rbp),%eax
mov    $0x3b9aca00,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
je     115f <sleep_msec+0x4f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    113b <sleep_msec+0x2b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_msec>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movslq %edi,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0xf4240,%eax,%eax
cltq
imul   $0x44b82fa1,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x3c,%rcx
add    %edx,%ecx
imul   $0x3b9aca00,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     119a <sleep_msec+0x8a>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
jne    1180 <sleep_msec+0x70>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
define void @sleep_msec(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = sdiv i32 %5, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %6, i32* %7
%8 = load i32, i32* %2
%9 = mul nsw i32 %8, 1000000
%10 = srem i32 %9, 1000000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %10, i32* %11
br label %12
12: ; preds = %15, %1
%13 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = bitcast %struct.timespec* %3 to i8*
%17 = bitcast %struct.timespec* %4 to i8*
br label %12
18: ; preds = %12
ret void
}

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