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

/* Variables and functions */
int /*<<< orphan*/ clock_gettime(int, struct timespec *);
char *clockstring(int);
int diff_timespec(struct timespec, struct timespec, struct timespec);
long long llabs(long long);
int /*<<< orphan*/ printf(char *, char *, long long, long long, long long);
int /*<<< orphan*/ usleep(unsigned long long);
int test_offsets(int clockid, long long margin, int sleep, int runtime) {
  struct timespec testclk, mono, bound, start;
  long long off1, off2, delta;
  clock_gettime(1, &mono);
  clock_gettime(clockid, &testclk);
  clock_gettime(1, &bound);
  off1 = diff_timespec(mono, testclk, bound);
  start = mono;
  while (1) {
    clock_gettime(1, &mono);
    clock_gettime(clockid, &testclk);
    clock_gettime(1, &bound);
    off2 = diff_timespec(mono, testclk, bound);
    delta = llabs(off2 - off1);
    if (delta > margin) {
      printf("ERROR: Clock: %s Offset: %lld Delta: %lld Margin: %lld\n",
             clockstring(clockid), off2, delta, margin);
      return -1;
    }
    off1 = off2;
    if (sleep)
      usleep(1000000000ULL / 1000);
    if (diff_timespec(start, mono, start) > runtime * 1000000000ULL)
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_offsets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x1,%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1070 <diff_timespec@plt>
cltq
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    $0x1,%edi
lea    -0x28(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x1,%edi
lea    -0x30(%rbp),%rsi
call   1030 <clock_gettime@plt>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1070 <diff_timespec@plt>
cltq
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
sub    -0x40(%rbp),%rdi
call   1060 <llabs@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x10(%rbp),%rax
jle    1241 <test_offsets+0xe1>
mov    -0x8(%rbp),%edi
call   1040 <clockstring@plt>
mov    %rax,%rsi
mov    -0x48(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xdd0(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <test_offsets+0x132>
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     125d <test_offsets+0xfd>
mov    $0xf4240,%edi
call   1080 <usleep@plt>
mov    -0x38(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x38(%rbp),%edx
call   1070 <diff_timespec@plt>
cltq
movslq -0x18(%rbp),%rcx
imul   $0x3b9aca00,%rcx,%rcx
cmp    %rcx,%rax
jbe    1286 <test_offsets+0x126>
jmp    128b <test_offsets+0x12b>
jmp    11b7 <test_offsets+0x57>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_offsets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebx
mov    %edx,0x1c(%rsp)
mov    %rsi,0x20(%rsp)
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
mov    $0x1,%edi
call   1030 <clock_gettime@plt>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
call   1030 <clock_gettime@plt>
lea    0x10(%rsp),%rsi
mov    $0x1,%edi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%edi
mov    0x18(%rsp),%esi
mov    0x10(%rsp),%edx
call   1060 <diff_timespec@plt>
movslq %eax,%r12
mov    0x8(%rsp),%r15d
movslq %ebx,%rax
imul   $0x3b9aca00,%rax,%r14
jmp    11d6 <test_offsets+0x86>
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%esi
mov    %r15d,%edi
mov    %r15d,%edx
call   1060 <diff_timespec@plt>
cltq
cmp    %rax,%r14
jb     1244 <test_offsets+0xf4>
mov    %r12,%rbx
mov    $0x1,%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    %ebp,%edi
lea    0x18(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    $0x1,%edi
lea    0x10(%rsp),%rsi
call   1030 <clock_gettime@plt>
mov    0x8(%rsp),%edi
mov    0x18(%rsp),%esi
mov    0x10(%rsp),%edx
call   1060 <diff_timespec@plt>
movslq %eax,%r12
mov    %r12,%rax
sub    %rbx,%rax
mov    %rax,%r13
neg    %r13
cmovs  %rax,%r13
cmp    0x20(%rsp),%r13
jg     1248 <test_offsets+0xf8>
cmpl   $0x0,0x1c(%rsp)
je     11c0 <test_offsets+0x70>
mov    $0xf4240,%edi
call   1070 <usleep@plt>
jmp    11c0 <test_offsets+0x70>
xor    %eax,%eax
jmp    126e <test_offsets+0x11e>
mov    %ebp,%edi
call   1040 <clockstring@plt>
lea    0xdaa(%rip),%rdi
mov    %rax,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    0x20(%rsp),%r8
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [56 x i8] c"ERROR: Clock: %s Offset: %lld Delta: %lld Margin: %lld\0A\00"
define i32 @test_offsets(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.timespec
%11 = alloca %struct.timespec
%12 = alloca %struct.timespec
%13 = alloca %struct.timespec
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
store i32 %0, i32* %6
store i64 %1, i64* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%17 = call i32 @clock_gettime(i32 1, %struct.timespec* %11)
%18 = load i32, i32* %6
%19 = call i32 @clock_gettime(i32 %18, %struct.timespec* %10)
%20 = call i32 @clock_gettime(i32 1, %struct.timespec* %12)
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @diff_timespec(i32 %22, i32 %24, i32 %26)
%28 = sext i32 %27 to i64
store i64 %28, i64* %14
%29 = bitcast %struct.timespec* %13 to i8*
%30 = bitcast %struct.timespec* %11 to i8*
br label %31
31: ; preds = %4, %78
%32 = call i32 @clock_gettime(i32 1, %struct.timespec* %11)
%33 = load i32, i32* %6
%34 = call i32 @clock_gettime(i32 %33, %struct.timespec* %10)
%35 = call i32 @clock_gettime(i32 1, %struct.timespec* %12)
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.timespec, %struct.timespec* %10, i32 0, i32 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @diff_timespec(i32 %37, i32 %39, i32 %41)
%43 = sext i32 %42 to i64
store i64 %43, i64* %15
%44 = load i64, i64* %15
%45 = load i64, i64* %14
%46 = sub nsw i64 %44, %45
%47 = call i64 @llabs(i64 %46)
store i64 %47, i64* %16
%48 = load i64, i64* %16
%49 = load i64, i64* %7
%50 = icmp sgt i64 %48, %49
br i1 %50, label %51, label %58
51: ; preds = %31
%52 = load i32, i32* %6
%53 = call i8* @clockstring(i32 %52)
%54 = load i64, i64* %15
%55 = load i64, i64* %16
%56 = load i64, i64* %7
%57 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %53, i64 %54, i64 %55, i64 %56)
store i32 -1, i32* %5
br label %80
58: ; preds = %31
%59 = load i64, i64* %15
store i64 %59, i64* %14
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %58
%63 = call i32 @usleep(i64 1000000)
br label %64
64: ; preds = %62, %58
%65 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
%66 = load i32, i32* %65
%67 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
%68 = load i32, i32* %67
%69 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 0
%70 = load i32, i32* %69
%71 = call i32 @diff_timespec(i32 %66, i32 %68, i32 %70)
%72 = sext i32 %71 to i64
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = mul i64 %74, 1000000000
%76 = icmp ugt i64 %72, %75
br i1 %76, label %77, label %78
77: ; preds = %64
br label %79
78: ; preds = %64
br label %31
79: ; preds = %77
store i32 0, i32* %5
br label %80
80: ; preds = %79, %51
%81 = load i32, i32* %5
ret i32 %81
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@.str = [56 x i8] c"ERROR: Clock: %s Offset: %lld Delta: %lld Margin: %lld\0A\00"
define i32 @test_offsets(i32 %0, i64 %1, i32 %2, i32 %3) {
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = bitcast %struct.timespec* %5 to i8*
%9 = bitcast %struct.timespec* %6 to i8*
%10 = bitcast %struct.timespec* %7 to i8*
%11 = call i32 @clock_gettime(i32 1, %struct.timespec* %6)
%12 = call i32 @clock_gettime(i32 %0, %struct.timespec* %5)
%13 = call i32 @clock_gettime(i32 1, %struct.timespec* %7)
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @diff_timespec(i32 %15, i32 %17, i32 %19)
%21 = sext i32 %20 to i64
%22 = load i32, i32* %14
%23 = icmp eq i32 %2, 0
%24 = sext i32 %3 to i64
%25 = mul nsw i64 %24, 1000000000
br label %26
26: ; preds = %45, %4
%27 = phi i64 [ %21, %4 ], [ %35, %45 ]
%28 = call i32 @clock_gettime(i32 1, %struct.timespec* %6)
%29 = call i32 @clock_gettime(i32 %0, %struct.timespec* %5)
%30 = call i32 @clock_gettime(i32 1, %struct.timespec* %7)
%31 = load i32, i32* %14
%32 = load i32, i32* %16
%33 = load i32, i32* %18
%34 = call i32 @diff_timespec(i32 %31, i32 %32, i32 %33)
%35 = sext i32 %34 to i64
%36 = sub nsw i64 %35, %27
%37 = call i64 @llvm.abs.i64(i64 %36, i1 true)
%38 = icmp sgt i64 %37, %1
br i1 %38, label %39, label %42
39: ; preds = %26
%40 = call i8* @clockstring(i32 %0)
%41 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %40, i64 %35, i64 %37, i64 %1)
br label %50
42: ; preds = %26
br i1 %23, label %45, label %43
43: ; preds = %42
%44 = call i32 @usleep(i64 1000000)
br label %45
45: ; preds = %43, %42
%46 = load i32, i32* %14
%47 = call i32 @diff_timespec(i32 %22, i32 %46, i32 %22)
%48 = sext i32 %47 to i64
%49 = icmp ult i64 %25, %48
br i1 %49, label %50, label %26
50: ; preds = %45, %39
%51 = phi i32 [ -1, %39 ], [ 0, %45 ]
ret i32 %51
}