#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 tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
  int sleepnsec[13] = {1,         2,         10,       100,       1000,
                       10000,     1000000,   10000000, 100000000, 200000000,
                       500000000, 750000000, 999999900};
  int i;
  int failure = 0;
  int slepts, sleptns;
  if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
    perror("Error in clock_gettime()\n");
    return 2;
  }
  tssleepfor.tv_sec = 0;
  for (i = 0; i < 13; i++) {
    tssleepfor.tv_nsec = sleepnsec[i];
    if (nanosleep(&tssleepfor, &tsstorage) != 0) {
      printf("nanosleep() did not return success\n");
      return 2;
    }
    if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
      perror("Error in clock_gettime()\n");
      return 2;
    }
    slepts = tsafter.tv_sec - tsbefore.tv_sec;
    sleptns = tsafter.tv_nsec - tsbefore.tv_nsec;
    if (sleptns < 0) {
      sleptns = sleptns + 1000000000;
      slepts = slepts - 1;
    }
    if (slepts >= 1 || sleptns > sleepnsec[i]) {
      printf("PASS slept %ds %dns >= %d\n", slepts, sleptns, sleepnsec[i]);
    } else {
      printf("FAIL slept %ds %dns < %d\n", slepts, sleptns, sleepnsec[i]);
      failure = 1;
    }
  }
  if (failure) {
    printf("At least one test FAILED\n");
    return 1;
  }
  printf("All tests PASSED\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe96(%rip),%rsi
mov    $0x34,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x68(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    11ae <main+0x5e>
lea    0xe97(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1310 <main+0x1c0>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x64(%rbp)
cmpl   $0xd,-0x64(%rbp)
jge    12d7 <main+0x187>
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <nanosleep@plt>
cmp    $0x0,%rax
je     1202 <main+0xb2>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1310 <main+0x1c0>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0xffffffff,%eax
jne    1235 <main+0xe5>
lea    0xe10(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1310 <main+0x1c0>
mov    -0x28(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x24(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jge    1265 <main+0x115>
mov    -0x70(%rbp),%eax
add    $0x3b9aca00,%eax
mov    %eax,-0x70(%rbp)
mov    -0x6c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x6c(%rbp)
cmpl   $0x1,-0x6c(%rbp)
jge    1280 <main+0x130>
mov    -0x70(%rbp),%eax
movslq -0x64(%rbp),%rcx
cmp    -0x60(%rbp,%rcx,4),%eax
jle    12a1 <main+0x151>
mov    -0x6c(%rbp),%esi
mov    -0x70(%rbp),%edx
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%ecx
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c4 <main+0x174>
mov    -0x6c(%rbp),%esi
mov    -0x70(%rbp),%edx
movslq -0x64(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%ecx
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x68(%rbp)
jmp    12c9 <main+0x179>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    11bc <main+0x6c>
cmpl   $0x0,-0x68(%rbp)
je     12fb <main+0x1ab>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1310 <main+0x1c0>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     129b <main+0x14b>
movl   $0x0,0x18(%rsp)
mov    $0x1,%bl
lea    0xe75(%rip),%rax
lea    0x18(%rsp),%r14
lea    0x20(%rsp),%r15
xor    %r13d,%r13d
mov    %ebx,0x4(%rsp)
lea    (%rax,%r13,4),%r12
add    $0x1,%r13
lea    0x8(%rsp),%rbx
jmp    11d2 <main+0x82>
nopl   0x0(%rax,%rax,1)
lea    0xe97(%rip),%rdi
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r13
add    $0x4,%r12
cmp    $0xe,%r13
je     1264 <main+0x114>
mov    (%r12),%ebp
mov    %ebp,0x1c(%rsp)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <nanosleep@plt>
test   %rax,%rax
jne    1251 <main+0x101>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1040 <clock_gettime@plt>
cmp    $0xffffffff,%eax
je     129b <main+0x14b>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
sub    0x10(%rsp),%eax
sub    0x14(%rsp),%ecx
lea    0x3b9aca00(%rcx),%edx
mov    %ecx,%esi
sar    $0x1f,%esi
test   %ecx,%ecx
cmovns %ecx,%edx
add    %eax,%esi
jg     11b0 <main+0x60>
cmp    %ebp,%edx
jg     11b0 <main+0x60>
xor    %ebx,%ebx
lea    0xe34(%rip),%rdi
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xd,%r13
lea    0xdb7(%rip),%rax
jne    1198 <main+0x48>
jmp    127b <main+0x12b>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
jmp    128c <main+0x13c>
testb  $0x1,0x4(%rsp)
je     127b <main+0x12b>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    128c <main+0x13c>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
jmp    125d <main+0x10d>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@__const.main.sleepnsec = [13 x i32] [i32 1, i32 2, i32 10, i32 100, i32 1000, i32 10000, i32 1000000, i32 10000000, i32 100000000, i32 200000000, i32 500000000, i32 750000000, i32 999999900]
@CLOCK_REALTIME = global i32 0
@.str = [26 x i8] c"Error in clock_gettime()\0A\00"
@.str.1 = [36 x i8] c"nanosleep() did not return success\0A\00"
@.str.2 = [27 x i8] c"PASS slept %ds %dns >= %d\0A\00"
@.str.3 = [26 x i8] c"FAIL slept %ds %dns < %d\0A\00"
@.str.4 = [26 x i8] c"At least one test FAILED\0A\00"
@.str.5 = [18 x i8] c"All tests PASSED\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca %struct.timespec
%6 = alloca [13 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = bitcast [13 x i32]* %6 to i8*
store i32 0, i32* %8
%12 = load i32, i32* @CLOCK_REALTIME
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %93
17: ; preds = %0
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
store i32 0, i32* %18
store i32 0, i32* %7
br label %19
19: ; preds = %83, %17
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 13
br i1 %21, label %22, label %86
22: ; preds = %19
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [13 x i32], [13 x i32]* %6, i64 0, i64 %24
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 1
store i32 %26, i32* %27
%28 = call i64 @nanosleep(%struct.timespec* %2, %struct.timespec* %3)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %93
32: ; preds = %22
%33 = load i32, i32* @CLOCK_REALTIME
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %5)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %93
38: ; preds = %32
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 0
%42 = load i32, i32* %41
%43 = sub nsw i32 %40, %42
store i32 %43, i32* %9
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i32 0, i32 1
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i32 0, i32 1
%47 = load i32, i32* %46
%48 = sub nsw i32 %45, %47
store i32 %48, i32* %10
%49 = load i32, i32* %10
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %38
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 1000000000
store i32 %53, i32* %10
%54 = load i32, i32* %9
%55 = sub nsw i32 %54, 1
store i32 %55, i32* %9
br label %56
56: ; preds = %51, %38
%57 = load i32, i32* %9
%58 = icmp sge i32 %57, 1
br i1 %58, label %66, label %59
59: ; preds = %56
%60 = load i32, i32* %10
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [13 x i32], [13 x i32]* %6, i64 0, i64 %62
%64 = load i32, i32* %63
%65 = icmp sgt i32 %60, %64
br i1 %65, label %66, label %74
66: ; preds = %59, %56
%67 = load i32, i32* %9
%68 = load i32, i32* %10
%69 = load i32, i32* %7
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [13 x i32], [13 x i32]* %6, i64 0, i64 %70
%72 = load i32, i32* %71
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %67, i32 %68, i32 %72)
br label %82
74: ; preds = %59
%75 = load i32, i32* %9
%76 = load i32, i32* %10
%77 = load i32, i32* %7
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [13 x i32], [13 x i32]* %6, i64 0, i64 %78
%80 = load i32, i32* %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %75, i32 %76, i32 %80)
store i32 1, i32* %8
br label %82
82: ; preds = %74, %66
br label %83
83: ; preds = %82
%84 = load i32, i32* %7
%85 = add nsw i32 %84, 1
store i32 %85, i32* %7
br label %19
86: ; preds = %19
%87 = load i32, i32* %8
%88 = icmp ne i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %86
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %93
91: ; preds = %86
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %93
93: ; preds = %91, %89, %36, %30, %15
%94 = load i32, i32* %1
ret i32 %94
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@__const.main.sleepnsec = [13 x i32] [i32 1, i32 2, i32 10, i32 100, i32 1000, i32 10000, i32 1000000, i32 10000000, i32 100000000, i32 200000000, i32 500000000, i32 750000000, i32 999999900]
@CLOCK_REALTIME = global i32 0
@.str = [26 x i8] c"Error in clock_gettime()\0A\00"
@.str.2 = [27 x i8] c"PASS slept %ds %dns >= %d\0A\00"
@.str.3 = [26 x i8] c"FAIL slept %ds %dns < %d\0A\00"
@str = [17 x i8] c"All tests PASSED\00"
@str.6 = [25 x i8] c"At least one test FAILED\00"
@str.7 = [35 x i8] c"nanosleep() did not return success\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timespec
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = bitcast %struct.timespec* %1 to i8*
%6 = bitcast %struct.timespec* %2 to i8*
%7 = bitcast %struct.timespec* %3 to i8*
%8 = bitcast %struct.timespec* %4 to i8*
%9 = load i32, i32* @CLOCK_REALTIME
%10 = call i32 @clock_gettime(i32 %9, %struct.timespec* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %66
14: ; preds = %0
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 1
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
br label %21
21: ; preds = %57, %14
%22 = phi i64 [ %59, %57 ], [ 0, %14 ]
%23 = phi i1 [ false, %57 ], [ true, %14 ]
br label %24
24: ; preds = %21, %53
%25 = phi i64 [ %55, %53 ], [ %22, %21 ]
%26 = getelementptr inbounds [13 x i32], [13 x i32]* @__const.main.sleepnsec, i64 0, i64 %25
%27 = load i32, i32* %26
store i32 %27, i32* %16
%28 = call i64 @nanosleep(%struct.timespec* %1, %struct.timespec* %2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.7, i64 0, i64 0))
br label %66
32: ; preds = %24
%33 = load i32, i32* @CLOCK_REALTIME
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %4)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %66
38: ; preds = %32
%39 = load i32, i32* %17
%40 = load i32, i32* %18
%41 = sub nsw i32 %39, %40
%42 = load i32, i32* %19
%43 = load i32, i32* %20
%44 = sub nsw i32 %42, %43
%45 = icmp slt i32 %44, 0
%46 = add nsw i32 %44, 1000000000
%47 = ashr i32 %44, 31
%48 = add nsw i32 %47, %41
%49 = select i1 %45, i32 %46, i32 %44
%50 = icmp sgt i32 %48, 0
%51 = icmp sgt i32 %49, %27
%52 = select i1 %50, i1 true, i1 %51
br i1 %52, label %53, label %57
53: ; preds = %38
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %49, i32 %27)
%55 = add nuw nsw i64 %25, 1
%56 = icmp eq i64 %55, 13
br i1 %56, label %61, label %24
57: ; preds = %38
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %48, i32 %49, i32 %27)
%59 = add nuw nsw i64 %25, 1
%60 = icmp eq i64 %59, 13
br i1 %60, label %62, label %21
61: ; preds = %53
br i1 %23, label %64, label %62
62: ; preds = %57, %61
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
br label %66
64: ; preds = %61
%65 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %66
66: ; preds = %64, %62, %36, %30, %12
%67 = phi i32 [ 2, %12 ], [ 2, %30 ], [ 2, %36 ], [ 1, %62 ], [ 0, %64 ]
ret i32 %67
}