#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ sandglass_get_currtime(struct timespec *);
int /*<<< orphan*/ sandglass_timespec_add(struct timespec *,
                                          struct timespec const *);
scalar_t__ sandglass_timespec_cmp(struct timespec *, struct timespec *);
void sandglass_spin(const struct timespec *ts) {
  struct timespec curr, until;
  sandglass_get_currtime(&curr);
  until = curr;
  sandglass_timespec_add(&until, ts);
  do {
    sandglass_get_currtime(&curr);
  } while (sandglass_timespec_cmp(&curr, &until) < 0);
}


x86 assembly (clang, -O0):
<sandglass_spin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <sandglass_get_currtime@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1040 <sandglass_timespec_add@plt>
lea    -0x10(%rbp),%rdi
call   1050 <sandglass_get_currtime@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <sandglass_timespec_cmp@plt>
cmp    $0x0,%rax
jl     1158 <sandglass_spin+0x28>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sandglass_spin>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <sandglass_get_currtime@plt>
mov    0x8(%rsp),%eax
mov    %eax,(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <sandglass_timespec_add@plt>
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <sandglass_get_currtime@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <sandglass_timespec_cmp@plt>
test   %rax,%rax
js     1160 <sandglass_spin+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
define void @sandglass_spin(%struct.timespec* %0) {
%2 = alloca %struct.timespec*
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store %struct.timespec* %0, %struct.timespec** %2
%5 = call i32 @sandglass_get_currtime(%struct.timespec* %3)
%6 = bitcast %struct.timespec* %4 to i8*
%7 = bitcast %struct.timespec* %3 to i8*
%8 = load %struct.timespec*, %struct.timespec** %2
%9 = call i32 @sandglass_timespec_add(%struct.timespec* %4, %struct.timespec* %8)
br label %10
10: ; preds = %12, %1
%11 = call i32 @sandglass_get_currtime(%struct.timespec* %3)
br label %12
12: ; preds = %10
%13 = call i64 @sandglass_timespec_cmp(%struct.timespec* %3, %struct.timespec* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %10, label %15
15: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
define void @sandglass_spin(%struct.timespec* %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 @sandglass_get_currtime(%struct.timespec* %2)
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%9 = load i32, i32* %7
store i32 %9, i32* %8
%10 = call i32 @sandglass_timespec_add(%struct.timespec* %3, %struct.timespec* %0)
br label %11
11: ; preds = %11, %1
%12 = call i32 @sandglass_get_currtime(%struct.timespec* %2)
%13 = call i64 @sandglass_timespec_cmp(%struct.timespec* %2, %struct.timespec* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %11, label %15
15: ; preds = %11
ret void
}