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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
int usleep(useconds_t useconds) {
  struct timespec ts;
  ts.tv_sec = (long int)useconds / 1000000;
  ts.tv_nsec = ((long int)useconds % 1000000) * 1000;
  if (!nanosleep(&ts, &ts))
    return 0;
  if (errno == EINTR)
    return ts.tv_sec;
  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
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rsi
mov    %rsi,%rdi
call   1030 <nanosleep@plt>
cmp    $0x0,%eax
jne    1168 <usleep+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1195 <usleep+0x85>
mov    0x2e71(%rip),%rax
mov    (%rax),%rax
mov    0x2e5f(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <usleep+0x7e>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1195 <usleep+0x85>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<usleep>:
sub    $0x18,%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,0x8(%rsp)
imul   $0xf4240,%rdx,%rax
sub    %rax,%rdi
imul   $0x3e8,%rdi,%rax
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    %rdi,%rsi
call   1030 <nanosleep@plt>
test   %eax,%eax
je     1182 <usleep+0x72>
mov    0x2e7b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e69(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    117d <usleep+0x6d>
mov    0x8(%rsp),%eax
add    $0x18,%rsp
ret
xor    %eax,%eax
add    $0x18,%rsp
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 = sdiv 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 = srem i64 %8, 1000000
%10 = mul nsw i64 %9, 1000
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 %10, i64* %11
%12 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %4)
%13 = icmp ne i32 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %1
store i32 0, i32* %2
br label %24
15: ; preds = %1
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%21 = load i64, i64* %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %2
br label %24
23: ; preds = %15
store i32 -1, i32* %2
br label %24
24: ; preds = %23, %19, %14
%25 = load i32, i32* %2
ret i32 %25
}

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 = sdiv i64 %0, 1000000
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %4, i64* %5
%6 = srem i64 %0, 1000000
%7 = mul nsw i64 %6, 1000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %7, i64* %8
%9 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i64, i64* %5
%17 = trunc i64 %16 to i32
br label %18
18: ; preds = %11, %1, %15
%19 = phi i32 [ %17, %15 ], [ 0, %1 ], [ -1, %11 ]
ret i32 %19
}