#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_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    int /*<<< orphan*/ *);
int errno;
int usleep(useconds_t useconds) {
  struct timespec ts = {.tv_sec = useconds / 1000000,
                        .tv_nsec = useconds % 1000000 * 1000};
  int error = clock_nanosleep(CLOCK_REALTIME, 0, &ts, NULL);
  if (error != 0) {
    errno = error;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<usleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0xc(%rbp)
mov    0x2e94(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <clock_nanosleep@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117a <usleep+0x6a>
mov    -0x14(%rbp),%ecx
mov    0x2e74(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1181 <usleep+0x71>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep>:
push   %rbx
sub    $0x10,%rsp
movslq %edi,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
xor    %esi,%esi
xor    %ecx,%ecx
call   1030 <clock_nanosleep@plt>
test   %eax,%eax
je     116d <usleep+0x5d>
mov    0x2e7a(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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