#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct timespec {
  int tv_sec;
  scalar_t__ tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int /*<<< orphan*/ TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/,
                                   struct timespec *, struct timespec *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int do_test(void) {
  struct timeval tv1;
  (void)gettimeofday(&tv1, NULL);
  struct timespec ts;
  ts.tv_sec = 1;
  ts.tv_nsec = 0;
  TEMP_FAILURE_RETRY(clock_nanosleep(CLOCK_REALTIME, 0, &ts, &ts));
  struct timeval tv2;
  (void)gettimeofday(&tv2, NULL);
  tv2.tv_sec -= tv1.tv_sec;
  tv2.tv_usec -= tv1.tv_usec;
  if (tv2.tv_usec < 0)
    --tv2.tv_sec;
  if (tv2.tv_sec < 1) {
    puts("clock_nanosleep didn't sleep long enough");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x1,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x28(%rbp),%rcx
mov    %rcx,%rdx
call   1050 <clock_nanosleep@plt>
mov    %eax,%edi
call   1060 <TEMP_FAILURE_RETRY@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    -0x18(%rbp),%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11bd <do_test+0x7d>
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x1,-0x38(%rbp)
jge    11df <do_test+0x9f>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e6 <do_test+0xa6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbx
sub    $0x30,%rsp
xor    %ebx,%ebx
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rcx
xor    %esi,%esi
mov    %rcx,%rdx
call   1050 <clock_nanosleep@plt>
mov    %eax,%edi
call   1060 <TEMP_FAILURE_RETRY@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    (%rsp),%eax
sub    0x20(%rsp),%eax
mov    0x8(%rsp),%rcx
xor    %edx,%edx
cmp    0x28(%rsp),%rcx
setl   %dl
sub    %edx,%eax
test   %eax,%eax
jg     11ba <do_test+0x7a>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [41 x i8] c"clock_nanosleep didn't sleep long enough\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca %struct.timespec
%4 = alloca %struct.timeval
%5 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 0, i64* %7
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i32 @clock_nanosleep(i32 %8, i32 0, %struct.timespec* %3, %struct.timespec* %3)
%10 = call i32 @TEMP_FAILURE_RETRY(i32 %9)
%11 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = sub nsw i32 %15, %13
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%18 = load i64, i64* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%20 = load i64, i64* %19
%21 = sub nsw i64 %20, %18
store i64 %21, i64* %19
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%23 = load i64, i64* %22
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %0
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = add nsw i32 %27, -1
store i32 %28, i32* %26
br label %29
29: ; preds = %25, %0
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%31 = load i32, i32* %30
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @puts(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %36
35: ; preds = %29
store i32 0, i32* %1
br label %36
36: ; preds = %35, %33
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.timespec = type { i32, i64 }
@CLOCK_REALTIME = global i32 0
@.str = [41 x i8] c"clock_nanosleep didn't sleep long enough\00"
define i32 @do_test() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timespec
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %1 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%6 = bitcast %struct.timespec* %2 to i8*
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 1, i32* %7
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %8
%9 = load i32, i32* @CLOCK_REALTIME
%10 = call i32 @clock_nanosleep(i32 %9, i32 0, %struct.timespec* %2, %struct.timespec* %2)
%11 = call i32 @TEMP_FAILURE_RETRY(i32 %10)
%12 = bitcast %struct.timeval* %3 to i8*
%13 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = sub nsw i32 %17, %15
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%20 = load i64, i64* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%22 = load i64, i64* %21
%23 = icmp slt i64 %22, %20
%24 = sext i1 %23 to i32
%25 = add nsw i32 %18, %24
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %29
27: ; preds = %0
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %0, %27
%30 = phi i32 [ 1, %27 ], [ 0, %0 ]
ret i32 %30
}