#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 int tv_nsec;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
void sleep_ms(unsigned int milliseconds) {
  struct timespec req = {0};
  time_t seconds = (int)(milliseconds / 1000);
  milliseconds = milliseconds - ((unsigned int)seconds * 1000);
  req.tv_sec = seconds;
  req.tv_nsec = milliseconds * 1000000L;
  while (nanosleep(&req, &req) == -1)
    ;
}


x86 assembly (clang, -O0):
<sleep_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %ecx
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
imul   $0x3e8,-0x14(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%eax
imul   $0xf4240,%rax,%rax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    1186 <sleep_ms+0x66>
jmp    116c <sleep_ms+0x4c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_ms>:
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x8(%rsp)
mov    %edi,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0xfffffc18,%eax,%ecx
add    %edi,%ecx
mov    %eax,(%rsp)
imul   $0xf4240,%ecx,%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     1150 <sleep_ms+0x40>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32, i32 }
define void @sleep_ms(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca i32
store i32 %0, i32* %2
%5 = bitcast %struct.timespec* %3 to i8*
%6 = load i32, i32* %2
%7 = udiv i32 %6, 1000
store i32 %7, i32* %4
%8 = load i32, i32* %2
%9 = load i32, i32* %4
%10 = mul i32 %9, 1000
%11 = sub i32 %8, %10
store i32 %11, i32* %2
%12 = load i32, i32* %4
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* %2
%15 = zext i32 %14 to i64
%16 = mul nsw i64 %15, 1000000
%17 = trunc i64 %16 to i32
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i32 %17, i32* %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 void
}

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