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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int sleep_us(unsigned usec) {
  if (!(usec >= 0 && usec <= 1000000)) {
    errno = EINVAL;
    return -1;
  }
  struct timeval timeval;
  timeval.tv_sec = 0;
  timeval.tv_usec = usec;
  return select(0, NULL, NULL, NULL, &timeval);
}


x86 assembly (clang, -O0):
<sleep_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jb     1132 <sleep_us+0x22>
cmpl   $0xf4240,-0x8(%rbp)
jbe    1150 <sleep_us+0x40>
mov    0x2e9f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1176 <sleep_us+0x66>
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <select@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_us>:
sub    $0x18,%rsp
cmp    $0xf4241,%edi
jae    1140 <sleep_us+0x30>
movq   $0x0,0x10(%rsp)
mov    %edi,0x8(%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
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    0x2e90(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sleep_us(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp uge i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* %3
%9 = icmp ule i32 %8, 1000000
br i1 %9, label %12, label %10
10: ; preds = %7, %1
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
store i32 -1, i32* %2
br label %17
12: ; preds = %7
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %13
%14 = load i32, i32* %3
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 %14, i32* %15
%16 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %4)
store i32 %16, i32* %2
br label %17
17: ; preds = %12, %10
%18 = load i32, i32* %2
ret i32 %18
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @sleep_us(i32 %0) {
%2 = alloca %struct.timeval
%3 = icmp ult i32 %0, 1000001
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = load i32, i32* @EINVAL
store i32 %5, i32* @errno
br label %11
6: ; preds = %1
%7 = bitcast %struct.timeval* %2 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 %0, i32* %9
%10 = call i32 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %2)
br label %11
11: ; preds = %6, %4
%12 = phi i32 [ %10, %6 ], [ -1, %4 ]
ret i32 %12
}