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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ memcpy(struct timespec *, struct timespec *, int);
int nanosleep(struct timespec *, struct timespec *);
int delay(int64_t ns) {
  struct timespec req, rem;
  req.tv_sec = 0;
  while (ns >= 1000000000L) {
    ns -= 1000000000L;
    req.tv_sec += 1;
  }
  req.tv_nsec = ns;
  while (nanosleep(&req, &rem) == -1)
    if (EINTR == errno)
      memcpy(&req, &rem, sizeof(rem));
    else
      return -1;
  return 0;
}


x86 assembly (clang, -O0):
<delay>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x3b9aca00,-0x10(%rbp)
3b
jl     1161 <delay+0x41>
mov    -0x10(%rbp),%rax
sub    $0x3b9aca00,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1134 <delay+0x14>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11c1 <delay+0xa1>
mov    0x2e52(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
jne    11b0 <delay+0x90>
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
jmp    11bc <delay+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c8 <delay+0xa8>
jmp    1169 <delay+0x49>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delay>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
xor    %r14d,%r14d
cmp    $0x3b9aca00,%rdi
jl     1181 <delay+0x61>
cmp    $0x773593ff,%rdi
mov    $0x773593ff,%eax
cmovl  %rdi,%rax
neg    %rax
add    %rdi,%rax
add    $0x3b9ac9ff,%rax
shr    $0x9,%rax
movabs $0x44b82fa09b5a53,%rcx
mul    %rcx
shr    $0xb,%rdx
imul   $0xffffffffc4653600,%rdx,%rax
add    %rax,%rdi
add    $0xffffffffc4653600,%rdi
add    $0x1,%rdx
jmp    1183 <delay+0x63>
xor    %edx,%edx
mov    %rdx,0x8(%rsp)
mov    %rdi,(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
jne    11f2 <delay+0xd2>
mov    0x2e33(%rip),%r12
mov    0x2e34(%rip),%r13
mov    %rsp,%r15
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    11ec <delay+0xcc>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <nanosleep@plt>
cmp    $0xffffffff,%eax
je     11c0 <delay+0xa0>
jmp    11f2 <delay+0xd2>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@EINTR = global i64 0
@errno = global i64 0
define i32 @delay(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
store i64 %0, i64* %3
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
store i64 0, i64* %6
br label %7
7: ; preds = %10, %1
%8 = load i64, i64* %3
%9 = icmp sge i64 %8, 1000000000
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = load i64, i64* %3
%12 = sub nsw i64 %11, 1000000000
store i64 %12, i64* %3
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%14 = load i64, i64* %13
%15 = add nsw i64 %14, 1
store i64 %15, i64* %13
br label %7
16: ; preds = %7
%17 = load i64, i64* %3
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
store i64 %17, i64* %18
br label %19
19: ; preds = %29, %16
%20 = call i32 @nanosleep(%struct.timespec* %4, %struct.timespec* %5)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i64, i64* @EINTR
%24 = load i64, i64* @errno
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @memcpy(%struct.timespec* %4, %struct.timespec* %5, i32 16)
br label %29
28: ; preds = %22
store i32 -1, i32* %2
br label %31
29: ; preds = %26
br label %19
30: ; preds = %19
store i32 0, i32* %2
br label %31
31: ; preds = %30, %28
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@EINTR = global i64 0
@errno = global i64 0
define i32 @delay(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
%7 = icmp sgt i64 %0, 999999999
br i1 %7, label %8, label %17
8: ; preds = %1
%9 = add nsw i64 %0, -1000000000
%10 = add nuw i64 %0, 999999999
%11 = call i64 @llvm.smin.i64(i64 %0, i64 1999999999)
%12 = sub nuw i64 %10, %11
%13 = udiv i64 %12, 1000000000
%14 = mul nsw i64 %13, -1000000000
%15 = add nsw i64 %14, %9
%16 = add nuw nsw i64 %13, 1
br label %17
17: ; preds = %8, %1
%18 = phi i64 [ 0, %1 ], [ %16, %8 ]
%19 = phi i64 [ %0, %1 ], [ %15, %8 ]
store i64 %18, i64* %6
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i64 %19, i64* %20
%21 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %31
23: ; preds = %17, %27
%24 = load i64, i64* @EINTR
%25 = load i64, i64* @errno
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = call i32 @memcpy(%struct.timespec* %2, %struct.timespec* %3, i32 16)
%29 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%30 = icmp eq i32 %29, -1
br i1 %30, label %23, label %31
31: ; preds = %23, %27, %17
%32 = phi i32 [ 0, %17 ], [ 0, %27 ], [ -1, %23 ]
ret i32 %32
}