#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;
};
typedef int int32_t;

/* Variables and functions */
int diff_timeval(struct timeval *, struct timeval *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
void measure_gettimeofday(void) {
  struct timeval ta;
  struct timeval tb;
  int32_t granularity;
  int cnt = 0;
  gettimeofday(&ta, 0);
  tb = ta;
  while (tb.tv_usec == ta.tv_usec) {
    gettimeofday(&ta, 0);
    cnt++;
  }
  granularity = diff_timeval(&tb, &ta);
  printf("\tgettimeofday: %s%d ns\r\n",
         (cnt == 1 && granularity != 1000) ? "<=" : "", granularity);
}


x86 assembly (clang, -O0):
<measure_gettimeofday>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x18(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1179 <measure_gettimeofday+0x49>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1152 <measure_gettimeofday+0x22>
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <diff_timeval@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jne    11a5 <measure_gettimeofday+0x75>
cmpl   $0x3e8,-0x14(%rbp)
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%cl
lea    0xe69(%rip),%rsi
lea    0xe63(%rip),%rax
test   $0x1,%cl
cmovne %rax,%rsi
mov    -0x14(%rbp),%edx
lea    0xe39(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<measure_gettimeofday>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
mov    (%rsp),%rbx
mov    %rbx,0x8(%rsp)
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
add    $0xffffffff,%ebp
cmp    (%rsp),%rbx
je     1160 <measure_gettimeofday+0x30>
lea    0x8(%rsp),%rdi
mov    %rsp,%rsi
call   1050 <diff_timeval@plt>
cmp    $0x3e8,%eax
lea    0xe8d(%rip),%rsi
lea    0xe85(%rip),%rcx
cmove  %rcx,%rsi
test   %ebp,%ebp
cmovne %rcx,%rsi
lea    0xe5c(%rip),%rdi
mov    %eax,%edx
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64 }
@.str = [25 x i8] c"\09gettimeofday: %s%d ns\0D\0A\00"
@.str.1 = [3 x i8] c"<=\00"
@.str.2 = [1 x i8] zeroinitializer
define void @measure_gettimeofday() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %4
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%6 = bitcast %struct.timeval* %2 to i8*
%7 = bitcast %struct.timeval* %1 to i8*
br label %8
8: ; preds = %14, %0
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%10 = load i64, i64* %9
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%12 = load i64, i64* %11
%13 = icmp eq i64 %10, %12
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
store i32 %17, i32* %4
br label %8
18: ; preds = %8
%19 = call i32 @diff_timeval(%struct.timeval* %2, %struct.timeval* %1)
store i32 %19, i32* %3
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* %3
%24 = icmp ne i32 %23, 1000
br label %25
25: ; preds = %22, %18
%26 = phi i1 [ false, %18 ], [ %24, %22 ]
%27 = zext i1 %26 to i64
%28 = select i1 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%29 = load i32, i32* %3
%30 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %28, i32 %29)
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64 }
@.str = [25 x i8] c"\09gettimeofday: %s%d ns\0D\0A\00"
@.str.1 = [3 x i8] c"<=\00"
@.str.2 = [1 x i8] zeroinitializer
define void @measure_gettimeofday() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%8 = load i64, i64* %6
store i64 %8, i64* %7
br label %9
9: ; preds = %0, %9
%10 = phi i32 [ %12, %9 ], [ 0, %0 ]
%11 = call i32 @gettimeofday(%struct.timeval* %1, i32 0)
%12 = add nuw nsw i32 %10, 1
%13 = load i64, i64* %6
%14 = icmp eq i64 %8, %13
br i1 %14, label %9, label %15
15: ; preds = %9
%16 = icmp eq i32 %10, 0
%17 = call i32 @diff_timeval(%struct.timeval* %2, %struct.timeval* %1)
%18 = icmp ne i32 %17, 1000
%19 = and i1 %16, %18
%20 = select i1 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
%21 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %17)
ret void
}