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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
int EINTR;
char *asctime(struct tm *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int clock_nanosleep(int /*<<< orphan*/, int /*<<< orphan*/, struct timespec *,
                    struct timespec *);
int /*<<< orphan*/ exit(int);
int getpid();
struct tm *gmtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(struct timespec *, struct timespec *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, const char *argv[]) {
  struct timespec TimeSpec, TimeSpecRemain;
  unsigned long long StartNs = 0, EndNs = 0, DiffNs = 0;
  time_t Time;
  struct tm *UtcTime;
  int SleepResult = 0;
  printf("Checking if we ever overshoot clock_nanosleep() for too long "
         "(program never exits).\n");
  for (;;) {
    clock_gettime(CLOCK_MONOTONIC_RAW, &TimeSpec);
    StartNs = (unsigned long long)(TimeSpec.tv_sec) * 1000000000ULL +
              (unsigned long long)(TimeSpec.tv_nsec);
    TimeSpec.tv_sec = 0;
    TimeSpec.tv_nsec = 33000000;
    for (;;) {
      SleepResult =
          clock_nanosleep(CLOCK_MONOTONIC, 0, &TimeSpec, &TimeSpecRemain);
      if (SleepResult == 0) {
        break;
      } else if (SleepResult != EINTR) {
        Time = time(NULL);
        UtcTime = gmtime(&Time);
        printf("pid %d: clock_nanosleep() failed with %d (%s) at %s", getpid(),
               SleepResult, strerror(SleepResult), asctime(UtcTime));
        exit(1);
      }
      memcpy(&TimeSpec, &TimeSpecRemain, sizeof(TimeSpec));
    }
    clock_gettime(CLOCK_MONOTONIC_RAW, &TimeSpec);
    EndNs = (unsigned long long)(TimeSpec.tv_sec) * 1000000000ULL +
            (unsigned long long)(TimeSpec.tv_nsec);
    DiffNs = EndNs - StartNs;
    if (DiffNs > 100000000) {
      Time = time(NULL);
      UtcTime = gmtime(&Time);
      printf("pid %d: clock_nanosleep() took %llu nanoseconds instead of "
             "33000000, at %s",
             getpid(), DiffNs, asctime(UtcTime));
    }
  }
  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,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movl   $0x0,-0x5c(%rbp)
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1f78a40,-0x20(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   10b0 <clock_nanosleep@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jne    1244 <main+0xa4>
jmp    12d5 <main+0x135>
mov    -0x5c(%rbp),%eax
mov    0x2d82(%rip),%rcx
cmp    (%rcx),%eax
je     12b9 <main+0x119>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x4c(%rbp),%rdi
call   1090 <gmtime@plt>
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x70(%rbp)
mov    -0x5c(%rbp),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x5c(%rbp),%edi
call   10c0 <strerror@plt>
mov    %rax,-0x68(%rbp)
mov    -0x58(%rbp),%rdi
call   1050 <asctime@plt>
mov    -0x70(%rbp),%esi
mov    -0x6c(%rbp),%edx
mov    -0x68(%rbp),%rcx
mov    %rax,%r8
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    12be <main+0x11e>
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1070 <memcpy@plt>
jmp    121a <main+0x7a>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x5f5e100,-0x48(%rbp)
05
jbe    1360 <main+0x1c0>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x4c(%rbp),%rdi
call   1090 <gmtime@plt>
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x58(%rbp),%rdi
call   1050 <asctime@plt>
mov    -0x7c(%rbp),%esi
mov    -0x78(%rbp),%rdx
mov    %rax,%rcx
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11e6 <main+0x46>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xeba(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e0f(%rip),%r13
lea    0x18(%rsp),%rbx
mov    0x2e0b(%rip),%r12
lea    0x28(%rsp),%r14
mov    0x2de7(%rip),%r15
nopl   0x0(%rax)
mov    0x0(%r13),%edi
mov    %rbx,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%rax
mov    %rax,0x8(%rsp)
movslq 0x18(%rsp),%r13
movq   $0x0,0x20(%rsp)
movl   $0x1f78a40,0x18(%rsp)
01
mov    (%r12),%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   10c0 <clock_nanosleep@plt>
test   %eax,%eax
je     1270 <main+0xc0>
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    (%r15),%ebp
jne    12fb <main+0x14b>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1080 <memcpy@plt>
mov    (%r12),%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   10c0 <clock_nanosleep@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1240 <main+0x90>
mov    %r13,%rbp
mov    0x2d66(%rip),%r13
mov    0x0(%r13),%edi
mov    %rbx,%rsi
call   1040 <clock_gettime@plt>
mov    0x20(%rsp),%rax
sub    0x8(%rsp),%rax
movslq 0x18(%rsp),%rcx
sub    %rbp,%rcx
imul   $0x3b9aca00,%rax,%rbp
add    %rcx,%rbp
cmp    $0x5f5e101,%rbp
jb     11f0 <main+0x40>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   10a0 <gmtime@plt>
mov    %rax,0x8(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rsp),%rdi
call   1060 <asctime@plt>
lea    0xd4f(%rip),%rdi
mov    0x14(%rsp),%esi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <printf@plt>
jmp    11f0 <main+0x40>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   10a0 <gmtime@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   10d0 <strerror@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1060 <asctime@plt>
lea    0xcca(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
%struct.tm = type { i32 }
@.str = [85 x i8] c"Checking if we ever overshoot clock_nanosleep() for too long (program never exits).\0A\00"
@CLOCK_MONOTONIC_RAW = global i32 0
@CLOCK_MONOTONIC = global i32 0
@EINTR = global i32 0
@.str.1 = [52 x i8] c"pid %d: clock_nanosleep() failed with %d (%s) at %s\00"
@.str.2 = [75 x i8] c"pid %d: clock_nanosleep() took %llu nanoseconds instead of 33000000, 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 %struct.timespec
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.tm*
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %8
store i64 0, i64* %9
store i64 0, i64* %10
store i32 0, i32* %13
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %74, %2
%16 = load i32, i32* @CLOCK_MONOTONIC_RAW
%17 = call i32 @clock_gettime(i32 %16, %struct.timespec* %6)
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%19 = load i64, i64* %18
%20 = mul i64 %19, 1000000000
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = sext i32 %22 to i64
%24 = add i64 %20, %23
store i64 %24, i64* %8
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 33000000, i32* %26
br label %27
27: ; preds = %49, %15
%28 = load i32, i32* @CLOCK_MONOTONIC
%29 = call i32 @clock_nanosleep(i32 %28, i32 0, %struct.timespec* %6, %struct.timespec* %7)
store i32 %29, i32* %13
%30 = load i32, i32* %13
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
br label %51
33: ; preds = %27
%34 = load i32, i32* %13
%35 = load i32, i32* @EINTR
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %48
37: ; preds = %33
%38 = call i32 @time(i32* null)
store i32 %38, i32* %11
%39 = call %struct.tm* @gmtime(i32* %11)
store %struct.tm* %39, %struct.tm** %12
%40 = call i32 (...) @getpid()
%41 = load i32, i32* %13
%42 = load i32, i32* %13
%43 = call i8* @strerror(i32 %42)
%44 = load %struct.tm*, %struct.tm** %12
%45 = call i8* @asctime(%struct.tm* %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %40, i32 %41, i8* %43, i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %33
br label %49
49: ; preds = %48
%50 = call i32 @memcpy(%struct.timespec* %6, %struct.timespec* %7, i32 16)
br label %27
51: ; preds = %32
%52 = load i32, i32* @CLOCK_MONOTONIC_RAW
%53 = call i32 @clock_gettime(i32 %52, %struct.timespec* %6)
%54 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%55 = load i64, i64* %54
%56 = mul i64 %55, 1000000000
%57 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%58 = load i32, i32* %57
%59 = sext i32 %58 to i64
%60 = add i64 %56, %59
store i64 %60, i64* %9
%61 = load i64, i64* %9
%62 = load i64, i64* %8
%63 = sub i64 %61, %62
store i64 %63, i64* %10
%64 = load i64, i64* %10
%65 = icmp ugt i64 %64, 100000000
br i1 %65, label %66, label %74
66: ; preds = %51
%67 = call i32 @time(i32* null)
store i32 %67, i32* %11
%68 = call %struct.tm* @gmtime(i32* %11)
store %struct.tm* %68, %struct.tm** %12
%69 = call i32 (...) @getpid()
%70 = load i64, i64* %10
%71 = load %struct.tm*, %struct.tm** %12
%72 = call i8* @asctime(%struct.tm* %71)
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.2, i64 0, i64 0), i32 %69, i64 %70, i8* %72)
br label %74
74: ; preds = %66, %51
br label %15
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
%struct.tm = type { i32 }
@CLOCK_MONOTONIC_RAW = global i32 0
@CLOCK_MONOTONIC = global i32 0
@EINTR = global i32 0
@.str.1 = [52 x i8] c"pid %d: clock_nanosleep() failed with %d (%s) at %s\00"
@.str.2 = [75 x i8] c"pid %d: clock_nanosleep() took %llu nanoseconds instead of 33000000, at %s\00"
@str = [84 x i8] c"Checking if we ever overshoot clock_nanosleep() for too long (program never exits).\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timespec
%4 = alloca %struct.timespec
%5 = alloca i32
%6 = bitcast %struct.timespec* %3 to i8*
%7 = bitcast %struct.timespec* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([84 x i8], [84 x i8]* @str, i64 0, i64 0))
%10 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
br label %12
12: ; preds = %55, %2
%13 = load i32, i32* @CLOCK_MONOTONIC_RAW
%14 = call i32 @clock_gettime(i32 %13, %struct.timespec* %3)
%15 = load i64, i64* %10
%16 = load i32, i32* %11
%17 = sext i32 %16 to i64
store i64 0, i64* %10
store i32 33000000, i32* %11
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = call i32 @clock_nanosleep(i32 %18, i32 0, %struct.timespec* %3, %struct.timespec* %4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %38, label %21
21: ; preds = %12, %33
%22 = phi i32 [ %36, %33 ], [ %19, %12 ]
%23 = load i32, i32* @EINTR
%24 = icmp eq i32 %22, %23
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = call i32 @time(i32* null)
store i32 %26, i32* %5
%27 = call %struct.tm* @gmtime(i32* %5)
%28 = call i32 (...) @getpid()
%29 = call i8* @strerror(i32 %22)
%30 = call i8* @asctime(%struct.tm* %27)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %22, i8* %29, i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %21
%34 = call i32 @memcpy(%struct.timespec* %3, %struct.timespec* %4, i32 16)
%35 = load i32, i32* @CLOCK_MONOTONIC
%36 = call i32 @clock_nanosleep(i32 %35, i32 0, %struct.timespec* %3, %struct.timespec* %4)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %21
38: ; preds = %33, %12
%39 = load i32, i32* @CLOCK_MONOTONIC_RAW
%40 = call i32 @clock_gettime(i32 %39, %struct.timespec* %3)
%41 = load i64, i64* %10
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = sub i64 %41, %15
%45 = mul i64 %44, 1000000000
%46 = sub nsw i64 %43, %17
%47 = add i64 %46, %45
%48 = icmp ugt i64 %47, 100000000
br i1 %48, label %49, label %55
49: ; preds = %38
%50 = call i32 @time(i32* null)
store i32 %50, i32* %5
%51 = call %struct.tm* @gmtime(i32* %5)
%52 = call i32 (...) @getpid()
%53 = call i8* @asctime(%struct.tm* %51)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.2, i64 0, i64 0), i32 %52, i64 %47, i8* %53)
br label %55
55: ; preds = %49, %38
br label %12
}