#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 {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ clockid_t;

/* Variables and functions */
int TIMER_ABSTIME;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ timespec_add(struct timespec *, struct timespec const *,
                                struct timespec *);
scalar_t__ timespec_diff(struct timespec const *, struct timespec *,
                         int /*<<< orphan*/ *);
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
                    struct timespec *rmtp) {
  int ret;
  struct timespec tcurrent;
  struct timespec tend;
  ret = clock_gettime(clock_id, &tcurrent);
  if (!(flags & TIMER_ABSTIME)) {
    ret = clock_gettime(clock_id, &tcurrent);
    timespec_add(&tcurrent, rqtp, &tend);
    rqtp = &tend;
  }
  while (ret == 0) {
    if (timespec_diff(rqtp, &tcurrent, NULL) <= 0) {
      if (rmtp != NULL) {
        rmtp->tv_sec = 0;
        rmtp->tv_nsec = 0;
      }
      break;
    }
    ret = clock_gettime(clock_id, &tcurrent);
  }
  return ret;
}


x86 assembly (clang, -O0):
<clock_nanosleep>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2e89(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1192 <clock_nanosleep+0x62>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rdx
call   1040 <timespec_add@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1197 <clock_nanosleep+0x67>
cmpl   $0x0,-0x1c(%rbp)
jne    11f7 <clock_nanosleep+0xc7>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <timespec_diff@plt>
cmp    $0x0,%rax
jg     11e3 <clock_nanosleep+0xb3>
cmpq   $0x0,-0x18(%rbp)
je     11de <clock_nanosleep+0xae>
mov    -0x18(%rbp),%rax
movq   $0x0,0x8(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
jmp    11f7 <clock_nanosleep+0xc7>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
jmp    1197 <clock_nanosleep+0x67>
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clock_nanosleep>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,%r12d
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    %eax,%ebx
mov    0x2e8c(%rip),%rax
test   %ebp,(%rax)
je     1166 <clock_nanosleep+0x36>
test   %ebx,%ebx
jne    11d3 <clock_nanosleep+0xa3>
jmp    1192 <clock_nanosleep+0x62>
lea    0x8(%rsp),%r13
mov    %r12d,%edi
mov    %r13,%rsi
call   1030 <clock_gettime@plt>
mov    %eax,%ebx
lea    0x18(%rsp),%rbp
mov    %r13,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1040 <timespec_add@plt>
mov    %rbp,%r15
test   %ebx,%ebx
jne    11d3 <clock_nanosleep+0xa3>
lea    0x8(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %edx,%edx
call   1050 <timespec_diff@plt>
test   %rax,%rax
jle    11c5 <clock_nanosleep+0x95>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1030 <clock_gettime@plt>
test   %eax,%eax
je     11a0 <clock_nanosleep+0x70>
mov    %eax,%ebx
jmp    11d3 <clock_nanosleep+0xa3>
xor    %ebx,%ebx
test   %r14,%r14
je     11d3 <clock_nanosleep+0xa3>
xorps  %xmm0,%xmm0
movups %xmm0,(%r14)
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@TIMER_ABSTIME = global i32 0
define i32 @clock_nanosleep(i32 %0, i32 %1, %struct.timespec* %2, %struct.timespec* writeonly %3) {
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = bitcast %struct.timespec* %5 to i8*
%8 = bitcast %struct.timespec* %6 to i8*
%9 = call i32 @clock_gettime(i32 %0, %struct.timespec* %5)
%10 = load i32, i32* @TIMER_ABSTIME
%11 = and i32 %10, %1
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = call i32 @clock_gettime(i32 %0, %struct.timespec* %5)
%15 = call i32 @timespec_add(%struct.timespec* %5, %struct.timespec* %2, %struct.timespec* %6)
br label %16
16: ; preds = %13, %4
%17 = phi %struct.timespec* [ %2, %4 ], [ %6, %13 ]
%18 = phi i32 [ %9, %4 ], [ %14, %13 ]
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %16, %27
%21 = call i64 @timespec_diff(%struct.timespec* %17, %struct.timespec* %5, i32* null)
%22 = icmp slt i64 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = icmp eq %struct.timespec* %3, null
br i1 %24, label %30, label %25
25: ; preds = %23
%26 = bitcast %struct.timespec* %3 to i8*
br label %30
27: ; preds = %20
%28 = call i32 @clock_gettime(i32 %0, %struct.timespec* %5)
%29 = icmp eq i32 %28, 0
br i1 %29, label %20, label %30
30: ; preds = %27, %16, %23, %25
%31 = phi i32 [ 0, %23 ], [ 0, %25 ], [ %18, %16 ], [ %28, %27 ]
ret i32 %31
}