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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ ms_spin(int);
scalar_t__ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void ms_sleep(int msecs, bool spin) {
  if (msecs == 0)
    return;
  if (spin) {
    ms_spin(msecs);
    return;
  }
  struct timespec req;
  long secs = msecs / 1000;
  long nsecs = (msecs - secs * 1000) * 1000000;
  req.tv_sec = secs;
  req.tv_nsec = nsecs;
  if (nanosleep(&req, NULL) < 0) {
    fprintf(stderr, "Couldn't sleep for %d milliseconds\n", msecs);
  }
}


x86 assembly (clang, -O0):
<ms_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    114d <ms_sleep+0x1d>
jmp    11ce <ms_sleep+0x9e>
cmpl   $0x0,-0x8(%rbp)
je     1164 <ms_sleep+0x34>
mov    -0x4(%rbp),%edi
call   1050 <ms_spin@plt>
jmp    11ce <ms_sleep+0x9e>
mov    -0x4(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x20(%rbp)
movslq -0x4(%rbp),%rax
imul   $0x3e8,-0x20(%rbp),%rcx
sub    %rcx,%rax
imul   $0xf4240,%rax,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
jge    11ce <ms_sleep+0x9e>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe37(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ms_sleep>:
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     11a5 <ms_sleep+0x75>
mov    %edi,%ebx
test   %esi,%esi
je     114b <ms_sleep+0x1b>
mov    %ebx,%edi
add    $0x10,%rsp
pop    %rbx
jmp    1050 <ms_spin@plt>
movslq %ebx,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
movslq %ecx,%rcx
imul   $0xfffffffffffffc18,%rcx,%rdx
add    %rax,%rdx
imul   $0xf4240,%rdx,%rax
mov    %rcx,(%rsp)
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
test   %rax,%rax
jns    11a5 <ms_sleep+0x75>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
mov    %ebx,%edx
call   1040 <fprintf@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [36 x i8] c"Couldn't sleep for %d milliseconds\0A\00"
define void @ms_sleep(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timespec
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
br label %37
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* %3
%16 = call i32 @ms_spin(i32 %15)
br label %37
17: ; preds = %11
%18 = load i32, i32* %3
%19 = sdiv i32 %18, 1000
%20 = sext i32 %19 to i64
store i64 %20, i64* %6
%21 = load i32, i32* %3
%22 = sext i32 %21 to i64
%23 = load i64, i64* %6
%24 = mul nsw i64 %23, 1000
%25 = sub nsw i64 %22, %24
%26 = mul nsw i64 %25, 1000000
store i64 %26, i64* %7
%27 = load i64, i64* %6
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
store i64 %27, i64* %28
%29 = load i64, i64* %7
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
store i64 %29, i64* %30
%31 = call i64 @nanosleep(%struct.timespec* %5, i32* null)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %17
%34 = load i32, i32* @stderr
%35 = load i32, i32* %3
%36 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %10, %14, %33, %17
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [36 x i8] c"Couldn't sleep for %d milliseconds\0A\00"
define void @ms_sleep(i32 %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = icmp eq i32 %0, 0
br i1 %4, label %25, label %5
5: ; preds = %2
%6 = icmp eq i32 %1, 0
br i1 %6, label %9, label %7
7: ; preds = %5
%8 = tail call i32 @ms_spin(i32 %0)
br label %25
9: ; preds = %5
%10 = bitcast %struct.timespec* %3 to i8*
%11 = sdiv i32 %0, 1000
%12 = sext i32 %11 to i64
%13 = sext i32 %0 to i64
%14 = mul nsw i64 %12, -1000
%15 = add nsw i64 %14, %13
%16 = mul nsw i64 %15, 1000000
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i64 %12, i64* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
store i64 %16, i64* %18
%19 = call i64 @nanosleep(%struct.timespec* %3, i32* null)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %9
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %24
24: ; preds = %21, %9
br label %25
25: ; preds = %2, %24, %7
ret void
}