#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 long tv_sec;
  unsigned long tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
int usleep(unsigned long microSeconds) {
  fd_set readfds, writefds, exceptfds;
  struct timeval timer;
  timer.tv_sec = microSeconds / (unsigned long)1000000;
  timer.tv_usec = microSeconds % (unsigned long)1000000;
  FD_ZERO(&readfds);
  FD_ZERO(&writefds);
  FD_ZERO(&exceptfds);
  if (select(0, &readfds, &writefds, &exceptfds, &timer) < 0) {
    perror("usleep (select) failed");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<usleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x28(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <FD_ZERO@plt>
lea    -0x18(%rbp),%rdi
call   1030 <FD_ZERO@plt>
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x1c(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1040 <select@plt>
cmp    $0x0,%rax
jge    11b4 <usleep+0x84>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <usleep+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rdi,%rax
mul    %rcx
shr    $0x12,%rdx
mov    %rdx,0x18(%rsp)
imul   $0xf4240,%rdx,%rax
sub    %rax,%rdi
mov    %rdi,0x20(%rsp)
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1030 <FD_ZERO@plt>
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   1030 <FD_ZERO@plt>
xor    %ebx,%ebx
lea    0x18(%rsp),%r8
xor    %edi,%edi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1040 <select@plt>
test   %rax,%rax
js     11b4 <usleep+0x84>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11a6 <usleep+0x76>

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [23 x i8] c"usleep (select) failed\00"
define i32 @usleep(i64 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.timeval* %5 to i8*
%10 = udiv i64 %0, 1000000
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i64 %10, i64* %11
%12 = urem i64 %0, 1000000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 %12, i64* %13
%14 = call i32 @FD_ZERO(i32* %2)
%15 = call i32 @FD_ZERO(i32* %3)
%16 = call i32 @FD_ZERO(i32* %4)
%17 = call i64 @select(i32 0, i32* %2, i32* %3, i32* %4, %struct.timeval* %5)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %1, %19
%22 = phi i32 [ -1, %19 ], [ 0, %1 ]
ret i32 %22
}