#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 tv_usec;
  int tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ usleep(int);
int wfa_estimate_timer_latency() {
  struct timeval t1, t2, tp2;
  int sleep = 20000;
  int latency = 0;
  gettimeofday(&t1, NULL);
  usleep(sleep);
  gettimeofday(&t2, NULL);
  tp2.tv_usec = t1.tv_usec + 20000;
  if (tp2.tv_usec >= 1000000) {
    tp2.tv_sec = t1.tv_sec + 1;
    tp2.tv_usec -= 1000000;
  } else
    tp2.tv_sec = t1.tv_sec;
  return latency =
             (t2.tv_sec - tp2.tv_sec) * 1000000 + (t2.tv_usec - tp2.tv_usec);
}


x86 assembly (clang, -O0):
<wfa_estimate_timer_latency>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x4e20,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x1c(%rbp),%edi
call   1040 <usleep@plt>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
mov    -0x8(%rbp),%eax
add    $0x4e20,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xf4240,-0x18(%rbp)
jl     1189 <wfa_estimate_timer_latency+0x69>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x18(%rbp)
jmp    118f <wfa_estimate_timer_latency+0x6f>
mov    -0x4(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x14(%rbp),%eax
imul   $0xf4240,%eax,%eax
mov    -0x10(%rbp),%ecx
sub    -0x18(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wfa_estimate_timer_latency>:
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    $0x4e20,%edi
call   1040 <usleep@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
mov    0x10(%rsp),%ecx
xor    %edx,%edx
cmp    $0xef420,%ecx
setge  %dl
mov    $0xef420,%esi
mov    $0xffffb1e0,%eax
cmovge %esi,%eax
add    0x14(%rsp),%edx
sub    %ecx,%eax
mov    0xc(%rsp),%ecx
sub    %edx,%ecx
imul   $0xf4240,%ecx,%ecx
add    0x8(%rsp),%eax
add    %ecx,%eax
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @wfa_estimate_timer_latency() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
store i32 20000, i32* %4
store i32 0, i32* %5
%6 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%7 = load i32, i32* %4
%8 = call i32 @usleep(i32 %7)
%9 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 20000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = icmp sge i32 %15, 1000000
br i1 %16, label %17, label %25
17: ; preds = %0
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%19 = load i32, i32* %18
%20 = add nsw i32 %19, 1
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%23 = load i32, i32* %22
%24 = sub nsw i32 %23, 1000000
store i32 %24, i32* %22
br label %29
25: ; preds = %0
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i32 %27, i32* %28
br label %29
29: ; preds = %25, %17
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%33 = load i32, i32* %32
%34 = sub nsw i32 %31, %33
%35 = mul nsw i32 %34, 1000000
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%39 = load i32, i32* %38
%40 = sub nsw i32 %37, %39
%41 = add nsw i32 %35, %40
store i32 %41, i32* %5
ret i32 %41
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @wfa_estimate_timer_latency() {
%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* null)
%6 = tail call i32 @usleep(i32 20000)
%7 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp sgt i32 %9, 979999
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%12 = load i32, i32* %11
%13 = select i1 %10, i32 980000, i32 -20000
%14 = zext i1 %10 to i32
%15 = add nsw i32 %12, %14
%16 = sub i32 %13, %9
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%18 = load i32, i32* %17
%19 = sub nsw i32 %18, %15
%20 = mul nsw i32 %19, 1000000
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = add i32 %22, %16
%24 = add nsw i32 %23, %20
ret i32 %24
}