#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 {
  long tv_sec;
  long tv_usec;
};

/* Variables and functions */
int errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
int usec_sleep(long usecs) {
  struct timeval sleeptime;
  int tmperrno;
  sleeptime.tv_sec = usecs / 1000000;
  sleeptime.tv_usec = usecs % 1000000;
  if (select(0, NULL, NULL, NULL, &sleeptime) < 0) {
    tmperrno = errno;
    perror("select");
    errno = tmperrno;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<usec_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x18(%rbp)
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1030 <select@plt>
cmp    $0x0,%rax
jge    119f <usec_sleep+0x7f>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    0xe7e(%rip),%rdi
call   1040 <perror@plt>
mov    -0x24(%rbp),%ecx
mov    0x2e4f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a6 <usec_sleep+0x86>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usec_sleep>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rdi,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x12,%rdx
add    %rax,%rdx
mov    %rdx,0x8(%rsp)
imul   $0xf4240,%rdx,%rax
sub    %rax,%rdi
mov    %rdi,0x10(%rsp)
xor    %ebx,%ebx
lea    0x8(%rsp),%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <select@plt>
test   %rax,%rax
js     117a <usec_sleep+0x5a>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e5f(%rip),%rbx
mov    (%rbx),%ebp
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
mov    %ebp,(%rbx)
mov    $0xffffffff,%ebx
jmp    1171 <usec_sleep+0x51>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@errno = global i32 0
@.str = [7 x i8] c"select\00"
define i32 @usec_sleep(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.timeval
%5 = alloca i32
store i64 %0, i64* %3
%6 = load i64, i64* %3
%7 = sdiv i64 %6, 1000000
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i64 %7, i64* %8
%9 = load i64, i64* %3
%10 = srem i64 %9, 1000000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 %10, i64* %11
%12 = call i64 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %1
%15 = load i32, i32* @errno
store i32 %15, i32* %5
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %5
store i32 %17, i32* @errno
store i32 -1, i32* %2
br label %19
18: ; preds = %1
store i32 0, i32* %2
br label %19
19: ; preds = %18, %14
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@errno = global i32 0
@.str = [7 x i8] c"select\00"
define i32 @usec_sleep(i64 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = sdiv i64 %0, 1000000
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i64 %4, i64* %5
%6 = srem i64 %0, 1000000
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 %6, i64* %7
%8 = call i64 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* @errno
br label %13
13: ; preds = %1, %10
%14 = phi i32 [ -1, %10 ], [ 0, %1 ]
ret i32 %14
}