#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 int /*<<< orphan*/ time_t;
struct tm {
  int dummy;
};
struct timespec {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
int /*<<< orphan*/ asctime(struct tm *);
int atol(char const *);
int /*<<< orphan*/ clock_getres(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int getpid();
struct tm *gmtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, const char *argv[]) {
  struct timespec TimeSpec;
  unsigned long long ResolutionNs = 0, PrevNs = 0, CurrentNs = 0;
  unsigned long long DiffNs = 0;
  unsigned long long ThresholdNs = 100000000;
  time_t Time;
  struct tm *UtcTime;
  int Cooldown = 2;
  if (argc > 1) {
    ThresholdNs = atol(argv[1]) * 1000000;
  }
  clock_getres(CLOCK_MONOTONIC_RAW, &TimeSpec);
  ResolutionNs = (unsigned long long)(TimeSpec.tv_sec) * 1000000000ULL +
                 (unsigned long long)(TimeSpec.tv_nsec);
  printf("%d: Resolution of CLOCK_MONOTONIC_RAW is %llu nsec\n", getpid(),
         ResolutionNs);
  printf("%d: Largest tolerable difference between clock readings is %llu nsec "
         "(%llu ms)\n",
         getpid(), ThresholdNs, ThresholdNs / 1000000);
  clock_gettime(CLOCK_MONOTONIC_RAW, &TimeSpec);
  PrevNs = (unsigned long long)(TimeSpec.tv_sec) * 1000000000ULL +
           (unsigned long long)(TimeSpec.tv_nsec);
  printf("Checking if we ever see too large difference between clock readings "
         "(program never exits)\n");
  for (;;) {
    clock_gettime(CLOCK_MONOTONIC_RAW, &TimeSpec);
    CurrentNs = (unsigned long long)(TimeSpec.tv_sec) * 1000000000ULL +
                (unsigned long long)(TimeSpec.tv_nsec);
    DiffNs = CurrentNs - PrevNs;
    if (Cooldown == 0) {
      if (DiffNs > ThresholdNs) {
        Time = time(NULL);
        UtcTime = gmtime(&Time);
        printf("pid %d: too large difference between clock readings, %llu nsec "
               "(largest tolerable difference is %llu nsec) at %s",
               getpid(), DiffNs, ThresholdNs, asctime(UtcTime));
        Cooldown = 2;
      }
    } else {
      --Cooldown;
    }
    PrevNs = CurrentNs;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x5f5e100,-0x48(%rbp)
05
movl   $0x2,-0x5c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11eb <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atol@plt>
imul   $0xf4240,%eax,%eax
cltq
mov    %rax,-0x48(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1080 <clock_getres@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
add    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x28(%rbp),%rdx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x48(%rbp),%rax
mov    $0xf4240,%ecx
xor    %edx,%edx
div    %rcx
mov    -0x68(%rbp),%rdx
mov    %rax,%rcx
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
add    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
add    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
sub    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jne    1336 <main+0x1b6>
mov    -0x40(%rbp),%rax
cmp    -0x48(%rbp),%rax
jbe    1331 <main+0x1b1>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x4c(%rbp),%rdi
call   10a0 <gmtime@plt>
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x58(%rbp),%rdi
call   1050 <asctime@plt>
mov    -0x7c(%rbp),%esi
mov    -0x78(%rbp),%rdx
mov    -0x70(%rbp),%rcx
mov    %eax,%r8d
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x5c(%rbp)
jmp    133f <main+0x1bf>
mov    -0x5c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x5c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    128c <main+0x10c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x5f5e100,%ebp
cmp    $0x2,%edi
jl     11ba <main+0x2a>
mov    0x8(%rsi),%rdi
call   10a0 <atol@plt>
imul   $0xf4240,%eax,%eax
movslq %eax,%rbp
mov    0x2e27(%rip),%r12
mov    (%r12),%edi
lea    0x8(%rsp),%r15
mov    %r15,%rsi
call   1090 <clock_getres@plt>
imul   $0x3b9aca00,0x10(%rsp),%rbx
9a 3b
add    0x8(%rsp),%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe12(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%esi
movabs $0x431bde82d7b634db,%rcx
de 1b 43
mov    %rbp,%rax
mul    %rcx
mov    %rdx,%rcx
shr    $0x12,%rcx
lea    0xe13(%rip),%rdi
mov    %rbp,%r14
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%r12),%edi
mov    %r15,%r13
mov    %r15,%rsi
call   1040 <clock_gettime@plt>
imul   $0x3b9aca00,0x10(%rsp),%r12
9a 3b
add    0x8(%rsp),%r12
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
nopw   0x0(%rax,%rax,1)
mov    $0x2,%r15d
jmp    1274 <main+0xe4>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%r15d
mov    %r12,%rbp
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    %r13,%rsi
call   1040 <clock_gettime@plt>
imul   $0x3b9aca00,0x10(%rsp),%r12
9a 3b
add    0x8(%rsp),%r12
mov    %r12,%rbx
sub    %rbp,%rbx
test   %r15d,%r15d
jne    1270 <main+0xe0>
xor    %r15d,%r15d
cmp    %r14,%rbx
jbe    1274 <main+0xe4>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   10b0 <gmtime@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r15d
mov    %rbp,%rdi
call   1060 <asctime@plt>
lea    0xdaa(%rip),%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1260 <main+0xd0>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
%struct.tm = type { i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@.str = [52 x i8] c"%d: Resolution of CLOCK_MONOTONIC_RAW is %llu nsec\0A\00"
@.str.1 = [80 x i8] c"%d: Largest tolerable difference between clock readings is %llu nsec (%llu ms)\0A\00"
@.str.2 = [91 x i8] c"Checking if we ever see too large difference between clock readings (program never exits)\0A\00"
@.str.3 = [113 x i8] c"pid %d: too large difference between clock readings, %llu nsec (largest tolerable difference is %llu nsec) at %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca %struct.tm*
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i64 100000000, i64* %11
store i32 2, i32* %14
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atol(i8* %20)
%22 = mul nsw i32 %21, 1000000
%23 = sext i32 %22 to i64
store i64 %23, i64* %11
br label %24
24: ; preds = %17, %2
%25 = load i32, i32* @CLOCK_MONOTONIC_RAW
%26 = call i32 @clock_getres(i32 %25, %struct.timespec* %6)
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%28 = load i64, i64* %27
%29 = mul i64 %28, 1000000000
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%31 = load i64, i64* %30
%32 = add i64 %29, %31
store i64 %32, i64* %7
%33 = call i32 (...) @getpid()
%34 = load i64, i64* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %33, i64 %34)
%36 = call i32 (...) @getpid()
%37 = load i64, i64* %11
%38 = load i64, i64* %11
%39 = udiv i64 %38, 1000000
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i32 %36, i64 %37, i64 %39)
%41 = load i32, i32* @CLOCK_MONOTONIC_RAW
%42 = call i32 @clock_gettime(i32 %41, %struct.timespec* %6)
%43 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%44 = load i64, i64* %43
%45 = mul i64 %44, 1000000000
%46 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%47 = load i64, i64* %46
%48 = add i64 %45, %47
store i64 %48, i64* %8
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %81, %24
%51 = load i32, i32* @CLOCK_MONOTONIC_RAW
%52 = call i32 @clock_gettime(i32 %51, %struct.timespec* %6)
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%54 = load i64, i64* %53
%55 = mul i64 %54, 1000000000
%56 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%57 = load i64, i64* %56
%58 = add i64 %55, %57
store i64 %58, i64* %9
%59 = load i64, i64* %9
%60 = load i64, i64* %8
%61 = sub i64 %59, %60
store i64 %61, i64* %10
%62 = load i32, i32* %14
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %78
64: ; preds = %50
%65 = load i64, i64* %10
%66 = load i64, i64* %11
%67 = icmp ugt i64 %65, %66
br i1 %67, label %68, label %77
68: ; preds = %64
%69 = call i32 @time(i32* null)
store i32 %69, i32* %12
%70 = call %struct.tm* @gmtime(i32* %12)
store %struct.tm* %70, %struct.tm** %13
%71 = call i32 (...) @getpid()
%72 = load i64, i64* %10
%73 = load i64, i64* %11
%74 = load %struct.tm*, %struct.tm** %13
%75 = call i32 @asctime(%struct.tm* %74)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([113 x i8], [113 x i8]* @.str.3, i64 0, i64 0), i32 %71, i64 %72, i64 %73, i32 %75)
store i32 2, i32* %14
br label %77
77: ; preds = %68, %64
br label %81
78: ; preds = %50
%79 = load i32, i32* %14
%80 = add nsw i32 %79, -1
store i32 %80, i32* %14
br label %81
81: ; preds = %78, %77
%82 = load i64, i64* %9
store i64 %82, i64* %8
br label %50
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
%struct.tm = type { i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@.str = [52 x i8] c"%d: Resolution of CLOCK_MONOTONIC_RAW is %llu nsec\0A\00"
@.str.1 = [80 x i8] c"%d: Largest tolerable difference between clock readings is %llu nsec (%llu ms)\0A\00"
@.str.3 = [113 x i8] c"pid %d: too large difference between clock readings, %llu nsec (largest tolerable difference is %llu nsec) at %s\00"
@str = [90 x i8] c"Checking if we ever see too large difference between clock readings (program never exits)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = bitcast %struct.timespec* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atol(i8* %10)
%12 = mul nsw i32 %11, 1000000
%13 = sext i32 %12 to i64
br label %14
14: ; preds = %8, %2
%15 = phi i64 [ %13, %8 ], [ 100000000, %2 ]
%16 = load i32, i32* @CLOCK_MONOTONIC_RAW
%17 = call i32 @clock_getres(i32 %16, %struct.timespec* %3)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%19 = load i64, i64* %18
%20 = mul i64 %19, 1000000000
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%22 = load i64, i64* %21
%23 = add i64 %20, %22
%24 = call i32 (...) @getpid()
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %24, i64 %23)
%26 = call i32 (...) @getpid()
%27 = udiv i64 %15, 1000000
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @.str.1, i64 0, i64 0), i32 %26, i64 %15, i64 %27)
%29 = load i32, i32* @CLOCK_MONOTONIC_RAW
%30 = call i32 @clock_gettime(i32 %29, %struct.timespec* %3)
%31 = load i64, i64* %18
%32 = mul i64 %31, 1000000000
%33 = load i64, i64* %21
%34 = add i64 %32, %33
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([90 x i8], [90 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %49, %14
%37 = phi i64 [ %34, %14 ], [ %44, %49 ]
%38 = phi i32 [ 2, %14 ], [ %50, %49 ]
%39 = load i32, i32* @CLOCK_MONOTONIC_RAW
%40 = call i32 @clock_gettime(i32 %39, %struct.timespec* %3)
%41 = load i64, i64* %18
%42 = mul i64 %41, 1000000000
%43 = load i64, i64* %21
%44 = add i64 %42, %43
%45 = sub i64 %44, %37
%46 = icmp eq i32 %38, 0
br i1 %46, label %47, label %57
47: ; preds = %36
%48 = icmp ugt i64 %45, %15
br i1 %48, label %51, label %49
49: ; preds = %47, %51, %57
%50 = phi i32 [ 0, %47 ], [ 2, %51 ], [ %58, %57 ]
br label %36
51: ; preds = %47
%52 = call i32 @time(i32* null)
store i32 %52, i32* %4
%53 = call %struct.tm* @gmtime(i32* %4)
%54 = call i32 (...) @getpid()
%55 = call i32 @asctime(%struct.tm* %53)
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([113 x i8], [113 x i8]* @.str.3, i64 0, i64 0), i32 %54, i64 %45, i64 %15, i32 %55)
br label %49
57: ; preds = %36
%58 = add nsw i32 %38, -1
br label %49
}