#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 /*<<< orphan*/ ts;
struct timespec {
  int tv_sec;
  int tv_nsec;
};
typedef int /*<<< orphan*/ rts;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ abort();
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    struct timespec *);
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
void mt_msleep(int milli) {
  int sec, res;
  struct timespec ts, rts;
  memset(&ts, 0, sizeof(ts));
  sec = milli / 1000;
  milli -= (sec * 1000);
  ts.tv_sec = sec;
  ts.tv_nsec = milli * 1000;
  memset(&rts, 0, sizeof(rts));
  while (1) {
    res = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &rts);
    if (res)
      abort();
    if ((rts.tv_sec == 0) && (rts.tv_nsec == 0))
      break;
    ts.tv_sec = rts.tv_sec;
    ts.tv_nsec = rts.tv_nsec;
  }
}


x86 assembly (clang, -O0):
<mt_msleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
imul   $0x3e8,-0x8(%rbp),%ecx
mov    -0x4(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
imul   $0x3e8,-0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1050 <clock_nanosleep@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11b4 <mt_msleep+0x84>
mov    $0x0,%al
call   1030 <abort@plt>
cmpl   $0x0,-0x20(%rbp)
jne    11cd <mt_msleep+0x9d>
cmpl   $0x0,-0x1c(%rbp)
jne    11cd <mt_msleep+0x9d>
jmp    11de <mt_msleep+0xae>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1188 <mt_msleep+0x58>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mt_msleep>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movslq %ebp,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
imul   $0xfffffc18,%ecx,%edx
add    %edx,%eax
mov    %ecx,(%rsp)
imul   $0x3e8,%eax,%eax
mov    %eax,0x4(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e55(%rip),%rbp
mov    0x0(%rbp),%edi
xor    %esi,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <clock_nanosleep@plt>
test   %eax,%eax
jne    11d9 <mt_msleep+0xa9>
mov    %rsp,%r14
lea    0x8(%rsp),%rbx
nop
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
mov    %eax,%edx
or     %ecx,%edx
je     11e0 <mt_msleep+0xb0>
mov    %eax,(%rsp)
mov    %ecx,0x4(%rsp)
mov    0x0(%rbp),%edi
xor    %esi,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <clock_nanosleep@plt>
test   %eax,%eax
je     11b0 <mt_msleep+0x80>
xor    %eax,%eax
call   1030 <abort@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define void @mt_msleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
store i32 %0, i32* %2
%7 = call i32 @memset(%struct.timespec* %5, i32 0, i32 8)
%8 = load i32, i32* %2
%9 = sdiv i32 %8, 1000
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = mul nsw i32 %10, 1000
%12 = load i32, i32* %2
%13 = sub nsw i32 %12, %11
store i32 %13, i32* %2
%14 = load i32, i32* %3
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %2
%17 = mul nsw i32 %16, 1000
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = call i32 @memset(%struct.timespec* %6, i32 0, i32 8)
br label %20
20: ; preds = %1, %36
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = call i32 @clock_nanosleep(i32 %21, i32 0, %struct.timespec* %5, %struct.timespec* %6)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %20
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%33 = load i32, i32* %32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %43
36: ; preds = %31, %27
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%38 = load i32, i32* %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i32 %41, i32* %42
br label %20
43: ; preds = %35
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_MONOTONIC = global i32 0
define void @mt_msleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = call i32 @memset(%struct.timespec* %2, i32 0, i32 8)
%7 = sdiv i32 %0, 1000
%8 = mul nsw i32 %7, -1000
%9 = add i32 %8, %0
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %7, i32* %10
%11 = mul nsw i32 %9, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @memset(%struct.timespec* %3, i32 0, i32 8)
%14 = load i32, i32* @CLOCK_MONOTONIC
%15 = call i32 @clock_nanosleep(i32 %14, i32 0, %struct.timespec* %2, %struct.timespec* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
br label %22
20: ; preds = %28, %1
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %17, %28
%23 = load i32, i32* %18
%24 = icmp eq i32 %23, 0
%25 = load i32, i32* %19
%26 = icmp eq i32 %25, 0
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %32, label %28
28: ; preds = %22
store i32 %23, i32* %10
store i32 %25, i32* %12
%29 = load i32, i32* @CLOCK_MONOTONIC
%30 = call i32 @clock_nanosleep(i32 %29, i32 0, %struct.timespec* %2, %struct.timespec* %3)
%31 = icmp eq i32 %30, 0
br i1 %31, label %22, label %20
32: ; preds = %22
ret void
}