#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 timespec {
  unsigned long tv_sec;
  unsigned long tv_nsec;
};

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int usleep(unsigned long usec) {
  struct timespec ts;
  ts.tv_sec = usec / 1000000UL;
  ts.tv_nsec = (usec % 1000000UL) * 1000UL;
  for (;;) {
    if (nanosleep(&ts, &ts) == 0)
      return 0;
    if (errno != EINTR)
      return -1;
  }
}


x86 assembly (clang, -O0):
<usleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0x0,%rax
jne    1169 <usleep+0x59>
movl   $0x0,-0x4(%rbp)
jmp    1194 <usleep+0x84>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
je     118f <usleep+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <usleep+0x84>
jmp    1147 <usleep+0x37>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rdi,%rax
mul    %rcx
shr    $0x12,%rdx
mov    %rdx,(%rsp)
imul   $0xf4240,%rdx,%rax
sub    %rax,%rdi
imul   $0x3e8,%rdi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rbx
mov    0x2e8f(%rip),%r14
mov    0x2e80(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
test   %rax,%rax
je     117f <usleep+0x6f>
mov    (%r14),%rax
cmp    (%r15),%rax
je     1160 <usleep+0x50>
mov    $0xffffffff,%eax
jmp    1181 <usleep+0x71>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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