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

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
scalar_t__ clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int ac, char **av) {
  struct timeval tv;
  if (gettimeofday(&tv, NULL) < 0) {
    perror("gettimeofday");
    return -2;
  }
  printf("%ld : %ld usec\n", tv.tv_sec, tv.tv_usec);
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
    perror("clock_gettime");
    return -2;
  }
  printf("%ld : %ld ns\n", ts.tv_sec, ts.tv_nsec);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
cmp    $0x0,%rax
jge    1185 <main+0x45>
lea    0xe8c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e4 <main+0xa4>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <clock_gettime@plt>
cmp    $0x0,%rax
jge    11cb <main+0x8b>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e4 <main+0xa4>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %rax,%rax
js     119c <main+0x5c>
mov    0x10(%rsp),%edx
mov    0x14(%rsp),%esi
lea    0xea9(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1030 <clock_gettime@plt>
test   %rax,%rax
js     11a5 <main+0x65>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%esi
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
lea    0xe5d(%rip),%rdi
jmp    11ac <main+0x6c>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
mov    $0xfffffffe,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.timespec = type { i32, i32 }
@.str = [13 x i8] c"gettimeofday\00"
@.str.1 = [16 x i8] c"%ld : %ld usec\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.2 = [14 x i8] c"clock_gettime\00"
@.str.3 = [14 x i8] c"%ld : %ld ns\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timeval
%7 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i64 @gettimeofday(%struct.timeval* %6, i32* null)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -2, i32* %3
br label %29
12: ; preds = %2
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %16)
%18 = load i32, i32* @CLOCK_MONOTONIC
%19 = call i64 @clock_gettime(i32 %18, %struct.timespec* %7)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %3
br label %29
23: ; preds = %12
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %27)
store i32 0, i32* %3
br label %29
29: ; preds = %23, %21, %10
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.timespec = type { i32, i32 }
@.str = [13 x i8] c"gettimeofday\00"
@.str.1 = [16 x i8] c"%ld : %ld usec\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.2 = [14 x i8] c"clock_gettime\00"
@.str.3 = [14 x i8] c"%ld : %ld ns\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timespec
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %30
10: ; preds = %2
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %14)
%16 = bitcast %struct.timespec* %4 to i8*
%17 = load i32, i32* @CLOCK_MONOTONIC
%18 = call i64 @clock_gettime(i32 %17, %struct.timespec* %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %28
22: ; preds = %10
%23 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %26)
br label %28
28: ; preds = %22, %20
%29 = phi i32 [ -2, %20 ], [ 0, %22 ]
br label %30
30: ; preds = %28, %8
%31 = phi i32 [ -2, %8 ], [ %29, %28 ]
ret i32 %31
}