#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 /*<<< orphan*/ br_error(char *, char *);
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
int usec_sleep(long usecs) {
  struct timeval sleeptime;
  sleeptime.tv_sec = usecs / 1000000;
  sleeptime.tv_usec = usecs % 1000000;
  if (select(0, NULL, NULL, NULL, &sleeptime) < 0) {
    br_error("usec_sleep", "select");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<usec_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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    118e <usec_sleep+0x6e>
lea    0xe8a(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1040 <br_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1195 <usec_sleep+0x75>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usec_sleep>:
push   %rbx
sub    $0x10,%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,(%rsp)
imul   $0xf4240,%rdx,%rax
sub    %rax,%rdi
mov    %rdi,0x8(%rsp)
xor    %ebx,%ebx
mov    %rsp,%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1030 <select@plt>
test   %rax,%rax
jns    1185 <usec_sleep+0x65>
lea    0xe8c(%rip),%rdi
lea    0xe90(%rip),%rsi
call   1040 <br_error@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [11 x i8] c"usec_sleep\00"
@.str.1 = [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 %12
10: ; preds = %1
%11 = call i32 @br_error(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %1, %10
%13 = phi i32 [ -1, %10 ], [ 0, %1 ]
ret i32 %13
}