#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 */

/* Variables and functions */
int /*<<< orphan*/ sleep(long long);
long long sysTimeMillis();
int /*<<< orphan*/ usleep(long long);
void osMilliSleep(long long millis) {
  if (millis <= 0) {
    return;
  }
  long long elapsed = sysTimeMillis();
  long long seconds = millis / 1000;
  if (seconds > 0) {
    sleep(seconds);
  }
  elapsed = sysTimeMillis() - elapsed;
  if (elapsed < millis) {
    millis = millis - elapsed;
    long long micro = millis * 1000;
    if (micro > 999999) {
      micro = 999999;
    }
    usleep(micro);
  }
}


x86 assembly (clang, -O0):
<osMilliSleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jg     114c <osMilliSleep+0x1c>
jmp    11d1 <osMilliSleep+0xa1>
mov    $0x0,%al
call   1030 <sysTimeMillis@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    117d <osMilliSleep+0x4d>
mov    -0x18(%rbp),%rdi
call   1040 <sleep@plt>
mov    $0x0,%al
call   1030 <sysTimeMillis@plt>
sub    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jge    11d1 <osMilliSleep+0xa1>
mov    -0x8(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
imul   $0x3e8,-0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0xf423f,-0x20(%rbp)
jle    11c8 <osMilliSleep+0x98>
movq   $0xf423f,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <usleep@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<osMilliSleep>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
jle    11a3 <osMilliSleep+0x73>
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <sysTimeMillis@plt>
mov    %rax,%r14
cmp    $0x3e8,%rbx
jb     116f <osMilliSleep+0x3f>
mov    %rbx,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
mov    %rdx,%rdi
shr    $0x4,%rdi
call   1040 <sleep@plt>
xor    %eax,%eax
call   1030 <sysTimeMillis@plt>
sub    %r14,%rax
cmp    %rbx,%rax
jge    11a3 <osMilliSleep+0x73>
sub    %rax,%rbx
imul   $0x3e8,%rbx,%rax
cmp    $0xf423f,%rax
mov    $0xf423f,%edi
cmovl  %rax,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <usleep@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @osMilliSleep(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = icmp sle i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
br label %37
9: ; preds = %1
%10 = call i64 (...) @sysTimeMillis()
store i64 %10, i64* %3
%11 = load i64, i64* %2
%12 = sdiv i64 %11, 1000
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i64, i64* %4
%17 = call i32 @sleep(i64 %16)
br label %18
18: ; preds = %15, %9
%19 = call i64 (...) @sysTimeMillis()
%20 = load i64, i64* %3
%21 = sub nsw i64 %19, %20
store i64 %21, i64* %3
%22 = load i64, i64* %3
%23 = load i64, i64* %2
%24 = icmp slt i64 %22, %23
br i1 %24, label %25, label %37
25: ; preds = %18
%26 = load i64, i64* %2
%27 = load i64, i64* %3
%28 = sub nsw i64 %26, %27
store i64 %28, i64* %2
%29 = load i64, i64* %2
%30 = mul nsw i64 %29, 1000
store i64 %30, i64* %5
%31 = load i64, i64* %5
%32 = icmp sgt i64 %31, 999999
br i1 %32, label %33, label %34
33: ; preds = %25
store i64 999999, i64* %5
br label %34
34: ; preds = %33, %25
%35 = load i64, i64* %5
%36 = call i32 @usleep(i64 %35)
br label %37
37: ; preds = %8, %34, %18
ret void
}

LLVM IR (-O2):
define void @osMilliSleep(i64 %0) {
%2 = icmp slt i64 %0, 1
br i1 %2, label %19, label %3
3: ; preds = %1
%4 = tail call i64 (...) @sysTimeMillis()
%5 = icmp ugt i64 %0, 999
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = udiv i64 %0, 1000
%8 = tail call i32 @sleep(i64 %7)
br label %9
9: ; preds = %6, %3
%10 = tail call i64 (...) @sysTimeMillis()
%11 = sub nsw i64 %10, %4
%12 = icmp slt i64 %11, %0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = sub nsw i64 %0, %11
%15 = mul nsw i64 %14, 1000
%16 = icmp slt i64 %15, 999999
%17 = select i1 %16, i64 %15, i64 999999
%18 = tail call i32 @usleep(i64 %17)
br label %19
19: ; preds = %9, %13, %1
ret void
}