#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 scalar_t__ uint64_t;

/* Variables and functions */
scalar_t__ getcurrenttime();
int /*<<< orphan*/ sleep(unsigned int);
int /*<<< orphan*/ usleep(unsigned int);
int msleep(unsigned int ms) {
  uint64_t stop_time = getcurrenttime() + ms;
  while (1) {
    uint64_t current_time = getcurrenttime();
    if (current_time >= stop_time) {
      return 0;
    }
    uint64_t delay_s = (stop_time - current_time) / 1000;
    if (delay_s > 0) {
      sleep((unsigned int)delay_s);
      continue;
    }
    uint64_t delay_us = (stop_time - current_time) * 1000;
    usleep((unsigned int)delay_us);
  }
}


x86 assembly (clang, -O0):
<msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getcurrenttime@plt>
mov    -0x4(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getcurrenttime@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jl     116d <msleep+0x3d>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jle    119e <msleep+0x6e>
mov    -0x20(%rbp),%rax
mov    %eax,%edi
call   1040 <sleep@plt>
jmp    114c <msleep+0x1c>
mov    -0x10(%rbp),%rax
sub    -0x18(%rbp),%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %eax,%edi
call   1050 <usleep@plt>
jmp    114c <msleep+0x1c>

x86 assembly (clang, -O2):
<msleep>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <getcurrenttime@plt>
mov    %ebx,%ebx
add    %rax,%rbx
xor    %eax,%eax
call   1030 <getcurrenttime@plt>
cmp    %rax,%rbx
jle    119e <msleep+0x6e>
movabs $0x20c49ba5e353f7cf,%r14
9b c4 20
jmp    1177 <msleep+0x47>
nopw   0x0(%rax,%rax,1)
imul   $0x3e8,%ecx,%edi
call   1050 <usleep@plt>
xor    %eax,%eax
call   1030 <getcurrenttime@plt>
cmp    %rax,%rbx
jle    119e <msleep+0x6e>
mov    %rbx,%rcx
sub    %rax,%rcx
cmp    $0x3e8,%rcx
jl     1160 <msleep+0x30>
shr    $0x3,%rcx
mov    %rcx,%rax
mul    %r14
mov    %rdx,%rdi
shr    $0x4,%rdi
call   1040 <sleep@plt>
jmp    116b <msleep+0x3b>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @msleep(i32 %0) {
%2 = tail call i64 (...) @getcurrenttime()
%3 = zext i32 %0 to i64
%4 = add nsw i64 %2, %3
%5 = tail call i64 (...) @getcurrenttime()
%6 = icmp sgt i64 %4, %5
br i1 %6, label %7, label %22
7: ; preds = %1, %19
%8 = phi i64 [ %20, %19 ], [ %5, %1 ]
%9 = sub nsw i64 %4, %8
%10 = icmp sgt i64 %9, 999
br i1 %10, label %11, label %15
11: ; preds = %7
%12 = udiv i64 %9, 1000
%13 = trunc i64 %12 to i32
%14 = tail call i32 @sleep(i32 %13)
br label %19
15: ; preds = %7
%16 = trunc i64 %9 to i32
%17 = mul i32 %16, 1000
%18 = tail call i32 @usleep(i32 %17)
br label %19
19: ; preds = %11, %15
%20 = tail call i64 (...) @getcurrenttime()
%21 = icmp sgt i64 %4, %20
br i1 %21, label %7, label %22
22: ; preds = %19, %1
ret i32 0
}