#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 >= sleepuntilsec) {
    if (tsafter.tv_sec <= (sleepuntilsec + 1)) {
      printf("Test PASSED\n");
      return 0;
    } else {
      printf("clock_nanosleep() slept too long\n");
      return 1;
    }
  } else {
    printf("clock_nanosleep() did not sleep long enough\n");
    return 1;
  }
  printf("This code should not be executed.\n");
  return 2;
}


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
add    $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    -0x24(%rbp),%eax
jl     1268 <main+0x128>
mov    -0x20(%rbp),%eax
mov    -0x24(%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    0xe2c(%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   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e89(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %eax,%eax
jne    11fb <main+0xbb>
mov    0x8(%rsp),%ebx
mov    0xc(%rsp),%eax
lea    0x3(%rbx),%ebp
mov    %ebp,0x10(%rsp)
mov    %eax,0x14(%rsp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    (%r14),%edi
lea    0x10(%rsp),%rdx
xor    %ecx,%ecx
call   1060 <clock_nanosleep@plt>
test   %rax,%rax
je     11a7 <main+0x67>
lea    0xf00(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    11f2 <main+0xb2>
mov    (%r14),%edi
lea    0x18(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     1204 <main+0xc4>
mov    0x18(%rsp),%eax
cmp    %ebp,%eax
jge    11ca <main+0x8a>
lea    0xe7a(%rip),%rdi
jmp    11d8 <main+0x98>
add    $0x4,%ebx
cmp    %ebx,%eax
jle    11e4 <main+0xa4>
lea    0xe96(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    11f2 <main+0xb2>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
jmp    120b <main+0xcb>
lea    0xe1d(%rip),%rdi
call   1050 <perror@plt>
jmp    11a0 <main+0x60>

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 = [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 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 %53
12: ; preds = %0
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = add 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 %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 i32, i32* %37
%39 = load i32, i32* %5
%40 = icmp sge i32 %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 i32, i32* %42
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
%46 = icmp sle i32 %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 { 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 = [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 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 %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 i32, i32* %33
%35 = icmp slt i32 %34, %15
br i1 %35, label %43, label %36
36: ; preds = %32
%37 = add nsw i32 %14, 4
%38 = icmp sgt i32 %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
}