#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 */
typedef scalar_t__ time_t;
struct timeval {
  scalar_t__ tv_sec;
  int tv_usec;
};
struct timespec {
  scalar_t__ tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
void xusleep(const struct timespec *ts_delay) {
  struct timeval tv_delay;
  tv_delay.tv_sec = ts_delay->tv_sec;
  tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
  if (tv_delay.tv_usec == 1000000) {
    time_t t1 = tv_delay.tv_sec + 1;
    if (t1 < tv_delay.tv_sec)
      tv_delay.tv_usec = 1000000 - 1;
    else {
      tv_delay.tv_sec = t1;
      tv_delay.tv_usec = 0;
    }
  }
  select(0, NULL, NULL, NULL, &tv_delay);
}


x86 assembly (clang, -O0):
<xusleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%eax
add    $0x3e7,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jne    1185 <xusleep+0x75>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1171 <xusleep+0x61>
movl   $0xf423f,-0x10(%rbp)
jmp    1180 <xusleep+0x70>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
jmp    1185 <xusleep+0x75>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <select@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xusleep>:
sub    $0x18,%rsp
mov    (%rdi),%rax
mov    %rax,0x8(%rsp)
mov    0x8(%rdi),%ecx
lea    0x3e7(%rcx),%edx
movslq %edx,%rdx
imul   $0x10624dd3,%rdx,%rdx
mov    %rdx,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rdx
add    %esi,%edx
mov    %edx,0x10(%rsp)
add    $0xc46539e7,%ecx
cmp    $0x3e7,%ecx
ja     115f <xusleep+0x4f>
add    $0x1,%rax
mov    %rax,0x8(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x8(%rsp),%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <select@plt>
add    $0x18,%rsp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
%struct.timeval = type { i64, i32 }
define void @xusleep(%struct.timespec* nocapture %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i64 %5, i64* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %0, i64 0, i32 1
%8 = load i32, i32* %7
%9 = add nsw i32 %8, 999
%10 = sdiv i32 %9, 1000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = add i32 %8, -999999001
%13 = icmp ult i32 %12, 1000
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = add nsw i64 %5, 1
store i64 %15, i64* %6
store i32 0, i32* %11
br label %16
16: ; preds = %14, %1
%17 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %2)
ret void
}