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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, struct timespec *);
void wait_ms(unsigned long a) {
  struct timespec dly;
  struct timespec rem;
  dly.tv_sec = a / 1000;
  dly.tv_nsec = ((long)a % 1000) * 1000000;
  ;
  if ((dly.tv_sec > 0) || ((dly.tv_sec == 0) && (dly.tv_nsec > 100000))) {
    clock_nanosleep(CLOCK_MONOTONIC, 0, &dly, &rem);
    ;
  }
  return;
}


x86 assembly (clang, -O0):
<wait_ms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0xf4240,%rdx,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x18(%rbp)
ja     116b <wait_ms+0x5b>
cmpq   $0x0,-0x18(%rbp)
jne    1183 <wait_ms+0x73>
cmpq   $0x186a0,-0x10(%rbp)
jle    1183 <wait_ms+0x73>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1030 <clock_nanosleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_ms>:
sub    $0x28,%rsp
mov    %rdi,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
mov    %rdx,0x8(%rsp)
mov    %rdi,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
imul   $0x3e8,%rdx,%rax
mov    %rdi,%rcx
sub    %rax,%rcx
imul   $0xf4240,%rcx,%rax
mov    %rax,0x10(%rsp)
cmp    $0x3e7,%rdi
ja     1174 <wait_ms+0x64>
cmp    $0x186a1,%rax
jge    1174 <wait_ms+0x64>
add    $0x28,%rsp
ret
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
xor    %esi,%esi
call   1030 <clock_nanosleep@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define void @wait_ms(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = udiv i64 %5, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %6, i64* %7
%8 = load i64, i64* %2
%9 = srem i64 %8, 1000
%10 = mul nsw i64 %9, 1000000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %10, i64* %11
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%13 = load i64, i64* %12
%14 = icmp ugt i64 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %1
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%17 = load i64, i64* %16
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %15
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%21 = load i64, i64* %20
%22 = icmp sgt i64 %21, 100000
br i1 %22, label %23, label %26
23: ; preds = %19, %1
%24 = load i32, i32* @CLOCK_MONOTONIC
%25 = call i32 @clock_nanosleep(i32 %24, i32 0, %struct.timespec* %3, %struct.timespec* %4)
br label %26
26: ; preds = %23, %19, %15
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
define void @wait_ms(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = udiv i64 %0, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %6, i64* %7
%8 = srem i64 %0, 1000
%9 = mul nsw i64 %8, 1000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %9, i64* %10
%11 = icmp ugt i64 %0, 999
%12 = icmp sgt i64 %9, 100000
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @CLOCK_MONOTONIC
%16 = call i32 @clock_nanosleep(i32 %15, i32 0, %struct.timespec* %2, %struct.timespec* %3)
br label %17
17: ; preds = %1, %14
ret void
}