#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_sec;
};
struct timespec {
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int abs(int);
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  struct timespec tpundertest;
  struct timeval tvstandard;
  int delta;
  if (clock_gettime(CLOCK_REALTIME, &tpundertest) == 0) {
    if (gettimeofday(&tvstandard, NULL) == 0) {
      delta = (int)tvstandard.tv_sec - (int)tpundertest.tv_sec;
      if (abs(delta) <= 1) {
        printf("Test PASSED\n");
        return 0;
      } else {
        printf("FAIL:  expected %d, received %d\n", (int)tvstandard.tv_sec,
               (int)tpundertest.tv_sec);
        return 1;
      }
    } else {
      perror("Error calling gettimeofday()\n");
      return 2;
    }
  }
  printf("clock_gettime() failed\n");
  return 2;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
cmp    $0x0,%rax
jne    1208 <main+0xb8>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
cmp    $0x0,%rax
jne    11f0 <main+0xa0>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <abs@plt>
cmp    $0x1,%eax
jg     11ca <main+0x7a>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    121d <main+0xcd>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    121d <main+0xcd>
lea    0xe37(%rip),%rdi
call   1070 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    121d <main+0xcd>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1040 <clock_gettime@plt>
test   %rax,%rax
je     1182 <main+0x32>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
add    $0x18,%rsp
ret
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
test   %rax,%rax
jne    11d6 <main+0x86>
mov    0x8(%rsp),%esi
mov    0x10(%rsp),%edx
mov    %esi,%eax
sub    %edx,%eax
mov    %eax,%ecx
neg    %ecx
cmovs  %eax,%ecx
cmp    $0x1,%ecx
ja     11be <main+0x6e>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xe3b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
ret
lea    0xe44(%rip),%rdi
call   1070 <perror@plt>
jmp    1178 <main+0x28>

LLVM IR (-O0):
%struct.timespec = type { i64 }
%struct.timeval = type { i64 }
@CLOCK_REALTIME = global i32 0
@.str = [13 x i8] c"Test PASSED\0A\00"
@.str.1 = [33 x i8] c"FAIL: expected %d, received %d\0A\00"
@.str.2 = [30 x i8] c"Error calling gettimeofday()\0A\00"
@.str.3 = [24 x i8] c"clock_gettime() failed\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = alloca %struct.timeval
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @CLOCK_REALTIME
%6 = call i64 @clock_gettime(i32 %5, %struct.timespec* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %34
8: ; preds = %0
%9 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %32
11: ; preds = %8
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%13 = load i64, i64* %12
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = sub nsw i32 %14, %17
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = call i32 @abs(i32 %19)
%21 = icmp sle i32 %20, 1
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %1
br label %36
24: ; preds = %11
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%26 = load i64, i64* %25
%27 = trunc i64 %26 to i32
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i32 0, i32 0
%29 = load i64, i64* %28
%30 = trunc i64 %29 to i32
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %30)
store i32 1, i32* %1
br label %36
32: ; preds = %8
%33 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %36
34: ; preds = %0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %36
36: ; preds = %34, %32, %24, %22
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
%struct.timeval = type { i64 }
@CLOCK_REALTIME = global i32 0
@.str.1 = [33 x i8] c"FAIL: expected %d, received %d\0A\00"
@.str.2 = [30 x i8] c"Error calling gettimeofday()\0A\00"
@str = [23 x i8] c"clock_gettime() failed\00"
@str.4 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = alloca %struct.timespec
%2 = alloca %struct.timeval
%3 = bitcast %struct.timespec* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = load i32, i32* @CLOCK_REALTIME
%6 = call i64 @clock_gettime(i32 %5, %struct.timespec* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %27
8: ; preds = %0
%9 = call i64 @gettimeofday(%struct.timeval* %2, i32* null)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %8
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%13 = load i64, i64* %12
%14 = trunc i64 %13 to i32
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %1, i64 0, i32 0
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = sub nsw i32 %14, %17
%19 = call i32 @llvm.abs.i32(i32 %18, i1 true)
%20 = icmp ult i32 %19, 2
br i1 %20, label %21, label %23
21: ; preds = %11
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %29
23: ; preds = %11
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %17)
br label %29
25: ; preds = %8
%26 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %0
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %27, %25, %23, %21
%30 = phi i32 [ 0, %21 ], [ 1, %23 ], [ 2, %25 ], [ 2, %27 ]
ret i32 %30
}