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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ terminal_error(char *);
void microsleep(unsigned long long usecs) {
  struct timespec req, rem;
  rem.tv_sec = rem.tv_nsec = 0;
  req.tv_sec = usecs / 1000000;
  req.tv_nsec = (usecs - (req.tv_sec * 1000000)) * 1000;
continue_sleep:
  if ((nanosleep(&req, &rem)) == -1) {
    if (errno == EINTR) {
      if (rem.tv_sec || rem.tv_nsec) {
        req.tv_sec = rem.tv_sec;
        req.tv_nsec = rem.tv_nsec;
        goto continue_sleep;
      }
      goto out;
    }
    terminal_error("nanosleep");
  }
out:
  return;
}


x86 assembly (clang, -O0):
<microsleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
imul   $0xf4240,-0x18(%rbp),%rcx
sub    %rcx,%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11d4 <microsleep+0xb4>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jne    11c8 <microsleep+0xa8>
cmpq   $0x0,-0x28(%rbp)
jne    11ae <microsleep+0x8e>
cmpq   $0x0,-0x20(%rbp)
je     11c3 <microsleep+0xa3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1168 <microsleep+0x48>
jmp    11d9 <microsleep+0xb9>
lea    0xe31(%rip),%rdi
call   1040 <terminal_error@plt>
jmp    11d9 <microsleep+0xb9>
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
mul    %rcx
shr    $0x12,%rdx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rdx,(%rsp)
imul   $0xfffffffffff0bdc0,%rdx,%rax
add    %rdi,%rax
imul   $0x3e8,%rax,%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     117f <microsleep+0x5f>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e5a(%rip),%r15
mov    0x2e4b(%rip),%rbx
mov    %rsp,%r14
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%rax
cmp    (%rbx),%rax
jne    11d5 <microsleep+0xb5>
mov    0x10(%rsp),%rax
mov    0x18(%rsp),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
je     1173 <microsleep+0x53>
mov    %rax,(%rsp)
mov    %rcx,0x8(%rsp)
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     11a0 <microsleep+0x80>
jmp    1173 <microsleep+0x53>
lea    0xe24(%rip),%rdi
call   1040 <terminal_error@plt>
jmp    1173 <microsleep+0x53>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [10 x i8] c"nanosleep\00"
define void @microsleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i64 %0, i64* %2
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 0, i64* %6
%7 = load i64, i64* %2
%8 = udiv i64 %7, 1000000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %8, i64* %9
%10 = load i64, i64* %2
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%12 = load i64, i64* %11
%13 = mul i64 %12, 1000000
%14 = sub i64 %10, %13
%15 = mul i64 %14, 1000
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %15, i64* %16
br label %17
17: ; preds = %32, %1
%18 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %42
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %40
24: ; preds = %20
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp ne i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%30 = load i64, i64* %29
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %28, %24
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%34 = load i64, i64* %33
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %34, i64* %35
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%37 = load i64, i64* %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %37, i64* %38
br label %17
39: ; preds = %28
br label %43
40: ; preds = %20
%41 = call i32 @terminal_error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %42
42: ; preds = %40, %17
br label %43
43: ; preds = %42, %39
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@.str = [10 x i8] c"nanosleep\00"
define void @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 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%8 = udiv i64 %0, 1000000
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%10 = bitcast %struct.timespec* %3 to i8*
store i64 %8, i64* %9
%11 = mul i64 %8, -1000000
%12 = add i64 %11, %0
%13 = mul i64 %12, 1000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %13, i64* %14
%15 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %32
17: ; preds = %1, %27
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = load i64, i64* %7
%23 = icmp ne i64 %22, 0
%24 = load i64, i64* %6
%25 = icmp ne i64 %24, 0
%26 = select i1 %23, i1 true, i1 %25
br i1 %26, label %27, label %32
27: ; preds = %21
store i64 %22, i64* %9
store i64 %24, i64* %14
%28 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %17, label %32
30: ; preds = %17
%31 = call i32 @terminal_error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %21, %27, %1, %30
ret void
}