#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 {
  int 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;
  int sleepuntilsec, 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    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%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    127b <main+0x13b>
mov    -0x18(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
or     -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%esi
lea    -0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <clock_nanosleep@plt>
cmp    $0x0,%rax
je     11e8 <main+0xa8>
lea    0xe51(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    127b <main+0x13b>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    121b <main+0xdb>
lea    0xe48(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    127b <main+0x13b>
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     1268 <main+0x128>
mov    -0x20(%rbp),%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jg     1250 <main+0x110>
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    127b <main+0x13b>
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127b <main+0x13b>
lea    0xe2a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e8c(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %eax,%eax
jne    11f6 <main+0xb6>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
add    $0xfffffffd,%eax
mov    %eax,0x10(%rsp)
mov    %ecx,0x14(%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    11f0 <main+0xb0>
mov    (%rbx),%edi
lea    0x18(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     11ff <main+0xbf>
mov    0x18(%rsp),%eax
mov    0x8(%rsp),%ecx
cmp    %ecx,%eax
jge    11c8 <main+0x88>
lea    0xe7c(%rip),%rdi
jmp    11d6 <main+0x96>
add    $0x1,%ecx
cmp    %ecx,%eax
jle    11e2 <main+0xa2>
lea    0xe8c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11f0 <main+0xb0>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe03(%rip),%rdi
jmp    1206 <main+0xc6>
lea    0xe22(%rip),%rdi
call   1050 <perror@plt>
jmp    119b <main+0x5b>

LLVM IR (-O0):
%struct.timespec = type { i32, 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 i32
%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 i32, i32* %13
%15 = sub nsw i32 %14, 3
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i32 %16, i32* %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 i32, i32* %37
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%40 = load i32, i32* %39
%41 = icmp sge i32 %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 i32, i32* %43
%45 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%46 = load i32, i32* %45
%47 = add nsw i32 %46, 1
%48 = icmp sle i32 %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 { i32, 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 i32, i32* %13
%15 = add nsw i32 %14, -3
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 %15, i32* %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 i32, i32* %33
%35 = load i32, i32* %13
%36 = icmp slt i32 %34, %35
br i1 %36, label %44, label %37
37: ; preds = %32
%38 = add nsw i32 %35, 1
%39 = icmp sgt i32 %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
}