#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 >= tsbefore.tv_sec) {
    if (tsafter.tv_sec <= (tsbefore.tv_sec + 1)) {
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("FAIL:  clock_nanosleep() slept\n");
      return 1;
    }
  }
  printf("FAIL:  error in clock_gettime()\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
sub    $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    -0x28(%rbp),%rax
jl     1273 <main+0x133>
mov    -0x38(%rbp),%rax
mov    -0x28(%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    0xe1f(%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   %rbx
sub    $0x30,%rsp
mov    0x2e8c(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1040 <clock_gettime@plt>
test   %eax,%eax
jne    11fa <main+0xba>
mov    (%rsp),%rax
add    $0xfffffffffffffffd,%rax
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x18(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    (%rbx),%edi
lea    0x10(%rsp),%rdx
xor    %ecx,%ecx
call   1060 <clock_nanosleep@plt>
test   %rax,%rax
je     11a2 <main+0x62>
lea    0xef7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    11f4 <main+0xb4>
mov    (%rbx),%edi
lea    0x20(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     1203 <main+0xc3>
mov    0x20(%rsp),%rax
mov    (%rsp),%rcx
cmp    %rcx,%rax
jge    11ca <main+0x8a>
lea    0xe7a(%rip),%rdi
jmp    11da <main+0x9a>
add    $0x1,%rcx
cmp    %rcx,%rax
jle    11e6 <main+0xa6>
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11f4 <main+0xb4>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xdff(%rip),%rdi
jmp    120a <main+0xca>
lea    0xe1e(%rip),%rdi
call   1050 <perror@plt>
jmp    119b <main+0x5b>

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 = [32 x i8] c"FAIL: clock_nanosleep() slept\0A\00"
@.str.5 = [33 x i8] c"FAIL: error in clock_gettime()\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 %55
12: ; preds = %0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%14 = load i64, i64* %13
%15 = sub 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 %55
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 %55
36: ; preds = %30
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%40 = load i64, i64* %39
%41 = icmp sge i64 %38, %40
br i1 %41, label %42, label %53
42: ; preds = %36
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%46 = load i64, i64* %45
%47 = add nsw i64 %46, 1
%48 = icmp sle i64 %44, %47
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = 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 %55
51: ; preds = %42
%52 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %55
53: ; preds = %36
%54 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %55
55: ; preds = %53, %51, %49, %34, %28, %10
%56 = load i32, i32* %1
ret i32 %56
}

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 = [32 x i8] c"FAIL: error in clock_gettime()\00"
@str.6 = [31 x i8] c"FAIL: clock_nanosleep() slept\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 %46
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 %46
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 %46
32: ; preds = %26
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%34 = load i64, i64* %33
%35 = load i64, i64* %13
%36 = icmp slt i64 %34, %35
br i1 %36, label %44, label %37
37: ; preds = %32
%38 = add nsw i64 %35, 1
%39 = icmp sgt i64 %34, %38
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
br label %46
42: ; preds = %37
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.6, i64 0, i64 0))
br label %46
44: ; preds = %32
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %46
46: ; preds = %44, %42, %40, %30, %24, %10
%47 = phi i32 [ 2, %10 ], [ 2, %24 ], [ 2, %30 ], [ 0, %40 ], [ 1, %42 ], [ 1, %44 ]
ret i32 %47
}