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

/* Variables and functions */
int nanosleep(struct timespec *, struct timespec *);
int sleep_ns(size_t time) {
  struct timespec t, t2;
  if (time > 999999999) {
    t.tv_sec = time / 999999999;
    time %= 999999999;
  } else {
    t.tv_sec = 0;
  }
  t.tv_nsec = time;
  return nanosleep(&t, &t2);
}


x86 assembly (clang, -O0):
<sleep_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x3b9ac9ff,-0x8(%rbp)
3b
jbe    1153 <sleep_ns+0x43>
mov    -0x8(%rbp),%rax
mov    $0x3b9ac9ff,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3b9ac9ff,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x8(%rbp)
jmp    115b <sleep_ns+0x4b>
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <nanosleep@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_ns>:
cmp    $0x3b9aca00,%rdi
jb     1145 <sleep_ns+0x35>
movabs $0x12e0be870a00bfb7,%rcx
be e0 12
mov    %rdi,%rax
mul    %rcx
mov    %rdi,%rax
sub    %rdx,%rax
shr    %rax
add    %rdx,%rax
shr    $0x1d,%rax
imul   $0x3b9ac9ff,%rax,%rcx
sub    %rcx,%rdi
jmp    1147 <sleep_ns+0x37>
xor    %eax,%eax
sub    $0x28,%rsp
mov    %rax,0x8(%rsp)
mov    %rdi,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   1030 <nanosleep@plt>
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
define i32 @sleep_ns(i64 %0) {
%2 = alloca i64
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = icmp ugt i64 %5, 999999999
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i64, i64* %2
%9 = udiv i64 %8, 999999999
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 %9, i64* %10
%11 = load i64, i64* %2
%12 = urem i64 %11, 999999999
store i64 %12, i64* %2
br label %15
13: ; preds = %1
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i64 0, i64* %14
br label %15
15: ; preds = %13, %7
%16 = load i64, i64* %2
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 %16, i64* %17
%18 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %4)
ret i32 %18
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
define i32 @sleep_ns(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 = icmp ugt i64 %0, 999999999
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = udiv i64 %0, 999999999
%9 = urem i64 %0, 999999999
br label %10
10: ; preds = %1, %7
%11 = phi i64 [ %8, %7 ], [ 0, %1 ]
%12 = phi i64 [ %9, %7 ], [ %0, %1 ]
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %11, i64* %13
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 %12, i64* %14
%15 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
ret i32 %15
}