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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ EINTR;
int /*<<< orphan*/ SIGKILL;
int /*<<< orphan*/ TIMER_ABSTIME;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ kill(int /*<<< orphan*/, int /*<<< orphan*/);
void mysleep(long long int ns) {
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
    kill(0, SIGKILL);
  }
  ts.tv_nsec += ns;
  ts.tv_sec += ts.tv_nsec / 1000000000;
  ts.tv_nsec %= 1000000000;
  while (1) {
    int ret = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL);
    if (ret == -1 && errno == EINTR)
      continue;
    if (!ret)
      break;
    kill(0, SIGKILL);
  }
}


x86 assembly (clang, -O0):
<mysleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     1168 <mysleep+0x38>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <kill@plt>
mov    -0x8(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rax,%rcx
movslq -0x10(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3b9aca00,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x18(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <clock_nanosleep@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ec <mysleep+0xbc>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <mysleep+0xbc>
jmp    11a1 <mysleep+0x71>
cmpl   $0x0,-0x1c(%rbp)
jne    11fb <mysleep+0xcb>
jmp    1210 <mysleep+0xe0>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <kill@plt>
jmp    11a1 <mysleep+0x71>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mysleep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e98(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     116a <mysleep+0x3a>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
call   1040 <kill@plt>
add    0x8(%rsp),%rbx
movabs $0x112e0be826d694b3,%rcx
0b 2e 11
mov    %rbx,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x1a,%rdx
add    %rax,%rdx
add    %edx,0x10(%rsp)
imul   $0x3b9aca00,%rdx,%rax
sub    %rax,%rbx
mov    %rbx,0x8(%rsp)
mov    0x2e41(%rip),%r15
lea    0x8(%rsp),%rbx
mov    0x2e1d(%rip),%r12
mov    0x2e0e(%rip),%r13
mov    0x2dff(%rip),%rbp
jmp    11de <mysleep+0xae>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %eax,%eax
je     11ff <mysleep+0xcf>
mov    0x0(%rbp),%esi
xor    %edi,%edi
call   1040 <kill@plt>
mov    (%r14),%edi
mov    (%r15),%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1050 <clock_nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11d0 <mysleep+0xa0>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     11de <mysleep+0xae>
jmp    11d4 <mysleep+0xa4>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
@SIGKILL = global i32 0
@TIMER_ABSTIME = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @mysleep(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca i32
store i64 %0, i64* %2
%5 = load i32, i32* @CLOCK_MONOTONIC
%6 = call i64 @clock_gettime(i32 %5, %struct.timespec* %3)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @SIGKILL
%10 = call i32 @kill(i32 0, i32 %9)
br label %11
11: ; preds = %8, %1
%12 = load i64, i64* %2
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%14 = load i64, i64* %13
%15 = add nsw i64 %14, %12
store i64 %15, i64* %13
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%17 = load i64, i64* %16
%18 = sdiv i64 %17, 1000000000
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%20 = load i32, i32* %19
%21 = sext i32 %20 to i64
%22 = add nsw i64 %21, %18
%23 = trunc i64 %22 to i32
store i32 %23, i32* %19
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%25 = load i64, i64* %24
%26 = srem i64 %25, 1000000000
store i64 %26, i64* %24
br label %27
27: ; preds = %11, %37, %42
%28 = load i32, i32* @CLOCK_MONOTONIC
%29 = load i32, i32* @TIMER_ABSTIME
%30 = call i32 @clock_nanosleep(i32 %28, i32 %29, %struct.timespec* %3, i32* null)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %27
38: ; preds = %33, %27
%39 = load i32, i32* %4
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %38
br label %45
42: ; preds = %38
%43 = load i32, i32* @SIGKILL
%44 = call i32 @kill(i32 0, i32 %43)
br label %27
45: ; preds = %41
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_MONOTONIC = global i32 0
@SIGKILL = global i32 0
@TIMER_ABSTIME = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define void @mysleep(i64 %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_MONOTONIC
%5 = call i64 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = load i32, i32* @SIGKILL
%9 = call i32 @kill(i32 0, i32 %8)
br label %10
10: ; preds = %7, %1
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%12 = load i64, i64* %11
%13 = add nsw i64 %12, %0
%14 = sdiv i64 %13, 1000000000
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = trunc i64 %14 to i32
%18 = add i32 %16, %17
store i32 %18, i32* %15
%19 = srem i64 %13, 1000000000
store i64 %19, i64* %11
br label %20
20: ; preds = %28, %10
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = load i32, i32* @TIMER_ABSTIME
%23 = call i32 @clock_nanosleep(i32 %21, i32 %22, %struct.timespec* %2, i32* null)
switch i32 %23, label %29 [
i32 -1, label %24
i32 0, label %32
]
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24, %29
br label %20
29: ; preds = %24, %20
%30 = load i32, i32* @SIGKILL
%31 = call i32 @kill(i32 0, i32 %30)
br label %28
32: ; preds = %20
ret void
}