#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*/ TEMP_FAILURE_RETRY(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
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(nanosleep(&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("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   1050 <gettimeofday@plt>
movl   $0x1,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x28(%rbp),%rsi
mov    %rsi,%rdi
call   1040 <nanosleep@plt>
mov    %eax,%edi
call   1060 <TEMP_FAILURE_RETRY@plt>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <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    11b2 <do_test+0x72>
mov    -0x38(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x38(%rbp)
cmpl   $0x1,-0x38(%rbp)
jge    11d4 <do_test+0x94>
lea    0xe3d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    11db <do_test+0x9b>
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   1050 <gettimeofday@plt>
movl   $0x1,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    %rdi,%rsi
call   1040 <nanosleep@plt>
mov    %eax,%edi
call   1060 <TEMP_FAILURE_RETRY@plt>
mov    %rsp,%rdi
xor    %esi,%esi
call   1050 <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     11af <do_test+0x6f>
lea    0xe5b(%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 }
@.str = [35 x i8] c"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 = call i32 @nanosleep(%struct.timespec* %3, %struct.timespec* %3)
%9 = call i32 @TEMP_FAILURE_RETRY(i32 %8)
%10 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = sub nsw i32 %14, %12
store i32 %15, i32* %13
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%17 = load i64, i64* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%19 = load i64, i64* %18
%20 = sub nsw i64 %19, %17
store i64 %20, i64* %18
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%22 = load i64, i64* %21
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %0
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = add nsw i32 %26, -1
store i32 %27, i32* %25
br label %28
28: ; preds = %24, %0
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%30 = load i32, i32* %29
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %35
34: ; preds = %28
store i32 0, i32* %1
br label %35
35: ; preds = %34, %32
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.timespec = type { i32, i64 }
@.str = [35 x i8] c"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 = call i32 @nanosleep(%struct.timespec* %2, %struct.timespec* %2)
%10 = call i32 @TEMP_FAILURE_RETRY(i32 %9)
%11 = bitcast %struct.timeval* %3 to i8*
%12 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = sub nsw i32 %16, %14
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%21 = load i64, i64* %20
%22 = icmp slt i64 %21, %19
%23 = sext i1 %22 to i32
%24 = add nsw i32 %17, %23
%25 = icmp slt i32 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %0
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %0, %26
%29 = phi i32 [ 1, %26 ], [ 0, %0 ]
ret i32 %29
}