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

/* Variables and functions */
int gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ sub_timeval(struct timeval *, struct timeval *);
int test_deref(unsigned long *usec) {
  struct timeval start, end;
  unsigned long val = 0;
  unsigned long i, *ip = &i;
  int ret;
  ret = gettimeofday(&start, NULL);
  if (ret)
    goto err;
  for (i = 0; i < 9999999999UL; i++)
    val = *ip;
  ret = gettimeofday(&end, NULL);
  if (ret)
    goto err;
  sub_timeval(&end, &start);
  printf("deref:    sec %lu, usec %lu\n", end.tv_sec, end.tv_usec);
  *usec = end.tv_sec * 1000000 + end.tv_usec;
  if (val == 0)
    ret = -1;
  return ret;
err:
  perror("gettimeofday");
  return ret;
}


x86 assembly (clang, -O0):
<test_deref>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     117b <test_deref+0x3b>
jmp    1222 <test_deref+0xe2>
movq   $0x0,-0x30(%rbp)
movabs $0x2540be3ff,%rax
cmp    %rax,-0x30(%rbp)
jae    11b3 <test_deref+0x73>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1183 <test_deref+0x43>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11d2 <test_deref+0x92>
jmp    1222 <test_deref+0xe2>
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <sub_timeval@plt>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe14(%rip),%rdi
call   1040 <printf@plt>
imul   $0xf4240,-0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
movslq %eax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x28(%rbp)
jne    1217 <test_deref+0xd7>
movl   $0xffffffff,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <test_deref+0xf4>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_deref>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %eax,%eax
jne    11cc <test_deref+0x8c>
movabs $0x2540be400,%rax
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0xfffffffffffffff8,%rax
jne    1170 <test_deref+0x30>
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
test   %eax,%eax
jne    11cc <test_deref+0x8c>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1030 <sub_timeval@plt>
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
movslq 0x8(%rsp),%rax
imul   $0xf4240,%rax,%rax
movslq 0xc(%rsp),%rcx
add    %rax,%rcx
mov    %rcx,(%r14)
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,%ebx
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
jmp    11c2 <test_deref+0x82>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [29 x i8] c"deref: sec %lu, usec %lu\0A\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
define i32 @test_deref(i64* %0) {
%2 = alloca i32
%3 = alloca i64*
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64*
%9 = alloca i32
store i64* %0, i64** %3
store i64 0, i64* %6
store i64* %7, i64** %8
%10 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
store i32 %10, i32* %9
%11 = load i32, i32* %9
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
br label %49
14: ; preds = %1
store i64 0, i64* %7
br label %15
15: ; preds = %21, %14
%16 = load i64, i64* %7
%17 = icmp ult i64 %16, 9999999999
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i64*, i64** %8
%20 = load i64, i64* %19
store i64 %20, i64* %6
br label %21
21: ; preds = %18
%22 = load i64, i64* %7
%23 = add i64 %22, 1
store i64 %23, i64* %7
br label %15
24: ; preds = %15
%25 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
br label %49
29: ; preds = %24
%30 = call i32 @sub_timeval(%struct.timeval* %5, %struct.timeval* %4)
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%34 = load i32, i32* %33
%35 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %32, i32 %34)
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%37 = load i32, i32* %36
%38 = mul nsw i32 %37, 1000000
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%40 = load i32, i32* %39
%41 = add nsw i32 %38, %40
%42 = sext i32 %41 to i64
%43 = load i64*, i64** %3
store i64 %42, i64* %43
%44 = load i64, i64* %6
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %29
store i32 -1, i32* %9
br label %47
47: ; preds = %46, %29
%48 = load i32, i32* %9
store i32 %48, i32* %2
br label %52
49: ; preds = %28, %13
%50 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%51 = load i32, i32* %9
store i32 %51, i32* %2
br label %52
52: ; preds = %49, %47
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [29 x i8] c"deref: sec %lu, usec %lu\0A\00"
@.str.1 = [13 x i8] c"gettimeofday\00"
define i32 @test_deref(i64* nocapture writeonly %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %27
8: ; preds = %1, %8
%9 = phi i64 [ %10, %8 ], [ 0, %1 ]
%10 = add nuw nsw i64 %9, 8
%11 = icmp eq i64 %10, 10000000000
br i1 %11, label %12, label %8
12: ; preds = %8
%13 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %12
%16 = call i32 @sub_timeval(%struct.timeval* %3, %struct.timeval* %2)
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%20 = load i32, i32* %19
%21 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %20)
%22 = load i32, i32* %17
%23 = mul nsw i32 %22, 1000000
%24 = load i32, i32* %19
%25 = add nsw i32 %23, %24
%26 = sext i32 %25 to i64
store i64 %26, i64* %0
br label %30
27: ; preds = %12, %1
%28 = phi i32 [ %6, %1 ], [ %13, %12 ]
%29 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %27, %15
%31 = phi i32 [ %28, %27 ], [ 0, %15 ]
ret i32 %31
}