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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
void ms_spin(int msecs) {
  struct timespec t;
  long start_ms;
  long curr_ms;
  clock_gettime(CLOCK_REALTIME, &t);
  start_ms = t.tv_sec * 1000 + t.tv_nsec / 1000000;
  curr_ms = start_ms;
  while (curr_ms - start_ms < msecs) {
    clock_gettime(CLOCK_REALTIME, &t);
    curr_ms = t.tv_sec * 1000 + t.tv_nsec / 1000000;
  }
}


x86 assembly (clang, -O0):
<ms_spin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2ebe(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3e8,-0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
movslq -0x4(%rbp),%rcx
cmp    %rcx,%rax
jge    11a5 <ms_spin+0x95>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3e8,-0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
jmp    1157 <ms_spin+0x47>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ms_spin>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2ec0(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1030 <clock_gettime@plt>
test   %ebp,%ebp
jle    119d <ms_spin+0x8d>
movslq (%rsp),%rax
imul   $0x3e8,%rax,%rax
movslq 0x4(%rsp),%rcx
imul   $0x431bde83,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
movslq %ecx,%rbx
add    %rax,%rbx
movslq %ebp,%rbp
mov    %rsp,%r15
nop
mov    (%r14),%edi
mov    %r15,%rsi
call   1030 <clock_gettime@plt>
movslq (%rsp),%rax
imul   $0x3e8,%rax,%rax
movslq 0x4(%rsp),%rcx
imul   $0x431bde83,%rcx,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
movslq %ecx,%rcx
add    %rax,%rcx
sub    %rbx,%rcx
cmp    %rbp,%rcx
jl     1160 <ms_spin+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@CLOCK_REALTIME = global i32 0
define void @ms_spin(i32 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_REALTIME
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%7 = load i32, i32* %6
%8 = mul nsw i32 %7, 1000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%10 = load i32, i32* %9
%11 = sdiv i32 %10, 1000000
%12 = add nsw i32 %11, %8
%13 = sext i32 %12 to i64
%14 = sext i32 %0 to i64
%15 = icmp sgt i32 %0, 0
br i1 %15, label %16, label %27
16: ; preds = %1, %16
%17 = load i32, i32* @CLOCK_REALTIME
%18 = call i32 @clock_gettime(i32 %17, %struct.timespec* %2)
%19 = load i32, i32* %6
%20 = mul nsw i32 %19, 1000
%21 = load i32, i32* %9
%22 = sdiv i32 %21, 1000000
%23 = add nsw i32 %22, %20
%24 = sext i32 %23 to i64
%25 = sub nsw i64 %24, %13
%26 = icmp slt i64 %25, %14
br i1 %26, label %16, label %27
27: ; preds = %16, %1
ret void
}