#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 timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
struct ctx {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ctx_cleanup_timer(struct ctx *);
int /*<<< orphan*/ ctx_start_timer(struct ctx *);
int /*<<< orphan*/ gettimeofday_syscall_nofail(struct timeval *,
                                               int /*<<< orphan*/ *);
int /*<<< orphan*/ gettimeofday_vdso_nofail(struct timeval *,
                                            int /*<<< orphan*/ *);
int /*<<< orphan*/ log_failure(struct ctx *, char *, long, long, ...);
int /*<<< orphan*/ test_should_stop(struct ctx *);
int /*<<< orphan*/ timeval_normalized(struct timeval *);
int /*<<< orphan*/ timevals_ordered(struct timeval *, struct timeval *);
int /*<<< orphan*/ vdso_has_gettimeofday();
void gettimeofday_verify(struct ctx *ctx) {
  struct timeval now;
  gettimeofday_syscall_nofail(&now, NULL);
  ctx_start_timer(ctx);
  while (!test_should_stop(ctx)) {
    struct timeval prev;
    if (!vdso_has_gettimeofday())
      goto skip_vdso;
    prev = now;
    gettimeofday_vdso_nofail(&now, NULL);
    if (!timeval_normalized(&now)) {
      log_failure(ctx,
                  "timestamp obtained from libc/vDSO "
                  "not normalized:\n"
                  "\t[%ld, %ld]\n",
                  (long int)now.tv_sec, (long int)now.tv_usec);
    }
    if (!timevals_ordered(&prev, &now)) {
      log_failure(ctx,
                  "timestamp obtained from libc/vDSO "
                  "predates timestamp\n"
                  "previously obtained from kernel:\n"
                  "\t[%ld, %ld] (kernel)\n"
                  "\t[%ld, %ld] (vDSO)\n",
                  (long int)prev.tv_sec, (long int)prev.tv_usec,
                  (long int)now.tv_sec, (long int)now.tv_usec);
    }
  skip_vdso:
    prev = now;
    gettimeofday_syscall_nofail(&now, NULL);
    if (!timeval_normalized(&now)) {
      log_failure(ctx,
                  "timestamp obtained from kernel "
                  "not normalized:\n"
                  "\t[%ld, %ld]\n",
                  (long int)now.tv_sec, (long int)now.tv_usec);
    }
    if (!timevals_ordered(&prev, &now)) {
      log_failure(ctx,
                  "timestamp obtained from kernel "
                  "predates timestamp\n"
                  "previously obtained from libc/vDSO:\n"
                  "\t[%ld, %ld] (vDSO)\n"
                  "\t[%ld, %ld] (kernel)\n",
                  (long int)prev.tv_sec, (long int)prev.tv_usec,
                  (long int)now.tv_sec, (long int)now.tv_usec);
    }
  }
  ctx_cleanup_timer(ctx);
}


x86 assembly (clang, -O0):
<gettimeofday_verify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <gettimeofday_syscall_nofail@plt>
mov    -0x8(%rbp),%rdi
call   1080 <ctx_start_timer@plt>
mov    -0x8(%rbp),%rdi
call   1060 <test_should_stop@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d0 <gettimeofday_verify+0x40>
jmp    12f1 <gettimeofday_verify+0x161>
mov    $0x0,%al
call   1040 <vdso_has_gettimeofday@plt>
cmp    $0x0,%eax
jne    11e5 <gettimeofday_verify+0x55>
jmp    126b <gettimeofday_verify+0xdb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday_vdso_nofail@plt>
lea    -0x18(%rbp),%rdi
call   1050 <timeval_normalized@plt>
cmp    $0x0,%eax
jne    122e <gettimeofday_verify+0x9e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   10b0 <log_failure@plt>
lea    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <timevals_ordered@plt>
cmp    $0x0,%eax
jne    1266 <gettimeofday_verify+0xd6>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   10b0 <log_failure@plt>
jmp    126b <gettimeofday_verify+0xdb>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <gettimeofday_syscall_nofail@plt>
lea    -0x18(%rbp),%rdi
call   1050 <timeval_normalized@plt>
cmp    $0x0,%eax
jne    12b4 <gettimeofday_verify+0x124>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   10b0 <log_failure@plt>
lea    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <timevals_ordered@plt>
cmp    $0x0,%eax
jne    12ec <gettimeofday_verify+0x15c>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   10b0 <log_failure@plt>
jmp    11b2 <gettimeofday_verify+0x22>
mov    -0x8(%rbp),%rdi
call   1090 <ctx_cleanup_timer@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gettimeofday_verify>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
call   10a0 <gettimeofday_syscall_nofail@plt>
mov    %r14,%rdi
call   1080 <ctx_start_timer@plt>
mov    %r14,%rdi
call   1060 <test_should_stop@plt>
test   %eax,%eax
je     11d6 <gettimeofday_verify+0x46>
mov    %r14,%rdi
call   1090 <ctx_cleanup_timer@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%rbx
lea    0xede(%rip),%r15
lea    0x10(%rsp),%r12
lea    0xf0e(%rip),%r13
lea    0xe4c(%rip),%rbp
jmp    120c <gettimeofday_verify+0x7c>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1060 <test_should_stop@plt>
test   %eax,%eax
jne    11bf <gettimeofday_verify+0x2f>
xor    %eax,%eax
call   1040 <vdso_has_gettimeofday@plt>
test   %eax,%eax
je     127f <gettimeofday_verify+0xef>
movups (%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <gettimeofday_vdso_nofail@plt>
mov    %rbx,%rdi
call   1050 <timeval_normalized@plt>
test   %eax,%eax
jne    1250 <gettimeofday_verify+0xc0>
mov    (%rsp),%rcx
mov    0x8(%rsp),%rdx
mov    %r14,%rdi
lea    0xdb7(%rip),%rsi
xor    %eax,%eax
call   10b0 <log_failure@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <timevals_ordered@plt>
test   %eax,%eax
jne    127f <gettimeofday_verify+0xef>
mov    0x10(%rsp),%rcx
mov    0x18(%rsp),%rdx
mov    (%rsp),%r9
mov    0x8(%rsp),%r8
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   10b0 <log_failure@plt>
movups (%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <gettimeofday_syscall_nofail@plt>
mov    %rbx,%rdi
call   1050 <timeval_normalized@plt>
test   %eax,%eax
jne    12b4 <gettimeofday_verify+0x124>
mov    (%rsp),%rcx
mov    0x8(%rsp),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   10b0 <log_failure@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1070 <timevals_ordered@plt>
test   %eax,%eax
jne    1200 <gettimeofday_verify+0x70>
mov    0x10(%rsp),%rcx
mov    0x18(%rsp),%rdx
mov    (%rsp),%r9
mov    0x8(%rsp),%r8
mov    %r14,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   10b0 <log_failure@plt>
jmp    1200 <gettimeofday_verify+0x70>

LLVM IR (-O0):
%struct.ctx = type { i32 }
%struct.timeval = type { i64, i64 }
@.str = [63 x i8] c"timestamp obtained from libc/vDSO not normalized:\0A\09[%ld, %ld]\0A\00"
@.str.1 = [127 x i8] c"timestamp obtained from libc/vDSO predates timestamp\0Apreviously obtained from kernel:\0A\09[%ld, %ld] (kernel)\0A\09[%ld, %ld] (vDSO)\0A\00"
@.str.2 = [60 x i8] c"timestamp obtained from kernel not normalized:\0A\09[%ld, %ld]\0A\00"
@.str.3 = [127 x i8] c"timestamp obtained from kernel predates timestamp\0Apreviously obtained from libc/vDSO:\0A\09[%ld, %ld] (vDSO)\0A\09[%ld, %ld] (kernel)\0A\00"
define void @gettimeofday_verify(%struct.ctx* %0) {
%2 = alloca %struct.ctx*
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
store %struct.ctx* %0, %struct.ctx** %2
%5 = call i32 @gettimeofday_syscall_nofail(%struct.timeval* %3, i32* null)
%6 = load %struct.ctx*, %struct.ctx** %2
%7 = call i32 @ctx_start_timer(%struct.ctx* %6)
br label %8
8: ; preds = %72, %1
%9 = load %struct.ctx*, %struct.ctx** %2
%10 = call i32 @test_should_stop(%struct.ctx* %9)
%11 = icmp ne i32 %10, 0
%12 = xor i1 %11, true
br i1 %12, label %13, label %73
13: ; preds = %8
%14 = call i32 (...) @vdso_has_gettimeofday()
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %13
br label %45
17: ; preds = %13
%18 = bitcast %struct.timeval* %4 to i8*
%19 = bitcast %struct.timeval* %3 to i8*
%20 = call i32 @gettimeofday_vdso_nofail(%struct.timeval* %3, i32* null)
%21 = call i32 @timeval_normalized(%struct.timeval* %3)
%22 = icmp ne i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %17
%24 = load %struct.ctx*, %struct.ctx** %2
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%26 = load i64, i64* %25
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%28 = load i64, i64* %27
%29 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %24, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i64 %26, i64 %28)
br label %30
30: ; preds = %23, %17
%31 = call i32 @timevals_ordered(%struct.timeval* %4, %struct.timeval* %3)
%32 = icmp ne i32 %31, 0
br i1 %32, label %44, label %33
33: ; preds = %30
%34 = load %struct.ctx*, %struct.ctx** %2
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%40 = load i64, i64* %39
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%42 = load i64, i64* %41
%43 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %34, i8* getelementptr inbounds ([127 x i8], [127 x i8]* @.str.1, i64 0, i64 0), i64 %36, i64 %38, i64 %40, i64 %42)
br label %44
44: ; preds = %33, %30
br label %45
45: ; preds = %44, %16
%46 = bitcast %struct.timeval* %4 to i8*
%47 = bitcast %struct.timeval* %3 to i8*
%48 = call i32 @gettimeofday_syscall_nofail(%struct.timeval* %3, i32* null)
%49 = call i32 @timeval_normalized(%struct.timeval* %3)
%50 = icmp ne i32 %49, 0
br i1 %50, label %58, label %51
51: ; preds = %45
%52 = load %struct.ctx*, %struct.ctx** %2
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%54 = load i64, i64* %53
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%56 = load i64, i64* %55
%57 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %52, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i64 %54, i64 %56)
br label %58
58: ; preds = %51, %45
%59 = call i32 @timevals_ordered(%struct.timeval* %4, %struct.timeval* %3)
%60 = icmp ne i32 %59, 0
br i1 %60, label %72, label %61
61: ; preds = %58
%62 = load %struct.ctx*, %struct.ctx** %2
%63 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%64 = load i64, i64* %63
%65 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%66 = load i64, i64* %65
%67 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%68 = load i64, i64* %67
%69 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%70 = load i64, i64* %69
%71 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %62, i8* getelementptr inbounds ([127 x i8], [127 x i8]* @.str.3, i64 0, i64 0), i64 %64, i64 %66, i64 %68, i64 %70)
br label %72
72: ; preds = %61, %58
br label %8
73: ; preds = %8
%74 = load %struct.ctx*, %struct.ctx** %2
%75 = call i32 @ctx_cleanup_timer(%struct.ctx* %74)
ret void
}

