#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;
  int tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
spinWait(int milliseconds) {
  struct timeval startTime;
  struct timeval endTime;
  gettimeofday(&startTime, NULL);
  do {
    gettimeofday(&endTime, NULL);
  } while ((endTime.tv_sec - startTime.tv_sec) * 1000000 +
               (endTime.tv_usec - startTime.tv_usec) <
           milliseconds * 1000);
}


x86 assembly (clang, -O0):
<spinWait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
sub    -0x10(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    -0x14(%rbp),%ecx
sub    -0xc(%rbp),%ecx
add    %ecx,%eax
imul   $0x3e8,-0x8(%rbp),%ecx
cmp    %ecx,%eax
jl     1128 <spinWait+0x18>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spinWait>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x10(%rsp),%r15d
mov    0x14(%rsp),%ebx
imul   $0x3e8,%r14d,%ebp
lea    0x8(%rsp),%r14
xchg   %ax,%ax
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x8(%rsp),%eax
sub    %r15d,%eax
mov    0xc(%rsp),%ecx
sub    %ebx,%ecx
imul   $0xf4240,%eax,%eax
add    %ecx,%eax
cmp    %ebp,%eax
jl     1140 <spinWait+0x30>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @spinWait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
store i32 %0, i32* %3
%6 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %7
7: ; preds = %9, %1
%8 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
br label %9
9: ; preds = %7
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%13 = load i32, i32* %12
%14 = sub nsw i32 %11, %13
%15 = mul nsw i32 %14, 1000000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%19 = load i32, i32* %18
%20 = sub nsw i32 %17, %19
%21 = add nsw i32 %15, %20
%22 = load i32, i32* %3
%23 = mul nsw i32 %22, 1000
%24 = icmp slt i32 %21, %23
br i1 %24, label %7, label %25
25: ; preds = %9
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @spinWait(i32 %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 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = mul nsw i32 %0, 1000
br label %14
14: ; preds = %14, %1
%15 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%16 = load i32, i32* %7
%17 = sub nsw i32 %16, %9
%18 = mul nsw i32 %17, 1000000
%19 = load i32, i32* %10
%20 = sub i32 %19, %12
%21 = add nsw i32 %20, %18
%22 = icmp slt i32 %21, %13
br i1 %22, label %14, label %23
23: ; preds = %14
ret i32 undef
}