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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int atoi(char *);
scalar_t__ clock_getres(int /*<<< orphan*/, struct timespec *);
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
int errno;
int /*<<< orphan*/ poll(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, ...);
char *strerror(int);
int main(int argc, char *argv[]) {
  struct timespec cur;
  struct timespec prev;
  unsigned int ticks;
  int delay;
  if (argc > 1) {
    delay = atoi(argv[1]);
  } else {
    delay = 1;
  }
  printf("Using delay=%u milliseconds between calls.\n", delay);
  if (clock_getres(CLOCK_MONOTONIC, &cur) != 0) {
    printf("Failed clock resolution read errno=%d [%s].\n", errno,
           strerror(errno));
    return (0);
  }
  printf("Clock resolution sec=%lu nsec=%lu\n", cur.tv_sec, cur.tv_nsec);
  if (clock_gettime(CLOCK_MONOTONIC, &cur) != 0) {
    printf("Failed initial read errno=%d [%s].\n", errno, strerror(errno));
    return (0);
  }
  printf("Initial time sec=%lu nsec=%lu\n", cur.tv_sec, cur.tv_nsec);
  ticks = 0;
  while (1) {
    prev = cur;
    poll(0, 0, delay);
    if (clock_gettime(CLOCK_MONOTONIC, &cur) != 0) {
      printf("Failed subsequent read errno=%d [%s].\n", errno, strerror(errno));
      return (0);
    }
    if ((cur.tv_sec <= prev.tv_sec) && (cur.tv_nsec < prev.tv_nsec)) {
      printf("Time ran backward:\n\tcur:\t%lu%lu\n\tprev:\t%lu %lu\nInterval "
             "is >= %u milliseconds).\n",
             cur.tv_sec, cur.tv_nsec, prev.tv_sec, prev.tv_nsec, delay);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1195 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x38(%rbp)
jmp    119c <main+0x3c>
movl   $0x1,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <clock_getres@plt>
cmp    $0x0,%rax
je     1203 <main+0xa3>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %rax,%rdx
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133b <main+0x1db>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     126e <main+0x10e>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x40(%rbp),%esi
mov    %rax,%rdx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133b <main+0x1db>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%edx
xor    %esi,%esi
mov    %esi,%edi
call   1060 <poll@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
je     12fc <main+0x19c>
mov    0x2d0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%esi
mov    %rax,%rdx
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    133b <main+0x1db>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
jg     1336 <main+0x1d6>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    1336 <main+0x1d6>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%ecx
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9d
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128a <main+0x12a>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x1,%r12d
cmp    $0x2,%edi
jl     1185 <main+0x25>
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r12d
lea    0xe74(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e4b(%rip),%r15
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
call   1050 <clock_getres@plt>
test   %rax,%rax
je     11cb <main+0x6b>
mov    0x2e22(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe66(%rip),%rdi
jmp    1262 <main+0x102>
mov    0x10(%rsp),%esi
mov    0x8(%rsp),%rdx
lea    0xe7e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     120d <main+0xad>
mov    0x2ddd(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe71(%rip),%rdi
jmp    1262 <main+0x102>
mov    0x10(%rsp),%esi
mov    0x8(%rsp),%rdx
lea    0xe83(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%r13
mov    0x10(%rsp),%ebp
xor    %edi,%edi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <poll@plt>
mov    (%r15),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
je     127f <main+0x11f>
mov    0x2d86(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe5d(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%r14
jmp    12b3 <main+0x153>
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%r13
xor    %edi,%edi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <poll@plt>
mov    (%r15),%edi
mov    %r14,%rsi
call   1030 <clock_gettime@plt>
mov    %ebx,%ebp
test   %rax,%rax
jne    124b <main+0xeb>
mov    0x10(%rsp),%ebx
cmp    %ebp,%ebx
jg     1290 <main+0x130>
mov    0x8(%rsp),%rdx
cmp    %r13,%rdx
jge    1290 <main+0x130>
lea    0xe1a(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%ecx
mov    %r13,%r8
mov    %r12d,%r9d
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x10(%rsp),%ebx
jmp    1290 <main+0x130>

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@.str = [44 x i8] c"Using delay=%u milliseconds between calls.\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [45 x i8] c"Failed clock resolution read errno=%d [%s].\0A\00"
@errno = global i32 0
@.str.2 = [35 x i8] c"Clock resolution sec=%lu nsec=%lu\0A\00"
@.str.3 = [36 x i8] c"Failed initial read errno=%d [%s].\0A\00"
@.str.4 = [31 x i8] c"Initial time sec=%lu nsec=%lu\0A\00"
@.str.5 = [39 x i8] c"Failed subsequent read errno=%d [%s].\0A\00"
@.str.6 = [81 x i8] c"Time ran backward:\0A\09cur:\09%lu%lu\0A\09prev:\09%lu %lu\0AInterval is >= %u milliseconds).\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %9
br label %18
17: ; preds = %2
store i32 1, i32* %9
br label %18
18: ; preds = %17, %12
%19 = load i32, i32* %9
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @CLOCK_MONOTONIC
%22 = call i64 @clock_getres(i32 %21, %struct.timespec* %6)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i32, i32* @errno
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %25, i8* %27)
store i32 0, i32* %3
br label %86
29: ; preds = %18
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%33 = load i64, i64* %32
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %31, i64 %33)
%35 = load i32, i32* @CLOCK_MONOTONIC
%36 = call i64 @clock_gettime(i32 %35, %struct.timespec* %6)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %29
%39 = load i32, i32* @errno
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %39, i8* %41)
store i32 0, i32* %3
br label %86
43: ; preds = %29
%44 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%47 = load i64, i64* %46
%48 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %45, i64 %47)
store i32 0, i32* %8
br label %49
49: ; preds = %43, %85
%50 = bitcast %struct.timespec* %7 to i8*
%51 = bitcast %struct.timespec* %6 to i8*
%52 = load i32, i32* %9
%53 = call i32 @poll(i32 0, i32 0, i32 %52)
%54 = load i32, i32* @CLOCK_MONOTONIC
%55 = call i64 @clock_gettime(i32 %54, %struct.timespec* %6)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %62
57: ; preds = %49
%58 = load i32, i32* @errno
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %58, i8* %60)
store i32 0, i32* %3
br label %86
62: ; preds = %49
%63 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%64 = load i32, i32* %63
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%66 = load i32, i32* %65
%67 = icmp sle i32 %64, %66
br i1 %67, label %68, label %85
68: ; preds = %62
%69 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%70 = load i64, i64* %69
%71 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%72 = load i64, i64* %71
%73 = icmp slt i64 %70, %72
br i1 %73, label %74, label %85
74: ; preds = %68
%75 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%76 = load i32, i32* %75
%77 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%78 = load i64, i64* %77
%79 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%80 = load i32, i32* %79
%81 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%82 = load i64, i64* %81
%83 = load i32, i32* %9
%84 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.6, i64 0, i64 0), i32 %76, i64 %78, i32 %80, i64 %82, i32 %83)
br label %85
85: ; preds = %74, %68, %62
br label %49
86: ; preds = %57, %38, %24
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@.str = [44 x i8] c"Using delay=%u milliseconds between calls.\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [45 x i8] c"Failed clock resolution read errno=%d [%s].\0A\00"
@errno = global i32 0
@.str.2 = [35 x i8] c"Clock resolution sec=%lu nsec=%lu\0A\00"
@.str.3 = [36 x i8] c"Failed initial read errno=%d [%s].\0A\00"
@.str.4 = [31 x i8] c"Initial time sec=%lu nsec=%lu\0A\00"
@.str.5 = [39 x i8] c"Failed subsequent read errno=%d [%s].\0A\00"
@.str.6 = [81 x i8] c"Time ran backward:\0A\09cur:\09%lu%lu\0A\09prev:\09%lu %lu\0AInterval is >= %u milliseconds).\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
br label %10
10: ; preds = %2, %6
%11 = phi i32 [ %9, %6 ], [ 1, %2 ]
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @CLOCK_MONOTONIC
%14 = call i64 @clock_getres(i32 %13, %struct.timespec* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %17, i8* %18)
br label %65
20: ; preds = %10
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%24 = load i64, i64* %23
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %22, i64 %24)
%26 = load i32, i32* @CLOCK_MONOTONIC
%27 = call i64 @clock_gettime(i32 %26, %struct.timespec* %3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %20
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %30, i8* %31)
br label %65
33: ; preds = %20
%34 = load i32, i32* %21
%35 = load i64, i64* %23
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %34, i64 %35)
%37 = load i64, i64* %23
%38 = load i32, i32* %21
%39 = call i32 @poll(i32 0, i32 0, i32 %11)
%40 = load i32, i32* @CLOCK_MONOTONIC
%41 = call i64 @clock_gettime(i32 %40, %struct.timespec* %3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %58, %33
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 %44, i8* %45)
br label %65
47: ; preds = %33, %58
%48 = phi i32 [ %59, %58 ], [ %38, %33 ]
%49 = phi i64 [ %60, %58 ], [ %37, %33 ]
%50 = load i32, i32* %21
%51 = icmp sgt i32 %50, %48
br i1 %51, label %58, label %52
52: ; preds = %47
%53 = load i64, i64* %23
%54 = icmp slt i64 %53, %49
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.6, i64 0, i64 0), i32 %50, i64 %53, i32 %48, i64 %49, i32 %11)
%57 = load i32, i32* %21
br label %58
58: ; preds = %55, %52, %47
%59 = phi i32 [ %57, %55 ], [ %50, %52 ], [ %50, %47 ]
%60 = load i64, i64* %23
%61 = call i32 @poll(i32 0, i32 0, i32 %11)
%62 = load i32, i32* @CLOCK_MONOTONIC
%63 = call i64 @clock_gettime(i32 %62, %struct.timespec* %3)
%64 = icmp eq i64 %63, 0
br i1 %64, label %47, label %43
65: ; preds = %43, %29, %16
ret i32 0
}