LLVM IR (-O2):
%struct.ctx = type { i32 }
%struct.timeval = type { i64, i64 }
@.str = [63 x i8] c"timestamp obtained from libc/vDSO not normalized:\0A\09[%ld, %ld]\0A\00"
@.str.1 = [127 x i8] c"timestamp obtained from libc/vDSO predates timestamp\0Apreviously obtained from kernel:\0A\09[%ld, %ld] (kernel)\0A\09[%ld, %ld] (vDSO)\0A\00"
@.str.2 = [60 x i8] c"timestamp obtained from kernel not normalized:\0A\09[%ld, %ld]\0A\00"
@.str.3 = [127 x i8] c"timestamp obtained from kernel predates timestamp\0Apreviously obtained from libc/vDSO:\0A\09[%ld, %ld] (vDSO)\0A\09[%ld, %ld] (kernel)\0A\00"
define void @gettimeofday_verify(%struct.ctx* %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @gettimeofday_syscall_nofail(%struct.timeval* %2, i32* null)
%6 = call i32 @ctx_start_timer(%struct.ctx* %0)
%7 = call i32 @test_should_stop(%struct.ctx* %0)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %55
9: ; preds = %1
%10 = bitcast %struct.timeval* %3 to i8*
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
br label %15
15: ; preds = %9, %52
%16 = call i32 (...) @vdso_has_gettimeofday()
%17 = icmp eq i32 %16, 0
br i1 %17, label %35, label %18
18: ; preds = %15
%19 = call i32 @gettimeofday_vdso_nofail(%struct.timeval* %2, i32* null)
%20 = call i32 @timeval_normalized(%struct.timeval* %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i64, i64* %11
%24 = load i64, i64* %12
%25 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %0, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i64 %23, i64 %24)
br label %26
26: ; preds = %22, %18
%27 = call i32 @timevals_ordered(%struct.timeval* %3, %struct.timeval* %2)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %26
%30 = load i64, i64* %13
%31 = load i64, i64* %14
%32 = load i64, i64* %11
%33 = load i64, i64* %12
%34 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %0, i8* getelementptr inbounds ([127 x i8], [127 x i8]* @.str.1, i64 0, i64 0), i64 %30, i64 %31, i64 %32, i64 %33)
br label %35
35: ; preds = %26, %29, %15
%36 = call i32 @gettimeofday_syscall_nofail(%struct.timeval* %2, i32* null)
%37 = call i32 @timeval_normalized(%struct.timeval* %2)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i64, i64* %11
%41 = load i64, i64* %12
%42 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %0, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i64 %40, i64 %41)
br label %43
43: ; preds = %39, %35
%44 = call i32 @timevals_ordered(%struct.timeval* %3, %struct.timeval* %2)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %43
%47 = load i64, i64* %13
%48 = load i64, i64* %14
%49 = load i64, i64* %11
%50 = load i64, i64* %12
%51 = call i32 (%struct.ctx*, i8*, i64, i64, ...) @log_failure(%struct.ctx* %0, i8* getelementptr inbounds ([127 x i8], [127 x i8]* @.str.3, i64 0, i64 0), i64 %47, i64 %48, i64 %49, i64 %50)
br label %52
52: ; preds = %46, %43
%53 = call i32 @test_should_stop(%struct.ctx* %0)
%54 = icmp eq i32 %53, 0
br i1 %54, label %15, label %55
55: ; preds = %52, %1
%56 = call i32 @ctx_cleanup_timer(%struct.ctx* %0)
ret void
}