#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;
int /*<<< orphan*/ delayMicrosecondsHard(long);
scalar_t__ errno;
int nanosleep(struct timespec *, struct timespec *);
void ltm_sleep(long usec) {
  struct timespec to_wait;
  struct timespec remaining;
  int sleep_retval;
  if (usec < 100) {
    delayMicrosecondsHard(usec);
  } else {
    to_wait.tv_sec = usec / 1000000;
    remaining.tv_sec = 0;
    to_wait.tv_nsec = usec % 1000000;
    remaining.tv_nsec = 0;
    sleep_retval = -1;
    errno = EINTR;
    while ((sleep_retval == -1) && (errno == EINTR)) {
      sleep_retval = nanosleep(&to_wait, &remaining);
      to_wait.tv_sec = remaining.tv_sec;
      to_wait.tv_nsec = remaining.tv_nsec;
    }
  }
}


x86 assembly (clang, -O0):
<ltm_sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x64,-0x8(%rbp)
jge    1145 <ltm_sleep+0x25>
mov    -0x8(%rbp),%rdi
call   1040 <delayMicrosecondsHard@plt>
jmp    11f7 <ltm_sleep+0xd7>
mov    -0x8(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%rcx
mov    0x2e4f(%rip),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
cmpl   $0xffffffff,-0x2c(%rbp)
mov    %al,-0x2d(%rbp)
jne    11bd <ltm_sleep+0x9d>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11cd <ltm_sleep+0xad>
jmp    11f2 <ltm_sleep+0xd2>
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1194 <ltm_sleep+0x74>
jmp    11f7 <ltm_sleep+0xd7>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltm_sleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x63,%rdi
jg     1141 <ltm_sleep+0x21>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <delayMicrosecondsHard@plt>
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rdi,%rax
mul    %rcx
shr    $0x12,%rdx
mov    %rdx,(%rsp)
imul   $0xf4240,%rdx,%rax
sub    %rax,%rdi
mov    %rdi,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e61(%rip),%r15
mov    (%r15),%rax
mov    0x2e5f(%rip),%r12
mov    %rax,(%r12)
mov    %rsp,%r14
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
movaps 0x10(%rsp),%xmm0
movaps %xmm0,(%rsp)
cmp    $0xffffffff,%eax
jne    11b2 <ltm_sleep+0x92>
mov    (%r15),%rax
cmp    %rax,(%r12)
je     1190 <ltm_sleep+0x70>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@EINTR = global i64 0
@errno = global i64 0
define void @ltm_sleep(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 = icmp slt i64 %6, 100
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i64, i64* %2
%10 = call i32 @delayMicrosecondsHard(i64 %9)
br label %39
11: ; preds = %1
%12 = load i64, i64* %2
%13 = sdiv i64 %12, 1000000
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 0, i64* %15
%16 = load i64, i64* %2
%17 = srem i64 %16, 1000000
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %17, i64* %18
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %19
store i32 -1, i32* %5
%20 = load i64, i64* @EINTR
store i64 %20, i64* @errno
br label %21
21: ; preds = %30, %11
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br label %28
28: ; preds = %24, %21
%29 = phi i1 [ false, %21 ], [ %27, %24 ]
br i1 %29, label %30, label %38
30: ; preds = %28
%31 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
store i32 %31, i32* %5
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%33 = load i64, i64* %32
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %33, i64* %34
%35 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %36, i64* %37
br label %21
38: ; preds = %28
br label %39
39: ; preds = %38, %8
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@EINTR = global i64 0
@errno = global i64 0
define void @ltm_sleep(i64 %0) {
%2 = alloca <2 x i64>
%3 = bitcast <2 x i64>* %2 to %struct.timespec*
%4 = alloca <2 x i64>
%5 = bitcast <2 x i64>* %4 to %struct.timespec*
%6 = bitcast <2 x i64>* %2 to i8*
%7 = bitcast <2 x i64>* %4 to i8*
%8 = icmp slt i64 %0, 100
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @delayMicrosecondsHard(i64 %0)
br label %25
11: ; preds = %1
%12 = udiv i64 %0, 1000000
%13 = getelementptr inbounds <2 x i64>, <2 x i64>* %2, i64 0, i64 0
store i64 %12, i64* %13
%14 = urem i64 %0, 1000000
%15 = getelementptr inbounds <2 x i64>, <2 x i64>* %2, i64 0, i64 1
store i64 %14, i64* %15
store <2 x i64> zeroinitializer, <2 x i64>* %4
%16 = load i64, i64* @EINTR
store i64 %16, i64* @errno
br label %17
17: ; preds = %11, %17
%18 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %5)
%19 = load <2 x i64>, <2 x i64>* %4
store <2 x i64> %19, <2 x i64>* %2
%20 = icmp eq i32 %18, -1
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
%24 = select i1 %20, i1 %23, i1 false
br i1 %24, label %17, label %25
25: ; preds = %17, %9
ret void
}