#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 int useconds_t;
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int nanosleep(struct timespec *, struct timespec *);
int usleep(useconds_t useconds) {
  struct timespec rqtp;
  struct timespec rmtp;
  if (useconds == 0) {
    return 0;
  }
  if (useconds >= 1000000) {
    errno = EINVAL;
    return -1;
  }
  rqtp.tv_sec = 0;
  rqtp.tv_nsec = useconds * 1000;
  return nanosleep(&rqtp, &rmtp);
}


x86 assembly (clang, -O0):
<usleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1131 <usleep+0x21>
movl   $0x0,-0x4(%rbp)
jmp    117e <usleep+0x6e>
cmpl   $0xf4240,-0x8(%rbp)
jl     115c <usleep+0x4c>
mov    0x2e93(%rip),%rax
mov    (%rax),%ecx
mov    0x2e92(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    117e <usleep+0x6e>
movq   $0x0,-0x10(%rbp)
imul   $0x3e8,-0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep>:
sub    $0x28,%rsp
test   %edi,%edi
je     113c <usleep+0x2c>
cmp    $0xf4240,%edi
jl     1143 <usleep+0x33>
mov    0x2eb1(%rip),%rax
mov    (%rax),%eax
mov    0x2eb0(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x28,%rsp
ret
xor    %eax,%eax
add    $0x28,%rsp
ret
movq   $0x0,0x10(%rsp)
imul   $0x3e8,%edi,%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @usleep(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %20
9: ; preds = %1
%10 = load i32, i32* %3
%11 = icmp sge i32 %10, 1000000
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
store i32 -1, i32* %2
br label %20
14: ; preds = %9
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %15
%16 = load i32, i32* %3
%17 = mul nsw i32 %16, 1000
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i32 %17, i32* %18
%19 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
store i32 %19, i32* %2
br label %20
20: ; preds = %14, %12, %8
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @usleep(i32 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = icmp eq i32 %0, 0
br i1 %6, label %16, label %7
7: ; preds = %1
%8 = icmp sgt i32 %0, 999999
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
br label %16
11: ; preds = %7
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %12
%13 = mul nsw i32 %0, 1000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
br label %16
16: ; preds = %1, %11, %9
%17 = phi i32 [ -1, %9 ], [ %15, %11 ], [ 0, %1 ]
ret i32 %17
}