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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int TIMER_ABSTIME;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ clock_nanosleep(int /*<<< orphan*/, int, struct timespec *,
                           int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(void) {
  struct timespec tssleep, tsbefore, tsafter;
  time_t sleepuntilsec;
  int flags = 0;
  if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
    perror("clock_gettime() did not return success\n");
    return 2;
  }
  sleepuntilsec = tsbefore.tv_sec + 3;
  tssleep.tv_sec = sleepuntilsec;
  tssleep.tv_nsec = tsbefore.tv_nsec;
  flags |= TIMER_ABSTIME;
  if (clock_nanosleep(CLOCK_REALTIME, flags, &tssleep, NULL) != 0) {
    printf("clock_nanosleep() did not return success\n");
    return 2;
  }
  if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
    perror("Error in clock_gettime()\n");
    return 2;
  }
  if (tsafter.tv_sec >= sleepuntilsec) {
    if (tsafter.tv_sec <= (sleepuntilsec + 1)) {
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("clock_nanosleep() slept too long\n");
      return 1;
    }
  }
  printf("clock_nanosleep() did not sleep long enough\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x44(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%eax
je     1189 <main+0x49>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1286 <main+0x146>
mov    -0x28(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
or     -0x44(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    -0x44(%rbp),%esi
lea    -0x18(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <clock_nanosleep@plt>
cmp    $0x0,%rax
je     11ed <main+0xad>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1286 <main+0x146>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0xe0>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1286 <main+0x146>
mov    -0x38(%rbp),%rax
cmp    -0x40(%rbp),%rax
jl     1273 <main+0x133>
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jg     125b <main+0x11b>
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1286 <main+0x146>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1286 <main+0x146>
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e88(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
test   %eax,%eax
jne    1201 <main+0xc1>
mov    (%rsp),%r15
lea    0x3(%r15),%rbx
mov    %rbx,0x10(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x18(%rsp)
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    (%r14),%edi
lea    0x10(%rsp),%rdx
xor    %ecx,%ecx
call   1060 <clock_nanosleep@plt>
test   %rax,%rax
je     11a8 <main+0x68>
lea    0xeff(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    11f7 <main+0xb7>
mov    (%r14),%edi
lea    0x20(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     120a <main+0xca>
mov    0x20(%rsp),%rax
cmp    %rbx,%rax
jge    11cd <main+0x8d>
lea    0xe77(%rip),%rdi
jmp    11dd <main+0x9d>
add    $0x4,%r15
cmp    %r15,%rax
jle    11e9 <main+0xa9>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11f7 <main+0xb7>
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdf8(%rip),%rdi
jmp    1211 <main+0xd1>
lea    0xe17(%rip),%rdi
call   1050 <perror@plt>
jmp    11a1 <main+0x61>

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [40 x i8] c"clock_gettime() did not return success\0A\00"
@TIMER_ABSTIME = global i32 0
@.str.1 = [42 x i8] c"clock_nanosleep() did not return success\0A\00"
@.str.2 = [26 x i8] c"Error in clock_gettime()\0A\00"
@.str.3 = [13 x i8] c"Test PASSED\0A\00"
@.str.4 = [34 x i8] c"clock_nanosleep() slept too long\0A\00"
@.str.5 = [45 x i8] c"clock_nanosleep() did not sleep long enough\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %3)
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %53
12: ; preds = %0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%14 = load i64, i64* %13
%15 = add nsw i64 %14, 3
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i64 %16, i64* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @TIMER_ABSTIME
%22 = load i32, i32* %6
%23 = or i32 %22, %21
store i32 %23, i32* %6
%24 = load i32, i32* @CLOCK_REALTIME
%25 = load i32, i32* %6
%26 = call i64 @clock_nanosleep(i32 %24, i32 %25, %struct.timespec* %2, i32* null)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %12
%29 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %53
30: ; preds = %12
%31 = load i32, i32* @CLOCK_REALTIME
%32 = call i32 @clock_gettime(i32 %31, %struct.timespec* %4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %53
36: ; preds = %30
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%38 = load i64, i64* %37
%39 = load i64, i64* %5
%40 = icmp sge i64 %38, %39
br i1 %40, label %41, label %51
41: ; preds = %36
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%43 = load i64, i64* %42
%44 = load i64, i64* %5
%45 = add nsw i64 %44, 1
%46 = icmp sle i64 %43, %45
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %53
49: ; preds = %41
%50 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %53
51: ; preds = %36
%52 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %53
53: ; preds = %51, %49, %47, %34, %28, %10
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_REALTIME = global i32 0
@.str = [40 x i8] c"clock_gettime() did not return success\0A\00"
@TIMER_ABSTIME = global i32 0
@.str.2 = [26 x i8] c"Error in clock_gettime()\0A\00"
@str = [44 x i8] c"clock_nanosleep() did not sleep long enough\00"
@str.6 = [33 x i8] c"clock_nanosleep() slept too long\00"
@str.7 = [12 x i8] c"Test PASSED\00"
@str.8 = [41 x i8] c"clock_nanosleep() did not return success\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %1 to i8*
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast %struct.timespec* %3 to i8*
%7 = load i32, i32* @CLOCK_REALTIME
%8 = call i32 @clock_gettime(i32 %7, %struct.timespec* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %45
12: ; preds = %0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = add nsw i64 %14, 3
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i64 %15, i64* %16
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @TIMER_ABSTIME
%21 = load i32, i32* @CLOCK_REALTIME
%22 = call i64 @clock_nanosleep(i32 %21, i32 %20, %struct.timespec* %1, i32* null)
%23 = icmp eq i64 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %12
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0))
br label %45
26: ; preds = %12
%27 = load i32, i32* @CLOCK_REALTIME
%28 = call i32 @clock_gettime(i32 %27, %struct.timespec* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %45
32: ; preds = %26
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%34 = load i64, i64* %33
%35 = icmp slt i64 %34, %15
br i1 %35, label %43, label %36
36: ; preds = %32
%37 = add nsw i64 %14, 4
%38 = icmp sgt i64 %34, %37
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %45
41: ; preds = %36
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.6, i64 0, i64 0))
br label %45
43: ; preds = %32
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %45
45: ; preds = %43, %41, %39, %30, %24, %10
%46 = phi i32 [ 2, %10 ], [ 2, %24 ], [ 2, %30 ], [ 0, %39 ], [ 1, %41 ], [ 1, %43 ]
ret i32 %46
}