#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void spin(double sleep_time) {
  struct timeval spin_start, now;
  gettimeofday(&spin_start, NULL);
  long sleep_time_usec = (long)(sleep_time * 1e6);
  now = spin_start;
  while (now.tv_sec * 1000000 + now.tv_usec <
         spin_start.tv_sec * 1000000 + spin_start.tv_usec + sleep_time_usec) {
    gettimeofday(&now, NULL);
  }
}


x86 assembly (clang, -O0):
<spin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
movsd  0xece(%rip),%xmm0
mulsd  -0x8(%rbp),%xmm0
cvttsd2si %xmm0,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
imul   $0xf4240,-0x28(%rbp),%eax
cltq
add    -0x20(%rbp),%rax
imul   $0xf4240,-0x18(%rbp),%ecx
movslq %ecx,%rcx
add    -0x10(%rbp),%rcx
add    -0x30(%rbp),%rcx
cmp    %rcx,%rax
jge    118a <spin+0x7a>
lea    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
jmp    1150 <spin+0x40>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spin>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x10(%rsp)
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movsd  0x10(%rsp),%xmm0
mulsd  0xec9(%rip),%xmm0
cvttsd2si %xmm0,%rbx
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
movslq 0x18(%rsp),%rax
imul   $0xf4240,%rax,%rax
add    0x20(%rsp),%rbx
add    %rax,%rbx
movslq (%rsp),%rax
imul   $0xf4240,%rax,%rax
add    0x8(%rsp),%rax
cmp    %rbx,%rax
jge    119f <spin+0x8f>
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
movslq (%rsp),%rax
imul   $0xf4240,%rax,%rax
add    0x8(%rsp),%rax
cmp    %rbx,%rax
jl     1180 <spin+0x70>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define void @spin(double %0) {
%2 = alloca double
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca i64
store double %0, double* %2
%6 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%7 = load double, double* %2
%8 = fmul double %7, 1.000000e+06
%9 = fptosi double %8 to i64
store i64 %9, i64* %5
%10 = bitcast %struct.timeval* %4 to i8*
%11 = bitcast %struct.timeval* %3 to i8*
br label %12
12: ; preds = %30, %1
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = mul nsw i32 %14, 1000000
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%18 = load i64, i64* %17
%19 = add nsw i64 %16, %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = mul nsw i32 %21, 1000000
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%25 = load i64, i64* %24
%26 = add nsw i64 %23, %25
%27 = load i64, i64* %5
%28 = add nsw i64 %26, %27
%29 = icmp slt i64 %19, %28
br i1 %29, label %30, label %32
30: ; preds = %12
%31 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %12
32: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define void @spin(double %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = fmul double %0, 1.000000e+06
%8 = fptosi double %7 to i64
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = mul nsw i32 %12, 1000000
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%16 = load i64, i64* %15
%17 = add i64 %16, %8
%18 = add i64 %17, %14
%19 = load i32, i32* %9
%20 = mul nsw i32 %19, 1000000
%21 = sext i32 %20 to i64
%22 = load i64, i64* %10
%23 = add nsw i64 %22, %21
%24 = icmp slt i64 %23, %18
br i1 %24, label %25, label %33
25: ; preds = %1, %25
%26 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%27 = load i32, i32* %9
%28 = mul nsw i32 %27, 1000000
%29 = sext i32 %28 to i64
%30 = load i64, i64* %10
%31 = add nsw i64 %30, %29
%32 = icmp slt i64 %31, %18
br i1 %32, label %25, label %33
33: ; preds = %25, %1
ret void
}