#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_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ TimeSpec;

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int EINTR;
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    struct timespec *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memcpy(struct timespec *, struct timespec *, int);
int /*<<< orphan*/ stderr;
void Sleep(unsigned long long TimeToSleepNs) {
  struct timespec TimeSpec, TimeSpecRemain;
  int SleepResult;
  TimeSpec.tv_sec = 0;
  TimeSpec.tv_nsec = TimeToSleepNs;
  for (;;) {
    SleepResult =
        clock_nanosleep(CLOCK_MONOTONIC, 0, &TimeSpec, &TimeSpecRemain);
    if (SleepResult == 0) {
      break;
    } else if (SleepResult != EINTR) {
      fprintf(stderr, "clock_nanosleep() failed: SleepResult = %d\n",
              SleepResult);
      exit(1);
    }
    memcpy(&TimeSpec, &TimeSpecRemain, sizeof(TimeSpec));
  }
}


x86 assembly (clang, -O0):
<Sleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1060 <clock_nanosleep@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1186 <Sleep+0x46>
jmp    11d6 <Sleep+0x96>
mov    -0x2c(%rbp),%eax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%eax
je     11ba <Sleep+0x7a>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xe55(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11bf <Sleep+0x7f>
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
jmp    115c <Sleep+0x1c>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Sleep>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x0,0x10(%rsp)
mov    %rdi,0x8(%rsp)
mov    0x2e88(%rip),%r15
mov    (%r15),%edi
lea    0x8(%rsp),%rdx
lea    0x18(%rsp),%rcx
xor    %esi,%esi
call   1060 <clock_nanosleep@plt>
test   %eax,%eax
je     11ba <Sleep+0x7a>
mov    0x2e51(%rip),%r12
lea    0x8(%rsp),%r14
lea    0x18(%rsp),%rbx
nopl   0x0(%rax)
cmp    (%r12),%eax
jne    11c6 <Sleep+0x86>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    (%r15),%edi
xor    %esi,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <clock_nanosleep@plt>
test   %eax,%eax
jne    1190 <Sleep+0x50>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%edi
lea    0xe2a(%rip),%rsi
mov    %eax,%edx
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@EINTR = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"clock_nanosleep() failed: SleepResult = %d\0A\00"
define void @Sleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
store i64 %0, i64* %2
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 0, i64* %6
%7 = load i64, i64* %2
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %7, i64* %8
br label %9
9: ; preds = %25, %1
%10 = load i32, i32* @CLOCK_MONOTONIC
%11 = call i32 @clock_nanosleep(i32 %10, i32 0, %struct.timespec* %3, %struct.timespec* %4)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
br label %27
15: ; preds = %9
%16 = load i32, i32* %5
%17 = load i32, i32* @EINTR
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = load i32, i32* %5
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
br label %25
25: ; preds = %24
%26 = call i32 @memcpy(%struct.timespec* %3, %struct.timespec* %4, i32 16)
br label %9
27: ; preds = %14
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@CLOCK_MONOTONIC = global i32 0
@EINTR = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"clock_nanosleep() failed: SleepResult = %d\0A\00"
define void @Sleep(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* %2, i64 0, i32 1
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %0, i64* %7
%8 = load i32, i32* @CLOCK_MONOTONIC
%9 = call i32 @clock_nanosleep(i32 %8, i32 0, %struct.timespec* %2, %struct.timespec* %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %24, label %11
11: ; preds = %1, %19
%12 = phi i32 [ %22, %19 ], [ %9, %1 ]
%13 = load i32, i32* @EINTR
%14 = icmp eq i32 %12, %13
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %12)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = call i32 @memcpy(%struct.timespec* %2, %struct.timespec* %3, i32 16)
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = call i32 @clock_nanosleep(i32 %21, i32 0, %struct.timespec* %2, %struct.timespec* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %11
24: ; preds = %19, %1
ret void
}