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

/* Variables and functions */
long long flu_getms();
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
long long flu_do_msleep(long long milliseconds) {
  long long start = flu_getms();
  struct timespec treq;
  treq.tv_sec = milliseconds / 1000;
  treq.tv_nsec = (milliseconds * 1000000) % 1000000000;
  struct timespec trem;
  trem.tv_sec = 0;
  trem.tv_nsec = 0;
  while (1) {
    nanosleep(&treq, &trem);
    if (trem.tv_sec == 0 && trem.tv_nsec == 0)
      break;
    treq.tv_sec = trem.tv_sec;
    treq.tv_nsec = trem.tv_nsec;
    trem.tv_sec = 0;
    trem.tv_nsec = 0;
  }
  return flu_getms() - start;
}


x86 assembly (clang, -O0):
<flu_do_msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <flu_getms@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
imul   $0xf4240,-0x8(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <nanosleep@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1197 <flu_do_msleep+0x77>
cmpq   $0x0,-0x28(%rbp)
jne    1197 <flu_do_msleep+0x77>
jmp    11bc <flu_do_msleep+0x9c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
jmp    116f <flu_do_msleep+0x4f>
mov    $0x0,%al
call   1030 <flu_getms@plt>
sub    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flu_do_msleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <flu_getms@plt>
mov    %rax,%r14
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mov    %rbx,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,0x10(%rsp)
imul   $0xf4240,%rbx,%rcx
movabs $0x112e0be826d694b3,%rdx
0b 2e 11
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rcx
mov    %rcx,0x18(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
call   1040 <nanosleep@plt>
mov    (%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
je     11ed <flu_do_msleep+0xcd>
lea    0x10(%rsp),%r15
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rax,0x10(%rsp)
mov    %rcx,0x18(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <nanosleep@plt>
mov    (%rsp),%rax
mov    0x8(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
jne    11c0 <flu_do_msleep+0xa0>
xor    %eax,%eax
call   1030 <flu_getms@plt>
sub    %r14,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
define i64 @flu_do_msleep(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i64 %0, i64* %2
%6 = call i64 (...) @flu_getms()
store i64 %6, i64* %3
%7 = load i64, i64* %2
%8 = sdiv i64 %7, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %8, i64* %9
%10 = load i64, i64* %2
%11 = mul nsw i64 %10, 1000000
%12 = srem i64 %11, 1000000000
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %12, i64* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i64 0, i64* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %15
br label %16
16: ; preds = %1, %26
%17 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%19 = load i64, i64* %18
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %35
26: ; preds = %21, %16
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%28 = load i64, i64* %27
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %28, i64* %29
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%31 = load i64, i64* %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %31, i64* %32
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i64 0, i64* %33
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 0, i64* %34
br label %16
35: ; preds = %25
%36 = call i64 (...) @flu_getms()
%37 = load i64, i64* %3
%38 = sub nsw i64 %36, %37
ret i64 %38
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
define i64 @flu_do_msleep(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = tail call i64 (...) @flu_getms()
%5 = bitcast %struct.timespec* %2 to i8*
%6 = sdiv i64 %0, 1000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %6, i64* %7
%8 = mul nsw i64 %0, 1000000
%9 = srem i64 %8, 1000000000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %9, i64* %10
%11 = bitcast %struct.timespec* %3 to i8*
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%14 = bitcast %struct.timespec* %3 to i8*
%15 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%16 = load i64, i64* %12
%17 = icmp eq i64 %16, 0
%18 = load i64, i64* %13
%19 = icmp eq i64 %18, 0
%20 = select i1 %17, i1 %19, i1 false
br i1 %20, label %32, label %21
21: ; preds = %1
%22 = bitcast %struct.timespec* %3 to i8*
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ %29, %23 ], [ %18, %21 ]
%25 = phi i64 [ %27, %23 ], [ %16, %21 ]
store i64 %25, i64* %7
store i64 %24, i64* %10
%26 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%27 = load i64, i64* %12
%28 = icmp eq i64 %27, 0
%29 = load i64, i64* %13
%30 = icmp eq i64 %29, 0
%31 = select i1 %28, i1 %30, i1 false
br i1 %31, label %32, label %23
32: ; preds = %23, %1
%33 = call i64 (...) @flu_getms()
%34 = sub nsw i64 %33, %4
ret i64 %34
}