#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_sec;
  unsigned int tv_nsec;
};

/* Variables and functions */
scalar_t__ nanosleep(struct timespec *, struct timespec *);
void sleep_ms(unsigned int ms) {
  struct timespec req, rem;
  req.tv_sec = ms / 1000;
  ms -= req.tv_sec * 1000;
  req.tv_nsec = ms * 1000000;
  while (nanosleep(&req, &rem))
    req = rem;
}


x86 assembly (clang, -O0):
<sleep_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x10(%rbp)
imul   $0x3e8,-0x10(%rbp),%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
imul   $0xf4240,-0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
je     1167 <sleep_ms+0x57>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1143 <sleep_ms+0x33>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_ms>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
mov    %eax,0x8(%rsp)
imul   $0xfffffc18,%eax,%eax
add    %edi,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     117a <sleep_ms+0x6a>
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    1160 <sleep_ms+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
define void @sleep_ms(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 = udiv i32 %0, 1000
%9 = bitcast i64* %2 to i32*
store i32 %8, i32* %9
%10 = mul i32 %8, -1000
%11 = add i32 %10, %0
%12 = mul i32 %11, 1000000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %1, %16
%17 = load i64, i64* %4
store i64 %17, i64* %2
%18 = call i64 @nanosleep(%struct.timespec* %3, %struct.timespec* %5)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %16
20: ; preds = %16, %1
ret void
}