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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
int microsleep(long microsecs) {
  struct timespec req, rem;
  req.tv_sec = microsecs / 1000000;
  req.tv_nsec = (microsecs % 1000000) * 1000;
  int ret;
  while ((ret = nanosleep(&req, &rem)) == -1 && errno == EINTR)
    req = rem;
  return ret;
}


x86 assembly (clang, -O0):
<microsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x2d(%rbp)
jne    1181 <microsleep+0x71>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1191 <microsleep+0x81>
jmp    11a6 <microsleep+0x96>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1147 <microsleep+0x37>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<microsleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%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
imul   $0x3e8,%rdi,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11b2 <microsleep+0xa2>
mov    0x2e74(%rip),%r15
mov    0x2e65(%rip),%r12
mov    (%r12),%rcx
cmp    %rcx,(%r15)
jne    11b2 <microsleep+0xa2>
mov    %rsp,%r14
lea    0x18(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11b2 <microsleep+0xa2>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1190 <microsleep+0x80>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @microsleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = sdiv i64 %6, 1000000
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %7, i64* %8
%9 = load i64, i64* %2
%10 = srem i64 %9, 1000000
%11 = mul nsw i64 %10, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %11, i64* %12
br label %13
13: ; preds = %22, %1
%14 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %14, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %16, %13
%21 = phi i1 [ false, %13 ], [ %19, %16 ]
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = bitcast %struct.timespec* %3 to i8*
%24 = bitcast %struct.timespec* %4 to i8*
br label %13
25: ; preds = %20
%26 = load i32, i32* %5
ret i32 %26
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @microsleep(i64 %0) {
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %2 to i8*
%5 = bitcast %struct.timespec* %3 to i8*
%6 = sdiv i64 %0, 1000000
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %6, i64* %7
%8 = srem i64 %0, 1000000
%9 = mul nsw i64 %8, 1000
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %9, i64* %10
%11 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%12 = icmp eq i32 %11, -1
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
%16 = select i1 %12, i1 %15, i1 false
br i1 %16, label %17, label %24
17: ; preds = %1, %17
%18 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%19 = icmp eq i32 %18, -1
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
%23 = select i1 %19, i1 %22, i1 false
br i1 %23, label %17, label %24
24: ; preds = %17, %1
%25 = phi i32 [ %11, %1 ], [ %18, %17 ]
ret i32 %25
}